Mercurial > hg > MakeItSo
annotate makeitso/makeitso.py @ 27:ac44c36da885
make include example work
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 14 Dec 2010 22:46:56 -0800 |
parents | 044ac9e0b29c |
children | dc18d6db4956 |
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 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 import os |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 import re |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
8 import shutil |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 import subprocess |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 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
|
11 import tempfile |
15 | 12 import urllib |
24
1b802678b341
add note about urllib and timeouts
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
13 # 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
|
14 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 from optparse import OptionParser |
12
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
16 |
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
|
17 # 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
|
18 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
|
19 |
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 # 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
|
21 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
|
22 |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
23 def cleanup(): |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
24 # remove temporary net module directory |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
25 if 'tempdir' in globals(): |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
26 shutil.remove(tempdir) |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
27 |
12
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
28 try: |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
29 import tempita |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
30 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
|
31 |
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 # Get tempita from the net |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
33 # 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
|
34 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
|
35 """ |
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 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
|
37 - url : base url |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
38 - 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
|
39 - 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
|
40 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
|
41 """ |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
42 globals()['tempdir'] = tempfile.mkdtemp() |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
43 os.mkdir(subdir) |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
44 url = url.rstrip('/') |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
45 for filename in files: |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
46 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
|
47 content = file(f).read() |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
48 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
|
49 o = file(outfile, 'w') |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
50 print >> o, content |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
51 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
|
52 |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
53 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
|
54 |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
55 try: |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
56 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
|
57 sys.path.append(t) |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
58 import tempita |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
59 except: |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
60 cleanup() |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
61 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
|
62 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
64 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
65 # 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
|
66 shebang_re = '#!.*makeitso.*' |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 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
|
68 |
6
ac78e26cd568
starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
69 class MissingVariablesException(Exception): |
ac78e26cd568
starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
70 def __init__(self, message, missing): |
ac78e26cd568
starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
71 self.missing = missing |
ac78e26cd568
starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
72 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 def call(command, *args, **kw): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 code = subprocess.call(command, *args, **kw) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
75 if code: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 if isinstance(command, basestring): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 cmdstr = command |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 else: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 cmdstr = ' '.join(command) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
81 |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
82 def base_uri(uri): |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
83 if '://' in uri: |
27 | 84 return 'uri'.rsplit('/', 1)[0] + '/' |
85 else: | |
86 here = os.path.dirname(os.path.abspath('me')) | |
87 here = here.rstrip(os.path.sep) + os.path.sep | |
88 return here | |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
89 |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
90 def include(uri): |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
91 f, headers = urllib.urlretrieve(uri) |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
92 return file(f).read() |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
93 |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
94 ### functions for variables |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
95 |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
96 defaults = {'include': include} |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
97 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
98 def get_missing(name_error): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
99 """ |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
100 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
|
101 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
|
102 you a message: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
103 >>> try: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
104 ... eval('2*foo') |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
105 ... except Exception, e: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
106 ... pass |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
107 """ |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
108 message = name_error.args[0] |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
109 varname = message.split("'")[1] |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
110 return varname |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
111 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
112 def missing_variables(template, variables): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
113 """return additional variables needed""" |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
114 vars = variables.copy() |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
115 missing = set([]) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
116 while True: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
117 try: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
118 template.substitute(**vars) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
119 return missing |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
120 except NameError, e: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
121 missed = get_missing(e) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
122 missing.add(missed) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
123 vars[missed] = '' |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
124 return missing |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
125 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
126 def template_variables(template): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
127 """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
|
128 return missing_variables(template, {}) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
129 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
130 def read_variables(variables): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
131 retval = {} |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
132 for i in variables: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
133 print 'Enter %s: ' % i, |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
134 retval[i] = raw_input() |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
135 return retval |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
136 |
16
1818cc524cde
change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
137 def substitute(content, variables=None): |
1818cc524cde
change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
138 """interactive (for now) substitution""" |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
139 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
140 # 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
|
141 if shebang_re.match(content): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
142 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
|
143 |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
144 variables = variables or defaults.copy() |
13
99588f138993
use full dot path to template
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
145 template = tempita.Template(content) |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
146 missing = missing_variables(template, variables) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
147 if missing: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
148 # 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
|
149 variables.update(read_variables(missing)) |
16
1818cc524cde
change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
150 return template.substitute(**variables) |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
151 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
152 def invocation(url, **variables): |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
153 """returns a string appropriate for TTW invocation""" |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
154 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
|
155 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
|
156 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
157 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
|
158 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
159 # create option parser |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
160 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
|
161 parser = OptionParser(usage, description=__doc__) |
12
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
162 parser.add_option('-[', '--start-braces', dest='start_braces', |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
163 help='starting delimeter') |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
164 parser.add_option('-]', '--end-braces', dest='end_braces', |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
165 help='starting delimeter') |
15 | 166 parser.add_option('--in-place', dest='in_place', |
167 action='store_true', default=False, | |
168 help='interpret files in place') # TODO: unused | |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
169 parser.add_option('--commandline', dest='commandline', |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
170 action='store_true', default=False, |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
171 help="print the commandline to invoke this script TTW") |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
172 parser.add_option('--variables', dest='variables', |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
177 # 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
|
178 if options.variables: |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
179 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
180 # makes no sense without a template |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
181 if not args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
182 parser.print_usage() |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
183 parser.exit() |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
184 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
185 # find all variables |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
186 variables = set() |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
187 for arg in args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
188 content = file(arg).read() |
13
99588f138993
use full dot path to template
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
189 template = tempita.Template(content) |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
190 variables.update(template_variables(template)) |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
191 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
192 # print them |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
193 for variable in variables: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
194 print variable |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
195 return |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
196 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
197 # template variables |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
198 variables = defaults.copy() |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
199 _vars = [] |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
200 _args = [] |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
201 for arg in args: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
202 if '=' in arg: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
203 key, value = arg.split('=') |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
204 variables[key] = value |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
205 else: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
206 _args.append(arg) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
207 args = _args |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
208 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
209 # print TTW commandline for invocation |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
210 if options.commandline: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
211 if args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
212 for arg in args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
213 print invocation(arg, **variables) |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
214 else: |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
215 print invocation('[URI]', **variables) |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
216 return |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
217 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
218 # get the content |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
219 if args: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
220 for arg in args: |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
221 var_copy = variables.copy() |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
222 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
|
223 content = include(arg) |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
224 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
|
225 else: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
226 content = sys.stdin.read() |
16
1818cc524cde
change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
227 print substitute(content, variables=variables) |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
228 |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
229 # cleanup |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
230 cleanup() |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
231 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
232 if __name__ == '__main__': |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
233 main() |