-
Notifications
You must be signed in to change notification settings - Fork 54
/
virtualenv-burrito.py
executable file
·297 lines (237 loc) · 8.84 KB
/
virtualenv-burrito.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
# encoding: utf-8
#
# virtualenv-burrito.py — manages the Virtualenv Burrito environment
#
__version__ = "2.7.1"
import sys
import os
import csv
import urllib2
import shutil
import glob
import tempfile
import platform
try:
import hashlib
sha1 = hashlib.sha1
except ImportError: # Python < 2.5
import sha
sha1 = sha.new
try:
import subprocess
sh = lambda cmd: subprocess.call(cmd, shell=True)
except ImportError: # Python < 2.4
sh = os.system
NAME = os.path.basename(__file__)
VENVBURRITO = os.path.join(os.environ['HOME'], ".venvburrito")
VENVBURRITO_LIB = os.path.join(VENVBURRITO, "lib")
VERSIONS_URL = "https://raw.githubusercontent.com/brainsik/virtualenv-burrito/master/versions.csv"
def get_python_maj_min_str():
return ".".join(platform.python_version().split(".")[:2])
def get_python_lib_paths():
lib_paths = []
for pydir in glob.glob(os.path.join(VENVBURRITO_LIB, "python*")):
if os.path.exists(os.path.join(pydir, "site-packages")):
pydir = os.path.join(pydir, "site-packages")
lib_paths.append(pydir)
lib_paths.sort()
lib_paths.reverse()
return lib_paths
def get_installed_version(name):
"""Returns current version of `name`."""
versions = []
for pydir in get_python_lib_paths():
for egg_path in glob.glob("%s-*.egg*" % os.path.join(pydir, name)):
egg = os.path.basename(egg_path)
versions.append(map(int, egg.split('-')[1].split('.')))
if versions:
return ".".join(map(str, max(versions)))
def download(url, digest):
"""Returns a filename containing the contents of the URL.
Downloads and checks the SHA1 of the data matches the given hex digest.
"""
name = url.split('/')[-1]
print " Downloading", name, "…"
try:
download_data = urllib2.urlopen(url).read()
except Exception, e:
sys.stderr.write("\nERROR - Unable to download %s: %s %s\n"
% (url, type(e), str(e)))
raise SystemExit(1)
filehash = sha1()
filehash.update(download_data)
if filehash.hexdigest() != digest:
print ("\nThe file %s didn't look like we expected.\n"
"It may have been moved or tampered with. You should tell me:"
" @brainsik." % name)
raise SystemExit(1)
downloaded_file = tempfile.NamedTemporaryFile("wb", delete=False)
downloaded_file.write(download_data)
downloaded_file.close()
return downloaded_file.name
def drop_startup_sh():
# create the startup script
script = """
export WORKON_HOME="$HOME/.virtualenvs"
export PIP_VIRTUALENV_BASE="$WORKON_HOME"
export PIP_RESPECT_VIRTUALENV=true
venvb_py_path="$HOME/.venvburrito/lib/python%s/site-packages"
if [ -z "$PYTHONPATH" ]; then
export PYTHONPATH="$venvb_py_path"
elif ! echo $PYTHONPATH | grep -q "$venvb_py_path"; then
export PYTHONPATH="$venvb_py_path:$PYTHONPATH"
fi
venvb_bin_path="$HOME/.venvburrito/bin"
if ! echo $PATH | grep -q "$venvb_bin_path"; then
export PATH="$venvb_bin_path:$PATH"
fi
. $HOME/.venvburrito/bin/virtualenvwrapper.sh
if ! [ -e $HOME/.venvburrito/.firstrun ]; then
echo
echo "To create a virtualenv, run:"
echo "mkvirtualenv <cool-name>"
touch $HOME/.venvburrito/.firstrun
fi
""" % get_python_maj_min_str()
startup_sh = open(os.path.join(VENVBURRITO, "startup.sh"), 'w')
startup_sh.write(script)
startup_sh.close()
def selfupdate(src):
"""Copy src to our destination and exec the new script."""
dst = os.path.join(VENVBURRITO, "bin", "virtualenv-burrito")
shutil.copyfile(src, dst)
os.remove(src)
os.chmod(dst, 0755)
print " Restarting!\n"
sys.stdout.flush()
os.execl(dst, "virtualenv-burrito", "upgrade", "selfupdated")
def _getcwd():
try:
return os.getcwd()
except OSError:
return None
def upgrade_package(filename, name, version):
"""Install Python package in tarball `filename`."""
pyver = "python%s" % get_python_maj_min_str()
lib_python = os.path.join(VENVBURRITO_LIB, pyver, "site-packages")
pythonpath = ''
for pydir in reversed(get_python_lib_paths()):
pythonpath += "%s:" % pydir
os.environ['PYTHONPATH'] = pythonpath.rstrip(":")
realname = "%s-%s" % (name, version)
print " Installing", realname
owd = _getcwd()
tmp = tempfile.mkdtemp(prefix='venvburrito.')
try:
# unpack the zip or tarball
if name == 'setuptools':
sh("unzip %s -d %s" % (filename, tmp))
else:
sh("tar xfz %s -C %s" % (filename, tmp))
os.chdir(os.path.join(tmp, realname))
if name in ['setuptools', 'distribute']:
# build and install the egg to avoid patching the system
sh("%s setup.py bdist_egg" % sys.executable)
egg = glob.glob(os.path.join(os.getcwd(), "dist", "*egg"))[0]
sh("%s setup.py easy_install --exclude-scripts --install-dir %s %s >/dev/null"
% (sys.executable, lib_python, egg))
elif name == 'pip':
libexec = os.path.join(VENVBURRITO, "libexec")
sh("%s setup.py install --prefix='' --home='%s' --install-lib %s --install-scripts %s --no-compile >/dev/null"
% (sys.executable, VENVBURRITO, lib_python, libexec))
else:
pip = os.path.join(VENVBURRITO, "libexec", "pip")
sh("%s install --ignore-installed --install-option='--prefix=%s' ." % (pip, VENVBURRITO))
finally:
os.chdir(owd or VENVBURRITO)
shutil.rmtree(tmp)
def check_versions(selfcheck=True):
"""Return packages which can be upgraded."""
try:
fp = urllib2.urlopen(VERSIONS_URL)
except Exception, e:
sys.stderr.write("\nERROR - Couldn't open versions file at %s: %s %s\n"
% (VERSIONS_URL, type(e), str(e)))
raise SystemExit(1)
reader = csv.reader(fp)
has_update = []
for name, version, url, digest in reader:
if name == '_virtualenv-burrito':
if not selfcheck:
continue
name = NAME
current = __version__
else:
current = get_installed_version(name)
if not current or version != current:
print "+ %s will upgrade (%s -> %s)" % (name, current, version)
has_update.append((name, version, url, digest))
if name == NAME:
break
return has_update
def handle_upgrade(selfupdated=False, firstrun=False):
"""Handles the upgrade command."""
if os.path.exists(VENVBURRITO_LIB):
if not os.path.exists(os.path.join(VENVBURRITO, "libexec")):
print "! Removing burrito < 2.7 setup and preparing fresh wrap"
# nuke old lib and get pip out of the bin PATH
shutil.rmtree(VENVBURRITO_LIB)
for pip in glob.glob(os.path.join(VENVBURRITO, "bin", "pip*")):
os.remove(pip)
# create versioned python site-packages dir
pyver = "python%s" % get_python_maj_min_str()
os.mkdir(VENVBURRITO_LIB)
os.mkdir(os.path.join(VENVBURRITO_LIB, pyver))
os.mkdir(os.path.join(VENVBURRITO_LIB, pyver, "site-packages"))
has_update = check_versions(selfupdated is False)
# update other packages
for update in has_update:
name, version, url, digest = update
filename = download(url, digest)
try:
if name == NAME:
print "* Upgrading ourself …"
selfupdate(filename) # calls os.exec
else:
print "* Upgrading %s …" % name
upgrade_package(filename, name, version)
finally:
if filename and os.path.exists(filename):
os.remove(filename)
# startup.sh needs to be created after selfupdate AND on install
if selfupdated or firstrun:
drop_startup_sh()
if selfupdated:
print "\nTo finish the upgrade, run this:"
print "source %s/startup.sh" % VENVBURRITO
elif not has_update:
print "Everything is up to date."
return
else:
print "\nFin."
def usage(returncode=1):
print "Use like this:\n\t%s upgrade" % NAME
raise SystemExit(returncode)
def main(argv):
if len(argv) < 2:
usage()
if argv[1] in ['help', '--help', '-h', '-?']:
usage(returncode=0)
if argv[1] in ['version', '--version', '-V']:
print "virtualenv-burrito %s from %s" % (__version__, __file__)
raise SystemExit(0)
if argv[1] in ['upgrade', 'update']:
if len(argv) > 2:
if argv[2] in ['selfupdated', 'no-selfcheck']:
handle_upgrade(selfupdated=True)
elif argv[2] == 'firstrun':
handle_upgrade(firstrun=True)
else:
usage()
else:
handle_upgrade()
else:
usage()
if __name__ == '__main__':
main(sys.argv)