summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2017-11-01 17:56:49 +0000
committerSam Thursfield <sam@afuera.me.uk>2017-12-18 17:52:10 +0000
commit7036a21d7dea0b5e53c6b751b5472d9f72082645 (patch)
tree1984cf3555bb0b530a5c3a08fe7b303a71cc330e
parentcb04feb6480df40672382106f1c475a71b20b49f (diff)
downloadtracker-7036a21d7dea0b5e53c6b751b5472d9f72082645.tar.gz
utils: Allow specifying output path for Barnum-based data generator
This makes it usable from Meson, as previously the only way to control where the output went would be to change the working directory but Meson's custom_target() rule doesn't make that possible.
-rwxr-xr-xutils/data-generators/cc/generate26
-rw-r--r--utils/data-generators/cc/tools.py9
2 files changed, 24 insertions, 11 deletions
diff --git a/utils/data-generators/cc/generate b/utils/data-generators/cc/generate
index 03f133d45..5fa3bb608 100755
--- a/utils/data-generators/cc/generate
+++ b/utils/data-generators/cc/generate
@@ -1,6 +1,8 @@
#! /usr/bin/python2
# -*- coding: utf-8 -*-
+import argparse
+import os
import string
import time
import sys
@@ -32,25 +34,33 @@ def recent_enough_python ():
####################################################################################
-# we need a count
-if len(sys.argv) != 2:
- print "Usage: %s config-file" % sys.argv[0]
- sys.exit(1)
+def argument_parser():
+ parser = argparse.ArgumentParser(description="Nepomuk test data generator")
+ parser.add_argument('config_file', nargs=1)
+ parser.add_argument('output_dir', nargs='?')
+ return parser
+
+
+args = argument_parser().parse_args()
config = ConfigParser.RawConfigParser()
try:
- loaded_files = config.read(sys.argv[1])
+ loaded_files = config.read(args.config_file)
# config.read
# in 2.3 return None
# in 2.6+ returns a list of loaded files
if recent_enough_python ():
if (len (loaded_files) != 1):
- print "Cannot open %s" % (sys.argv[1])
+ print "Cannot open %s" % (args.config_file)
sys.exit (-1)
except Exception, e:
- print "Failed to read configuration file %s (%s)" % (sys.argv[1], e)
+ print "Failed to read configuration file %s (%s)" % (args.config_file, e)
sys.exit (-1)
+if args.output_dir:
+ if not os.path.exists(args.output_dir):
+ os.makedirs(args.output_dir)
+
def get_counter (section, option):
if config.has_option (section, option):
@@ -283,4 +293,4 @@ for index in xrange(1,count_others+1):
print "Done"
# dump all files
-tools.saveResult()
+tools.saveResult(output_dir=args.output_dir)
diff --git a/utils/data-generators/cc/tools.py b/utils/data-generators/cc/tools.py
index 049f8133e..d516bd3d6 100644
--- a/utils/data-generators/cc/tools.py
+++ b/utils/data-generators/cc/tools.py
@@ -3,6 +3,7 @@
import string
import random
import datetime
+import os
import ontology_prefixes
@@ -13,7 +14,7 @@ now = datetime.datetime.today().strftime('%Y-%m-%dT%H:%M:%SZ')
####################################################################################
def addType(name, order):
- output = 'ttl/%03d-' % order + name.replace( '#', '_') + '.ttl'
+ output = '%03d-' % order + name.replace( '#', '_') + '.ttl'
output_filenames[name] = output
result[name] = []
@@ -29,10 +30,12 @@ def getLastUri(type):
def getRandomUri(type):
return random.choice(last_uris[type])
-def saveResult ():
+def saveResult (output_dir=None):
+ output_dir = output_dir or 'ttl'
for ontology, content in result.items():
print 'Saving', output_filenames[ontology], '...'
- output = open( output_filenames[ontology], 'w')
+ path = os.path.join(output_dir, output_filenames[ontology])
+ output = open(path, 'w')
output.write( ontology_prefixes.ontology_prefixes )
for it in content:
output.write( it )