annotate makeitso/makeitso.py @ 6:ac78e26cd568

starting on web example; i have to understand more about shebangs
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 10 Nov 2010 18:11:11 -0800
parents f064be514e53
children a77630b2b491
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
6
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
17 class MissingVariablesException(Exception):
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
18 def __init__(self, message, missing):
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
19 self.missing = missing
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
20
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 def call(command, *args, **kw):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 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
23 if code:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if isinstance(command, basestring):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 cmdstr = command
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 cmdstr = ' '.join(command)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 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
29
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 def get_missing(name_error):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 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
33 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
34 you a message:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 >>> try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 ... eval('2*foo')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 ... except Exception, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 ... pass
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 message = name_error.args[0]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 varname = message.split("'")[1]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 return varname
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 def missing_variables(template, variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 """return additional variables needed"""
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 vars = variables.copy()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 missing = set([])
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 while True:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 template.substitute(**vars)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 except NameError, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 missed = get_missing(e)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 missing.add(missed)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 vars[missed] = ''
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 return missing
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 template_variables(template):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 """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
60 return missing_variables(template, {})
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 def read_variables(variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 retval = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 for i in variables:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 print 'Enter %s: ' % i,
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 retval[i] = raw_input()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 return retval
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 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
70
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 # 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
72 if shebang_re.match(content):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 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
74
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 variables = variables or {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 template = Template(content)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 missing = missing_variables(template, variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 if missing:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 # 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
80 variables.update(read_variables(missing))
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 print >> fp, template.substitute(**variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 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
85
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 # create option parser
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 usage = '%prog [options]'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 parser = OptionParser(usage, description=__doc__)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 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
90 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
91 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
92
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93 if options.variables:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 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
95 return
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 # template variables
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 variables = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 _vars = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 _args = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 if '=' in arg:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 key, value = arg.split('=')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 variables[key] = value
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 _args.append(arg)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107 args = _args
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 # get the content
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 if args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 content = file(arg).read()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 substitute(content, variables=variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 content = sys.stdin.read()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 substitute(content, variables=variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118 if __name__ == '__main__':
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
119 main()