annotate makeitso/makeitso.py @ 32:9422d4ad6c2c

add interactive flag and note about it
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 22 Dec 2010 18:46:45 -0800
parents 17f46f0e0a4a
children 190f310f2f5e
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
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
6 import inspect
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import os
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import re
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
9 import shutil
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import subprocess
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import sys
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
12 import tempfile
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
13 import urllib
24
1b802678b341 add note about urllib and timeouts
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
14 # TODO: may have to use urllib2.urlopen to get reasonable timeouts
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 from optparse import OptionParser
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
17
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
18 # URL of this file
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
19 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
20
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
21 # URL of tempita
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
22 tempita_location = 'http://bitbucket.org/ianb/tempita/raw-file/tip/tempita/'
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
23
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
24 def cleanup():
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
25 # remove temporary net module directory
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
26 if 'tempdir' in globals():
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
27 shutil.remove(tempdir)
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
28
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
29 try:
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
30 import tempita
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
31 except ImportError:
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
32
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
33 # Get tempita from the net
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
34 # TODO: abstract this to get arbitrary modules from the net
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
35 def getFiles(url, subdir, files):
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
36 """
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
37 fetch files from the internet
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
38 - url : base url
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
39 - subdirectory: to put things in
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
40 - files : list of files to retrieve
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
41 returns the location of a temporary directory
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
42 """
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
43 globals()['tempdir'] = tempfile.mkdtemp()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
44 os.mkdir(subdir)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
45 url = url.rstrip('/')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
46 for filename in files:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
47 f, headers = urllib.urlretrive('%s/%s' % (url, filename))
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
48 content = file(f).read()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
49 outfile = os.path.join(globals()['tempdir'], subdir, filename)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
50 o = file(outfile, 'w')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
51 print >> o, content
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
52 return globals()['tempdir']
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
53
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
54 tempita_files = ['__init__.py', '_looper.py', 'compat3.py']
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
55
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
56 try:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
57 t = getFiles(tempita_location, 'tempita', tempita_files)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
58 sys.path.append(t)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
59 import tempita
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
60 except:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
61 cleanup()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
62 raise NotImplementedError('This should say something like youre not connected to the net')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
63
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
64 # does tempita support delimeters?
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
65 has_delimeters = 'delimeters' in inspect.getargspec(tempita.Template.__init__).args
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
66
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
67 # regular expressions for finding the shebang
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 shebang_re = '#!.*makeitso.*'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 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
70
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
71 def base_uri(uri):
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
72 if '://' in uri:
27
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
73 return 'uri'.rsplit('/', 1)[0] + '/'
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
74 else:
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
75 here = os.path.dirname(os.path.abspath('me'))
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
76 here = here.rstrip(os.path.sep) + os.path.sep
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
77 return here
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
78
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
79 def include(uri):
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
80 f, headers = urllib.urlretrieve(uri)
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
81 return file(f).read()
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
82
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
83 ### things that deal with variables
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
84
31
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
85 # XXX duplicated in URITemplate namespace....don't need two
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
86 defaults = {'include': include}
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
87
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
88 class MissingVariablesException(Exception):
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
89 """exception for (non-interactive) missing variables"""
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
90 def __init__(self, message, missing):
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
91 self.missing = missing
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
92
31
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
93 class ContentTemplate(tempita.Template):
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
94 """MakeItSo's extension of tempita's Template class"""
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
95 defaults = {'include': include}
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
96 def __init__(self):
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
97 raise NotImplementedError
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
98
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
99 class URITemplate(tempita.Template):
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
100
32
9422d4ad6c2c add interactive flag and note about it
Jeff Hammel <jhammel@mozilla.com>
parents: 31
diff changeset
101 def __init__(self, interactive=True):
9422d4ad6c2c add interactive flag and note about it
Jeff Hammel <jhammel@mozilla.com>
parents: 31
diff changeset
102 # TODO: automagically tell if the program is interactive or not
31
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
103 raise NotImplementedError
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
104
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
105 class DirectoryTemplate(tempita.Template):
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
106 def __init__(self):
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
107 raise NotImplementedError
17f46f0e0a4a stub of using classes for this
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
108
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 def get_missing(name_error):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 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
112 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
113 you a message:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 >>> try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 ... eval('2*foo')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 ... except Exception, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117 ... pass
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
119 message = name_error.args[0]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
120 varname = message.split("'")[1]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
121 return varname
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
122
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
123 def missing_variables(template, variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
124 """return additional variables needed"""
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
125 vars = variables.copy()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
126 missing = set([])
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
127 while True:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
128 try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
129 template.substitute(**vars)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
130 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
131 except NameError, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
132 missed = get_missing(e)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
133 missing.add(missed)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
134 vars[missed] = ''
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
135 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
136
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
137 def template_variables(template):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
138 """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
139 return missing_variables(template, {})
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
140
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
141 def read_variables(variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
142 retval = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
143 for i in variables:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
144 print 'Enter %s: ' % i,
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
145 retval[i] = raw_input()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
146 return retval
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
147
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
148 ### functions for substitution
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
149
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
150 def substitute(content, variables=None):
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
151 """interactive (for now) substitution"""
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
152
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
153 # 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
154 if shebang_re.match(content):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
155 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
156
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
157 variables = variables or defaults.copy()
13
99588f138993 use full dot path to template
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
158 template = tempita.Template(content)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
159 missing = missing_variables(template, variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
160 if missing:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
161 # 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
162 variables.update(read_variables(missing))
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
163 return template.substitute(**variables)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
164
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
165 def substitute_directory(directory, output=None, variables=None):
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
166 # TODO: interpolate directory names
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
167
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
168 ###
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
169
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
170 def invocation(url, **variables):
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
171 """returns a string appropriate for TTW invocation"""
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
172 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()])
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
173 return 'python <(curl %s) %s %s' % (location, url, variables_string)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
174
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
175 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
176
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
177 # create option parser
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
178 usage = '%prog [options] template <template> <...>'
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
179 parser = OptionParser(usage, description=__doc__)
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
180
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
181 # delimeters
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
182 # XXX needs tempita trunk
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
183 if has_delimeters:
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
184 parser.add_option('-[', '--start-braces', dest='start_braces',
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
185 help='starting delimeter')
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
186 parser.add_option('-]', '--end-braces', dest='end_braces',
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
187 help='starting delimeter')
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
188
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
189 # options about where to put things
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
190 parser.add_option('--in-place', dest='in_place',
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
191 action='store_true', default=False,
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
192 help='interpret files in place') # TODO: unused
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
193 parser.add_option('-o', '--output', dest='output',
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
194 help='where to put the output (filename or directory)')
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
195
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
196 #
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
197 parser.add_option('--commandline', dest='commandline',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
198 action='store_true', default=False,
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
199 help="print the commandline to invoke this script TTW")
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
200 parser.add_option('--variables', dest='variables',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
201 action='store_true', default=False,
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
202 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
203 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
204
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
205 # print the variables for the templates
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
206 if options.variables:
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
207
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
208 # makes no sense without a template
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
209 if not args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
210 parser.print_usage()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
211 parser.exit()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
212
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
213 # find all variables
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
214 variables = set()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
215 for arg in args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
216 content = file(arg).read()
13
99588f138993 use full dot path to template
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
217 template = tempita.Template(content)
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
218 variables.update(template_variables(template))
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
219
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
220 # print them
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
221 for variable in variables:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
222 print variable
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
223 return
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
224
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
225 # template variables
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
226 variables = defaults.copy()
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
227 _vars = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
228 _args = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
229 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
230 if '=' in arg:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
231 key, value = arg.split('=')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
232 variables[key] = value
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
233 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
234 _args.append(arg)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
235 args = _args
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
236
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
237 # print TTW commandline for invocation
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
238 if options.commandline:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
239 if args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
240 for arg in args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
241 print invocation(arg, **variables)
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
242 else:
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
243 print invocation('[URI]', **variables)
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
244 return
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
245
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
246 # get the content
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
247 if args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
248 for arg in args:
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
249 var_copy = variables.copy()
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
250 var_copy['here'] = base_uri(arg)
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
251 content = include(arg)
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
252 print substitute(content, variables=var_copy)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
253 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
254 content = sys.stdin.read()
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
255 print substitute(content, variables=variables)
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
256
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
257 # cleanup
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
258 cleanup()
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
259
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
260 if __name__ == '__main__':
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
261 main()