Mercurial > hg > carton
annotate carton.py @ 38:99c610473c17
if develop fails, go with install
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon, 09 Apr 2012 13:44:16 -0700 |
| parents | 33e5d444ff30 |
| children | d9dcc5a1503b |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 2 | |
| 3 """ | |
| 29 | 4 make a self-extracting virtualenv from directories or URLs of packages |
| 5 | |
| 6 To package up all files in a virtualenvs source directory (e.g.):: | |
| 0 | 7 |
|
38
99c610473c17
if develop fails, go with install
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
8 python path/to/carton.py myproject project/src/* |
| 29 | 9 |
| 10 This will create a self-extracting file, `myproject.py`, that will unfold | |
| 11 a virtualenv with the specified packages setup for development | |
| 0 | 12 |
| 29 | 13 The sources may be directories, local or HTTP-accessible tarballs, or ordinary |
| 14 files. The `setup.py`s found in the `src` directory after extraction will be | |
| 15 run (via `python setup.py develop`) in the order they are provided. This makes | |
| 16 it possible to have completely local dependencies (without touching the net) | |
| 17 by correctly specifying the source order. If a `setup.py` is overwritten from | |
| 18 a later source, it will not be rerun (known limitation). | |
| 0 | 19 |
| 29 | 20 The extracted virtualenv will be created in the current directory and will have |
| 21 the same name as provided initially (e.g. `myproject`) unless `--env` is | |
| 22 specified. | |
| 23 | |
| 24 Normally, the entire contents of source directories are compressed and | |
| 25 packaged as-is. When running with the `--package` flag, a source tarball is | |
| 26 produced via `python setup.py sdist` if the directory contains a top-level | |
| 27 `setup.py`. | |
| 28 | |
| 29 Since directories are compressed as-is, portable file-based VCS repositories, | |
| 30 such a mercurial and git, may be cartoned this way (though note that newer | |
| 31 repositories may not be backwards-compatible with older clients). | |
| 0 | 32 """ |
| 33 | |
| 34 # imports | |
| 37 | 35 import optparse |
| 0 | 36 import os |
| 37 import sys | |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
38 import subprocess |
| 0 | 39 import tarfile |
| 40 import tempfile | |
| 41 import urllib2 | |
| 42 from StringIO import StringIO | |
| 43 | |
| 44 # global variables | |
| 45 usage = "%prog [options] environment_name directory|url [...]" | |
| 46 virtualenv_url = 'http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.6.1.tar.gz' | |
| 47 template = """#!/usr/bin/env python | |
| 48 | |
|
15
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
49 "create a virtualenv at %(ENV)s" |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
50 |
| 0 | 51 import os |
| 52 import shutil | |
| 53 import subprocess | |
| 54 import sys | |
| 55 import tarfile | |
| 56 import tempfile | |
| 57 from optparse import OptionParser | |
| 58 from StringIO import StringIO | |
| 59 | |
| 60 try: | |
| 61 call = subprocess.check_call | |
| 62 except AttributeError: | |
| 63 # old python; boo :( | |
| 64 call = subprocess.call | |
| 65 | |
| 66 # virtualenv name | |
| 67 ENV='''%(ENV)s''' | |
| 68 | |
| 69 # packed files | |
|
3
75919adb199a
use compression, but it doesnt seem to help much
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
70 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
|
71 PACKAGE_SOURCES=%(PACKAGE_SOURCES)s |
|
23
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
72 CARTON=%(CARTON)s |
| 0 | 73 |
|
35
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
74 # post-install scripts |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
75 PYTHON_SCRIPTS=%(PYTHON_SCRIPTS)s |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
76 |
|
15
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
77 # parse options |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
78 usage = os.path.basename(sys.argv[0]) + ' [options]' |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
79 parser = OptionParser(usage=usage, description=__doc__) |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
80 parser.add_option('--env', dest='env', help="environment name [DEFAULT: " + ENV + "]") |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
81 options, args = parser.parse_args() |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
82 if options.env: |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
83 ENV = options.env |
|
f05e636b7444
now you can specify the name of the env in the created script
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
84 |
| 0 | 85 # unpack virtualenv |
| 86 tempdir = tempfile.mkdtemp() | |
| 87 buffer = StringIO() | |
| 88 buffer.write(VIRTUAL_ENV) | |
| 89 buffer.seek(0) | |
| 90 tf = tarfile.open(mode='r', fileobj=buffer) | |
| 91 tf.extractall(tempdir) | |
| 92 | |
| 93 # find the virtualenv | |
| 94 for root, dirs, files in os.walk(tempdir): | |
| 95 if 'virtualenv.py' in files: | |
| 96 virtualenv = os.path.join(root, 'virtualenv.py') | |
| 97 break | |
| 98 else: | |
| 99 raise Exception("virtualenv.py not found in " + tempdir) | |
| 36 | 100 |
| 0 | 101 # create the virtualenv |
|
17
b05f5f1ec26e
pop PYTHONHOME as this will fuxor things
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
102 os.environ.pop('PYTHONHOME', None) |
| 0 | 103 call([sys.executable, virtualenv, ENV]) |
| 104 | |
| 105 # find the bin/scripts directory | |
| 106 for i in ('bin', 'Scripts'): | |
| 107 scripts_dir = os.path.abspath(os.path.join(ENV, i)) | |
| 108 if os.path.exists(scripts_dir): | |
| 109 break | |
| 110 else: | |
| 111 raise Exception("Scripts directory not found in " + ENV) | |
| 112 | |
| 113 # find the virtualenv's python | |
| 114 for i in ('python', 'python.exe'): | |
| 115 python = os.path.join(scripts_dir, i) | |
| 116 if os.path.exists(python): | |
| 117 break | |
| 118 else: | |
| 119 raise Exception("python not found in " + scripts_dir) | |
| 120 | |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
121 # 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
|
122 srcdir = os.path.join(ENV, 'src') |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
123 os.mkdir(srcdir) |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
124 setup_pys = set() |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
125 for source in PACKAGE_SOURCES: |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
126 source = source.decode('base64').decode('zlib') |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
127 buffer = StringIO() |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
128 buffer.write(source) |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
129 buffer.seek(0) |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
130 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
|
131 tf.extractall(srcdir) |
| 0 | 132 |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
133 # setup sources for development if there are any new setup.py files |
| 16 | 134 # TODO: ideally this would figure out dependency order for you |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
135 for i in os.listdir(srcdir): |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
136 if i in setup_pys: |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
137 continue |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
138 subdir = os.path.join(srcdir, i) |
|
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
139 if os.path.exists(os.path.join(srcdir, i, 'setup.py')): |
|
38
99c610473c17
if develop fails, go with install
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
140 try: |
|
99c610473c17
if develop fails, go with install
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
141 call([python, 'setup.py', 'develop'], cwd=subdir) |
|
99c610473c17
if develop fails, go with install
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
142 except: |
|
99c610473c17
if develop fails, go with install
Jeff Hammel <jhammel@mozilla.com>
parents:
37
diff
changeset
|
143 call([python, 'setup.py', 'install'], cwd=subdir) |
| 29 | 144 # TODO: try `setup.py install` if develop fails for distutils packages |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
145 setup_pys.add(i) |
| 0 | 146 |
|
22
9c710f06e51d
add virtualenv to the virtualenv(!)
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
147 # add virtualenv to the virtualenv (!) |
|
9c710f06e51d
add virtualenv to the virtualenv(!)
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
148 virtualenv_dir = os.path.dirname(virtualenv) |
|
9c710f06e51d
add virtualenv to the virtualenv(!)
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
149 if os.path.exists(os.path.join(virtualenv_dir, 'setup.py')): |
|
23
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
150 call([python, 'setup.py', 'install'], cwd=virtualenv_dir, stdout=subprocess.PIPE) |
|
22
9c710f06e51d
add virtualenv to the virtualenv(!)
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
151 |
|
27
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
152 # add carton to the virtualenv (!) |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
153 if CARTON: |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
154 CARTON = CARTON.decode('base64').decode('zlib') |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
155 carton_filename = os.path.join(scripts_dir, 'carton.py') |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
156 f = file(carton_filename, 'w') |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
157 f.write(CARTON) |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
158 f.close() |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
159 try: |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
160 os.chmod(carton_filename, 0755) |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
161 except: |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
162 # you probably don't have os.chmod |
|
dace84448c25
carry carton along with carton
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
163 pass |
|
23
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
164 |
|
31
8fef97a25925
near version bump and documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
165 # cleanup virtualenv tempdir |
|
8fef97a25925
near version bump and documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
30
diff
changeset
|
166 shutil.rmtree(tempdir) |
|
35
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
167 |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
168 # run post-install scripts |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
169 for script in PYTHON_SCRIPTS: |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
170 if not os.path.isabs(script): |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
171 script = os.path.join(os.path.abspath(ENV), script) |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
172 call([python, script]) |
| 0 | 173 """ |
| 174 | |
| 175 def isURL(path): | |
| 176 return path.startswith('http://') or path.startswith('https://') | |
| 177 | |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
178 try: |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
179 call = subprocess.check_call |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
180 except AttributeError: |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
181 # old python; boo :( |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
182 call = subprocess.call |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
183 |
| 0 | 184 def main(args=sys.argv[1:]): |
| 185 | |
| 186 # parse CLI arguments | |
| 36 | 187 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
| 188 """description formatter for console script entry point""" | |
| 189 def format_description(self, description): | |
| 190 if description: | |
| 191 return description.strip() + '\n' | |
| 192 else: | |
| 193 return '' | |
| 37 | 194 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
| 0 | 195 parser.add_option('-o', dest='outfile', |
| 196 help="specify outfile; otherwise it will come from environment_name") | |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
197 parser.add_option('-p', '--package', dest='package', |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
198 action='store_true', default=False, |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
199 help="create python packages from sources; do not take entire subdirectory") |
|
35
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
200 parser.add_option('--python-script', dest='python_scripts', default=[], |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
201 action='append', |
|
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
202 help="post-uncartoning python scripts to run in the virtualenv; these should be relative to $VIRTUAL_ENV") |
| 0 | 203 parser.add_option('--virtualenv', dest='virtualenv', |
|
12
542b46ac4e28
fix description more better
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
204 help="use this virtualenv URL or file tarball") |
| 0 | 205 options, args = parser.parse_args(args) |
| 206 if len(args) < 2: | |
| 207 parser.print_usage() | |
| 208 parser.exit() | |
| 209 environment = args[0] | |
| 210 if environment.endswith('.py'): | |
| 211 # stop on .py; will add it in later | |
| 212 environment = environment[:-3] | |
| 213 sources = args[1:] | |
| 214 | |
| 215 # tar up the sources | |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
216 source_array = [] |
| 0 | 217 for source in sources: |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
218 buffer = None |
| 0 | 219 |
| 220 if isURL(source): | |
|
14
c474362cf69a
allow fetching of remote tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
221 # remote tarball or resource |
|
c474362cf69a
allow fetching of remote tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
13
diff
changeset
|
222 buffer = urllib2.urlopen(source).read() |
| 0 | 223 else: |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
224 # local directory or tarball |
|
13
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
225 assert os.path.exists(source), "%s does not exist" % source |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
226 |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
227 # package up the source if applicable |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
228 if options.package and os.path.exists(os.path.join(source, 'setup.py')): |
| 24 | 229 |
| 230 # create a .tar.gz package | |
| 231 call([sys.executable, 'setup.py', 'sdist'], cwd=source, stdout=subprocess.PIPE) | |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
232 dist_dir = os.path.join(source, 'dist') |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
233 assert os.path.isdir(dist_dir), "dist directory not created in %s" % source |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
234 tarfiles = [i for i in os.listdir(dist_dir) |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
235 if i.endswith('.tar.gz')] |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
236 assert tarfiles, "no .tar.gz files found in %s" % dist_dir |
| 24 | 237 |
| 238 # use the last modified tarball | |
|
18
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
239 def last_modified(filename): |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
240 return os.path.getmtime(os.path.join(dist_dir, filename)) |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
241 tarfiles.sort(key=last_modified) |
|
c6a03199d4bf
stub out package creation; next: to test this
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
242 source = os.path.join(dist_dir, tarfiles[-1]) |
| 36 | 243 |
|
13
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
244 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
|
245 # check for a tarball |
|
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
246 buffer = file(source).read() |
|
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
247 else: |
| 24 | 248 # add other sources (files and directories) to the archive |
|
13
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
249 source_buffer = StringIO() |
|
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
250 source_tar = tarfile.open(mode="w:gz", fileobj=source_buffer) |
|
26
e6ee0410ceef
fix bug where os.path.sep is wrongly interpreted
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
251 source_tar.add(source, arcname=os.path.basename(source.rstrip(os.path.sep))) |
|
13
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
252 source_tar.close() |
|
f522620c6a78
now works properly with tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
253 buffer = source_buffer.getvalue() |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
254 |
| 0 | 255 # 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
|
256 source_array.append(buffer.encode('zlib').encode('base64')) |
| 0 | 257 |
| 258 # tar up virtualenv if not available | |
| 259 if options.virtualenv: | |
| 260 if isURL(options.virtualenv): | |
| 261 globals()['VIRTUAL_ENV'] = urllib2.urlopen(options.virtualenv).read() | |
| 262 else: | |
| 263 assert os.path.exists(options.virtualenv) | |
| 264 if os.path.isdir(options.virtualenv): | |
| 265 raise NotImplementedError("Hypothetically you should be able to use a local directory or tarball, but I haven't done this yet") | |
| 266 else: | |
| 267 # assert a tarfile | |
| 268 assert tarfile.is_tarfile(options.virtualenv), "%s must be a tar file" % options.virtualenv | |
| 269 globals()['VIRTUAL_ENV'] = file(options.virtualenv).read() | |
| 270 else: | |
| 271 globals()['VIRTUAL_ENV'] = urllib2.urlopen(virtualenv_url).read() | |
| 272 # TODO: used the below hashed value of VIRTUAL_ENV if set | |
| 273 # (set that with another file) | |
| 274 | |
|
23
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
275 # get the contents of this file |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
276 carton = None |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
277 try: |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
278 if __file__: |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
279 filename = __file__.rstrip('c') # avoid pyfiles |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
280 if os.path.exists(filename): |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
281 carton = file(filename).read().encode('zlib').encode('base64') |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
282 except NameError: |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
283 pass |
|
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
284 |
| 0 | 285 # interpolate "template" -> output |
| 30 | 286 # TODO: add the ability to include a post-deployment script |
| 0 | 287 outfile = options.outfile |
| 288 if outfile is None: | |
| 289 outfile = environment + '.py' | |
|
3
75919adb199a
use compression, but it doesnt seem to help much
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
290 variables = {'VIRTUAL_ENV': VIRTUAL_ENV.encode('zlib').encode('base64'), |
| 0 | 291 'ENV': environment, |
|
23
987086aad234
stub adding carton to to virtualenv
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
292 'CARTON': repr(carton), |
|
35
122c56779f2b
add functionality to run post-install python scripts
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
293 'PYTHON_SCRIPTS': repr(options.python_scripts), |
|
11
e6a62ba0c24d
now respect the order sources are installed in
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
294 'PACKAGE_SOURCES': repr(source_array)} |
| 0 | 295 f = file(outfile, 'w') |
| 296 f.write(template % variables) | |
| 297 f.close() | |
| 298 try: | |
| 299 os.chmod(outfile, 0755) | |
| 300 except: | |
| 301 # you probably don't have os.chmod | |
| 302 pass | |
| 303 | |
| 304 VIRTUAL_ENV = """""" | |
| 305 | |
| 306 if __name__ == '__main__': | |
| 307 main() |
