[tracker/wip/sam/meson-functional-tests: 58/60] utils: Allow specifying output path for Barnum-based data generator



commit 7036a21d7dea0b5e53c6b751b5472d9f72082645
Author: Sam Thursfield <sam thursfield codethink co uk>
Date:   Wed Nov 1 17:56:49 2017 +0000

    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.

 utils/data-generators/cc/generate |   26 ++++++++++++++++++--------
 utils/data-generators/cc/tools.py |    9 ++++++---
 2 files changed, 24 insertions(+), 11 deletions(-)
---
diff --git a/utils/data-generators/cc/generate b/utils/data-generators/cc/generate
index 03f133d..5fa3bb6 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 049f813..d516bd3 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 )


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]