Mercurial > hg > MakeItSo
annotate makeitso/main.py @ 3:8931be6d2c8a
stub for getting missing variables
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 04 Nov 2010 19:12:56 -0700 |
parents | 825922315ce6 |
children |
rev | line source |
---|---|
0
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 """ |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 filesystem template interpreter |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """ |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 import os |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 import subprocess |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 import sys |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 from optparse import OptionParser |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 from tempita import Template |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 def call(command, *args, **kw): |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 code = subprocess.call(command, *args, **kw) |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 if code: |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 if isinstance(command, basestring): |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 cmdstr = command |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 else: |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 cmdstr = ' '.join(command) |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 |
3
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
22 def get_missing(name_error): |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
23 """ |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
24 This is a horrible hack because python doesn't do the proper thing |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
25 via eval and return the name of the variable; instead, it just gives |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
26 you a message: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
27 >>> try: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
28 ... eval('2*foo') |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
29 ... except Exception, e: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
30 ... pass |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
31 """ |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
32 message = name_error.args[0] |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
33 varname = message.split("'")[1] |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
34 return varname |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
35 |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
36 def missing_variables(template, variables): |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
37 """return additional variables needed""" |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
38 vars = variables.copy() |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
39 missing = set([]) |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
40 while True: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
41 try: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
42 template.substitute(**missing) |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
43 return missing |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
44 except NameError, e: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
45 missed = get_missing(e) |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
46 missing.add(missed) |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
47 vars[missed] = '' |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
48 |
0
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 def template_variables(template): |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 """return the variables needed for a template""" |
3
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
51 return missing_variables(template, {}) |
0
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
52 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 def main(args=sys.argv[1:]): |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 # create option parser |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 usage = '%prog [options]' |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 parser = OptionParser(usage, description=__doc__) |
3
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
58 parser.add_option('--variables', dest='variables', action='store_true', |
0
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 help='print the variables in a template') |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 options, args = parser.parse_args(args) |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 |
3
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
62 if options.variables: |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
63 variables = template_variables() # TODO: pass template |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
64 return |
8931be6d2c8a
stub for getting missing variables
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
65 |
0
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
66 # template variables |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 variables = {} |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 _vars = [] |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 _args = [] |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 for arg in args: |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 if '=' in arg: |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 key, value = arg.split('=') |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 variables[key] = value |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 else: |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
75 _args.append(arg) |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 args = _args |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 # get the content |
2 | 79 if args: |
80 for arg in args: | |
81 content = file(arg).read() | |
82 template = Template(content) | |
83 print template.substitute(**variables) | |
84 else: | |
85 content = sys.stdin.read() | |
86 template = Template(content) | |
87 print template.substitute(**variables) | |
0
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
88 |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 if __name__ == '__main__': |
7a76836b50a7
initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
90 main() |