annotate makeitso/main.py @ 2:825922315ce6

make shebang work
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 04 Nov 2010 18:58:11 -0700
parents c2f8464e0395
children 8931be6d2c8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 def template_variables(template):
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 """return the variables needed for a template"""
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 raise NotImplementedError
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 def main(args=sys.argv[1:]):
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 # create option parser
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 usage = '%prog [options]'
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 parser = OptionParser(usage, description=__doc__)
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 parser.add_option('--variables', dest='variables',
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 help='print the variables in a template')
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 options, args = parser.parse_args(args)
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 # template variables
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 variables = {}
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 _vars = []
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 _args = []
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 for arg in args:
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 if '=' in arg:
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 key, value = arg.split('=')
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 variables[key] = value
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 else:
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 _args.append(arg)
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 args = _args
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 # get the content
2
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
48 if args:
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
49 for arg in args:
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
50 content = file(arg).read()
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
51 template = Template(content)
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
52 print template.substitute(**variables)
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
53 else:
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
54 content = sys.stdin.read()
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
55 template = Template(content)
825922315ce6 make shebang work
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
56 print template.substitute(**variables)
0
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 if __name__ == '__main__':
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 main()