Mercurial > hg > carton
annotate carton.py @ 14:c474362cf69a
allow fetching of remote tarballs
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 08 Jul 2011 15:23:04 -0700 |
parents | f522620c6a78 |
children | f05e636b7444 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 make a self-extracting virtualenv from directories or URLs | |
5 of packages | |
6 | |
7 To package up all files in a virtualenvs source directory (e.g.): | |
8 | |
9 python path/to/carton.py mozmill mozmill/src/* | |
10 | |
11 This will create a self-extracting file, `mozmill.py`, that will unfold | |
12 a virtualenv | |
13 """ | |
14 | |
15 # imports | |
16 import os | |
17 import sys | |
18 import tarfile | |
19 import tempfile | |
20 import urllib2 | |
21 from optparse import OptionParser | |
22 from StringIO import StringIO | |
23 | |
24 # global variables | |
25 usage = "%prog [options] environment_name directory|url [...]" | |
26 virtualenv_url = 'http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.6.1.tar.gz' | |
27 template = """#!/usr/bin/env python | |
28 | |
29 import os | |
30 import shutil | |
31 import subprocess | |
32 import sys | |
33 import tarfile | |
34 import tempfile | |
35 from optparse import OptionParser | |
36 from StringIO import StringIO | |
37 | |
38 try: | |
39 call = subprocess.check_call | |
40 except AttributeError: | |
41 # old python; boo :( | |
42 call = subprocess.call | |
43 | |
44 # virtualenv name | |
45 ENV='''%(ENV)s''' | |
46 | |
47 # packed files | |
3
75919adb199a
use compression, but it doesnt seem to help much
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
48 VIRTUAL_ENV='''%(VIRTUAL_ENV)s'''.decode('base64').decode('zlib') |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
49 PACKAGE_SOURCES=%(PACKAGE_SOURCES)s |
0 | 50 |
51 # unpack virtualenv | |
52 tempdir = tempfile.mkdtemp() | |
53 buffer = StringIO() | |
54 buffer.write(VIRTUAL_ENV) | |
55 buffer.seek(0) | |
56 tf = tarfile.open(mode='r', fileobj=buffer) | |
57 tf.extractall(tempdir) | |
58 | |
59 # find the virtualenv | |
60 for root, dirs, files in os.walk(tempdir): | |
61 if 'virtualenv.py' in files: | |
62 virtualenv = os.path.join(root, 'virtualenv.py') | |
63 break | |
64 else: | |
65 raise Exception("virtualenv.py not found in " + tempdir) | |
66 print virtualenv | |
67 | |
68 # create the virtualenv | |
69 call([sys.executable, virtualenv, ENV]) | |
70 | |
71 # find the bin/scripts directory | |
72 for i in ('bin', 'Scripts'): | |
73 scripts_dir = os.path.abspath(os.path.join(ENV, i)) | |
74 if os.path.exists(scripts_dir): | |
75 break | |
76 else: | |
77 raise Exception("Scripts directory not found in " + ENV) | |
78 | |
79 # find the virtualenv's python | |
80 for i in ('python', 'python.exe'): | |
81 python = os.path.join(scripts_dir, i) | |
82 if os.path.exists(python): | |
83 break | |
84 else: | |
85 raise Exception("python not found in " + scripts_dir) | |
86 | |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
87 # unpack the sources and setup for development |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
88 srcdir = os.path.join(ENV, 'src') |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
89 os.mkdir(srcdir) |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
90 setup_pys = set() |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
91 for source in PACKAGE_SOURCES: |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
92 source = source.decode('base64').decode('zlib') |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
93 buffer = StringIO() |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
94 buffer.write(source) |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
95 buffer.seek(0) |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
96 tf = tarfile.open(mode='r', fileobj=buffer) |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
97 tf.extractall(srcdir) |
0 | 98 |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
99 # setup sources for development if there are any new setup.py files |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
100 for i in os.listdir(srcdir): |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
101 if i in setup_pys: |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
102 continue |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
103 subdir = os.path.join(srcdir, i) |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
104 if os.path.exists(os.path.join(srcdir, i, 'setup.py')): |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
105 call([python, 'setup.py', 'develop'], cwd=subdir) |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
106 setup_pys.add(i) |
0 | 107 |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
108 # cleanup tempdir # TODO (optionally?) |
0 | 109 # shutil.rmtree(tempdir) |
110 | |
111 # TODO: | |
112 # - add carton to the virtualenv (!) | |
113 # - add virtualenv to the virtualenv (!) | |
114 """ | |
115 | |
116 def isURL(path): | |
117 return path.startswith('http://') or path.startswith('https://') | |
118 | |
119 def main(args=sys.argv[1:]): | |
120 | |
121 # parse CLI arguments | |
122 parser = OptionParser(usage=usage, description=__doc__) | |
123 parser.add_option('-o', dest='outfile', | |
124 help="specify outfile; otherwise it will come from environment_name") | |
125 parser.add_option('--virtualenv', dest='virtualenv', | |
12
542b46ac4e28
fix description more better
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
126 help="use this virtualenv URL or file tarball") |
0 | 127 options, args = parser.parse_args(args) |
128 if len(args) < 2: | |
129 parser.print_usage() | |
130 parser.exit() | |
131 environment = args[0] | |
132 if environment.endswith('.py'): | |
133 # stop on .py; will add it in later | |
134 environment = environment[:-3] | |
135 sources = args[1:] | |
136 | |
137 # tar up the sources | |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
138 source_array = [] |
0 | 139 for source in sources: |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
140 buffer = None |
0 | 141 |
142 if isURL(source): | |
14
c474362cf69a
allow fetching of remote tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
143 # remote tarball or resource |
c474362cf69a
allow fetching of remote tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
144 buffer = urllib2.urlopen(source).read() |
0 | 145 else: |
13
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
146 assert os.path.exists(source), "%s does not exist" % source |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
147 |
0 | 148 # local directory or tarball |
13
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
149 if (not os.path.isdir(source)) and tarfile.is_tarfile(source): |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
150 # check for a tarball |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
151 buffer = file(source).read() |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
152 else: |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
153 source_buffer = StringIO() |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
154 source_tar = tarfile.open(mode="w:gz", fileobj=source_buffer) |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
155 source_tar.add(source, arcname=os.path.basename(source)) |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
156 source_tar.close() |
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
157 buffer = source_buffer.getvalue() |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
158 |
0 | 159 # could use git, hg, etc repos. but probably shouldn't |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
160 |
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
161 source_array.append(buffer.encode('zlib').encode('base64')) |
0 | 162 |
163 # tar up virtualenv if not available | |
164 if options.virtualenv: | |
165 if isURL(options.virtualenv): | |
166 globals()['VIRTUAL_ENV'] = urllib2.urlopen(options.virtualenv).read() | |
167 else: | |
168 assert os.path.exists(options.virtualenv) | |
169 if os.path.isdir(options.virtualenv): | |
170 raise NotImplementedError("Hypothetically you should be able to use a local directory or tarball, but I haven't done this yet") | |
171 else: | |
172 # assert a tarfile | |
173 assert tarfile.is_tarfile(options.virtualenv), "%s must be a tar file" % options.virtualenv | |
174 globals()['VIRTUAL_ENV'] = file(options.virtualenv).read() | |
175 else: | |
176 globals()['VIRTUAL_ENV'] = urllib2.urlopen(virtualenv_url).read() | |
177 # TODO: used the below hashed value of VIRTUAL_ENV if set | |
178 # (set that with another file) | |
179 | |
180 # interpolate "template" -> output | |
181 outfile = options.outfile | |
182 if outfile is None: | |
183 outfile = environment + '.py' | |
3
75919adb199a
use compression, but it doesnt seem to help much
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
184 variables = {'VIRTUAL_ENV': VIRTUAL_ENV.encode('zlib').encode('base64'), |
0 | 185 'ENV': environment, |
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
186 'PACKAGE_SOURCES': repr(source_array)} |
0 | 187 f = file(outfile, 'w') |
188 f.write(template % variables) | |
189 f.close() | |
190 try: | |
191 os.chmod(outfile, 0755) | |
192 except: | |
193 # you probably don't have os.chmod | |
194 pass | |
195 | |
196 VIRTUAL_ENV = """""" | |
197 | |
198 if __name__ == '__main__': | |
199 main() |