annotate carton.py @ 15:f05e636b7444

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