changeset 9:a77630b2b491

* add commandline to print commandlines * add commandline option to print all variables
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 11 Nov 2010 18:19:44 -0800
parents 157df8d1c3ed
children ce98460ba1fc
files makeitso/makeitso.py
diffstat 1 files changed, 40 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/makeitso/makeitso.py	Thu Nov 11 16:33:27 2010 -0800
+++ b/makeitso/makeitso.py	Thu Nov 11 18:19:44 2010 -0800
@@ -11,6 +11,10 @@
 from optparse import OptionParser
 from tempita import Template
 
+# URL of this file
+location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
+
+# regular expressions for finding the shebang
 shebang_re = '#!.*makeitso.*'
 shebang_re = re.compile(shebang_re)
 
@@ -80,18 +84,42 @@
         variables.update(read_variables(missing))
     print >> fp, template.substitute(**variables)
 
+def invocation(url, **variables):
+    """returns a string appropriate for TTW invocation"""
+    variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()])
+    return 'python <(curl %s) %s %s' % (location, url, variables_string)
 
 def main(args=sys.argv[1:]):
 
     # create option parser
-    usage = '%prog [options]'
+    usage = '%prog [options] template <template> <...>'
     parser = OptionParser(usage, description=__doc__)
-    parser.add_option('--variables', dest='variables', action='store_true',
+    parser.add_option('--commandline', dest='commandline',
+                      action='store_true', default=False,
+                      help="print the commandline to invoke this script TTW")
+    parser.add_option('--variables', dest='variables',
+                      action='store_true', default=False,
                       help='print the variables in a template')
     options, args = parser.parse_args(args)
 
+    # print the variables for the templates
     if options.variables:
-        variables = template_variables() # TODO: pass template
+
+        # makes no sense without a template
+        if not args:
+            parser.print_usage()
+            parser.exit()
+
+        # find all variables
+        variables = set()
+        for arg in args:
+            content = file(arg).read()
+            template = Template(content)
+            variables.update(template_variables(template))
+
+        # print them
+        for variable in variables:
+            print variable
         return
 
     # template variables
@@ -106,6 +134,15 @@
             _args.append(arg)
     args = _args
 
+    # print TTW commandline for invocation
+    if options.commandline:
+        if args:
+            for arg in args:
+                print invocation(arg, **variables)
+        else:
+            print invocation('[URL]', **variables)
+        return
+
     # get the content
     if args:
         for arg in args: