annotate makeitso/main.py @ 1:c2f8464e0395

use correct method
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 03 Nov 2010 08:35:16 -0700
parents 7a76836b50a7
children 825922315ce6
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
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 content = sys.stdin.read()
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 template = Template(content)
1
c2f8464e0395 use correct method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
50 print template.substitute(**variables)
0
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
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 if __name__ == '__main__':
7a76836b50a7 initial (non-working) commit to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 main()