annotate makeitso/makeitso.py @ 5:f064be514e53

choose a better filename and remove some stuff from shebang
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 10 Nov 2010 18:01:07 -0800
parents
children ac78e26cd568
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 filesystem template interpreter
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import re
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import subprocess
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import sys
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 from optparse import OptionParser
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from tempita import Template
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 shebang_re = '#!.*makeitso.*'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 shebang_re = re.compile(shebang_re)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def call(command, *args, **kw):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 code = subprocess.call(command, *args, **kw)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 if code:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 if isinstance(command, basestring):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 cmdstr = command
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 cmdstr = ' '.join(command)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 def get_missing(name_error):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 This is a horrible hack because python doesn't do the proper thing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 via eval and return the name of the variable; instead, it just gives
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 you a message:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 >>> try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 ... eval('2*foo')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 ... except Exception, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 ... pass
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 message = name_error.args[0]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 varname = message.split("'")[1]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 return varname
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 def missing_variables(template, variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 """return additional variables needed"""
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 vars = variables.copy()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 missing = set([])
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 while True:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 template.substitute(**vars)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 except NameError, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 missed = get_missing(e)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 missing.add(missed)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 vars[missed] = ''
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 def template_variables(template):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 """return the variables needed for a template"""
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 return missing_variables(template, {})
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 def read_variables(variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 retval = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 for i in variables:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 print 'Enter %s: ' % i,
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 retval[i] = raw_input()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 return retval
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 def substitute(content, fp=sys.stdout, variables=None):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 # remove makeitso shebang if it has one
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 if shebang_re.match(content):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 content = os.linesep.join(content.splitlines()[1:])
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 variables = variables or {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 template = Template(content)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 missing = missing_variables(template, variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 if missing:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 # TODO: add a switch for interactive or not
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 variables.update(read_variables(missing))
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 print >> fp, template.substitute(**variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 def main(args=sys.argv[1:]):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 # create option parser
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 usage = '%prog [options]'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 parser = OptionParser(usage, description=__doc__)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 parser.add_option('--variables', dest='variables', action='store_true',
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 help='print the variables in a template')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 options, args = parser.parse_args(args)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 if options.variables:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 variables = template_variables() # TODO: pass template
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 return
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93 # template variables
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 variables = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 _vars = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 _args = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 if '=' in arg:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 key, value = arg.split('=')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 variables[key] = value
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 _args.append(arg)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 args = _args
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 # get the content
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 if args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 content = file(arg).read()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 substitute(content, variables=variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 content = sys.stdin.read()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 substitute(content, variables=variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 if __name__ == '__main__':
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 main()