comparison makeitso/makeitso.py @ 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 ac78e26cd568
children a6fd93354f23
comparison
equal deleted inserted replaced
8:157df8d1c3ed 9:a77630b2b491
9 import sys 9 import sys
10 10
11 from optparse import OptionParser 11 from optparse import OptionParser
12 from tempita import Template 12 from tempita import Template
13 13
14 # URL of this file
15 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
16
17 # regular expressions for finding the shebang
14 shebang_re = '#!.*makeitso.*' 18 shebang_re = '#!.*makeitso.*'
15 shebang_re = re.compile(shebang_re) 19 shebang_re = re.compile(shebang_re)
16 20
17 class MissingVariablesException(Exception): 21 class MissingVariablesException(Exception):
18 def __init__(self, message, missing): 22 def __init__(self, message, missing):
78 if missing: 82 if missing:
79 # TODO: add a switch for interactive or not 83 # TODO: add a switch for interactive or not
80 variables.update(read_variables(missing)) 84 variables.update(read_variables(missing))
81 print >> fp, template.substitute(**variables) 85 print >> fp, template.substitute(**variables)
82 86
87 def invocation(url, **variables):
88 """returns a string appropriate for TTW invocation"""
89 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()])
90 return 'python <(curl %s) %s %s' % (location, url, variables_string)
83 91
84 def main(args=sys.argv[1:]): 92 def main(args=sys.argv[1:]):
85 93
86 # create option parser 94 # create option parser
87 usage = '%prog [options]' 95 usage = '%prog [options] template <template> <...>'
88 parser = OptionParser(usage, description=__doc__) 96 parser = OptionParser(usage, description=__doc__)
89 parser.add_option('--variables', dest='variables', action='store_true', 97 parser.add_option('--commandline', dest='commandline',
98 action='store_true', default=False,
99 help="print the commandline to invoke this script TTW")
100 parser.add_option('--variables', dest='variables',
101 action='store_true', default=False,
90 help='print the variables in a template') 102 help='print the variables in a template')
91 options, args = parser.parse_args(args) 103 options, args = parser.parse_args(args)
92 104
105 # print the variables for the templates
93 if options.variables: 106 if options.variables:
94 variables = template_variables() # TODO: pass template 107
108 # makes no sense without a template
109 if not args:
110 parser.print_usage()
111 parser.exit()
112
113 # find all variables
114 variables = set()
115 for arg in args:
116 content = file(arg).read()
117 template = Template(content)
118 variables.update(template_variables(template))
119
120 # print them
121 for variable in variables:
122 print variable
95 return 123 return
96 124
97 # template variables 125 # template variables
98 variables = {} 126 variables = {}
99 _vars = [] 127 _vars = []
104 variables[key] = value 132 variables[key] = value
105 else: 133 else:
106 _args.append(arg) 134 _args.append(arg)
107 args = _args 135 args = _args
108 136
137 # print TTW commandline for invocation
138 if options.commandline:
139 if args:
140 for arg in args:
141 print invocation(arg, **variables)
142 else:
143 print invocation('[URL]', **variables)
144 return
145
109 # get the content 146 # get the content
110 if args: 147 if args:
111 for arg in args: 148 for arg in args:
112 content = file(arg).read() 149 content = file(arg).read()
113 substitute(content, variables=variables) 150 substitute(content, variables=variables)