comparison makeitso/makeitso.py @ 30:1549be7f0fcb

dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 22 Dec 2010 14:34:20 -0800
parents dc18d6db4956
children 17f46f0e0a4a
comparison
equal deleted inserted replaced
29:7e8a5da34eee 30:1549be7f0fcb
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 """ 2 """
3 filesystem template interpreter 3 filesystem template interpreter
4 """ 4 """
5 5
6 import inspect
6 import os 7 import os
7 import re 8 import re
8 import shutil 9 import shutil
9 import subprocess 10 import subprocess
10 import sys 11 import sys
58 import tempita 59 import tempita
59 except: 60 except:
60 cleanup() 61 cleanup()
61 raise NotImplementedError('This should say something like youre not connected to the net') 62 raise NotImplementedError('This should say something like youre not connected to the net')
62 63
63 64 # does tempita support delimeters?
65 has_delimeters = 'delimeters' in inspect.getargspec(tempita.Template.__init__).args
64 66
65 # regular expressions for finding the shebang 67 # regular expressions for finding the shebang
66 shebang_re = '#!.*makeitso.*' 68 shebang_re = '#!.*makeitso.*'
67 shebang_re = re.compile(shebang_re) 69 shebang_re = re.compile(shebang_re)
68
69 class MissingVariablesException(Exception):
70 def __init__(self, message, missing):
71 self.missing = missing
72
73 def call(command, *args, **kw):
74 code = subprocess.call(command, *args, **kw)
75 if code:
76 if isinstance(command, basestring):
77 cmdstr = command
78 else:
79 cmdstr = ' '.join(command)
80 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
81 70
82 def base_uri(uri): 71 def base_uri(uri):
83 if '://' in uri: 72 if '://' in uri:
84 return 'uri'.rsplit('/', 1)[0] + '/' 73 return 'uri'.rsplit('/', 1)[0] + '/'
85 else: 74 else:
89 78
90 def include(uri): 79 def include(uri):
91 f, headers = urllib.urlretrieve(uri) 80 f, headers = urllib.urlretrieve(uri)
92 return file(f).read() 81 return file(f).read()
93 82
94 ### functions for variables 83 ### things that deal with variables
95 84
96 defaults = {'include': include} 85 defaults = {'include': include}
86
87 class MissingVariablesException(Exception):
88 """exception for (non-interactive) missing variables"""
89 def __init__(self, message, missing):
90 self.missing = missing
97 91
98 def get_missing(name_error): 92 def get_missing(name_error):
99 """ 93 """
100 This is a horrible hack because python doesn't do the proper thing 94 This is a horrible hack because python doesn't do the proper thing
101 via eval and return the name of the variable; instead, it just gives 95 via eval and return the name of the variable; instead, it just gives
167 usage = '%prog [options] template <template> <...>' 161 usage = '%prog [options] template <template> <...>'
168 parser = OptionParser(usage, description=__doc__) 162 parser = OptionParser(usage, description=__doc__)
169 163
170 # delimeters 164 # delimeters
171 # XXX needs tempita trunk 165 # XXX needs tempita trunk
172 parser.add_option('-[', '--start-braces', dest='start_braces', 166 if has_delimeters:
173 help='starting delimeter') 167 parser.add_option('-[', '--start-braces', dest='start_braces',
174 parser.add_option('-]', '--end-braces', dest='end_braces', 168 help='starting delimeter')
175 help='starting delimeter') 169 parser.add_option('-]', '--end-braces', dest='end_braces',
170 help='starting delimeter')
176 171
177 # options about where to put things 172 # options about where to put things
178 parser.add_option('--in-place', dest='in_place', 173 parser.add_option('--in-place', dest='in_place',
179 action='store_true', default=False, 174 action='store_true', default=False,
180 help='interpret files in place') # TODO: unused 175 help='interpret files in place') # TODO: unused