forked from brainsik/virtualenv-burrito
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualenv-burrito.py
More file actions
283 lines (230 loc) · 7.96 KB
/
virtualenv-burrito.py
File metadata and controls
283 lines (230 loc) · 7.96 KB
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
#!/usr/bin/env python
# encoding: utf-8
#
# virtualenv-burrito.py — manages the Virtualenv Burrito environment
#
__version__ = "2.0.5"
import sys
import os
import csv
try:
from urllib import urlretrieve
except ImportError: # Python >= 3
from urllib.request import urlretrieve
try:
from urllib2 import urlopen
except ImportError: # Python >= 3
from urllib.request import urlopen
import shutil
import glob
import tempfile
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
try:
from StringIO import StringIO
except ImportError: # Python >= 3
from io import StringIO
NAME = os.path.basename(__file__)
VENVBURRITO = os.path.join(os.environ['HOME'], ".venvburrito")
VENVBURRITO_LIB = os.path.join(VENVBURRITO, "lib")
VERSIONS_URL = "https://raw.github.com/brainsik/virtualenv-burrito/master/versions.csv"
def get_installed_version(name):
"""Returns current version of `name`."""
pkg = os.path.join(VENVBURRITO_LIB, "python", name)
versions = []
for egg in glob.glob("%s-*.egg" % pkg):
versions.append(tuple(int(v) for v in 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:
filename = urlretrieve(url)[0]
except Exception as e:
sys.stderr.write("\nERROR - Unable to download %s: %s %s\n"
% (url, type(e), str(e)))
raise SystemExit(1)
filehash = sha1()
f = open(filename, 'rb')
filehash.update(f.read())
f.close()
if filehash.hexdigest() == digest:
return filename
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)
try:
os.remove(filename)
except OSError:
pass
raise SystemExit(1)
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"
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
"""
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, int('0755', 8))
print(" Restarting!\n")
sys.stdout.flush()
os.execl(dst, "virtualenv-burrito", "upgrade", "selfupdated")
def fix_bin_virtualenv():
"""Untie the virtualenv script from a specific version of Python"""
bin_virtualenv = os.path.join(VENVBURRITO, "bin", "virtualenv")
fi = open(bin_virtualenv, 'r')
fi.readline() # skip the hash bang
fo = open(bin_virtualenv, 'w')
fo.write("#!/usr/bin/env python\n")
fo.write(fi.read())
fi.close()
fo.close()
def upgrade_package(filename, name, version):
"""Install Python package in tarball `filename`."""
try:
owd = os.getcwd()
except OSError:
owd = None
realname = "%s-%s" % (name, version)
print(" Installing", realname)
os.environ['PYTHONPATH'] = os.path.join(VENVBURRITO_LIB, "python")
tmp = tempfile.mkdtemp(prefix='venvburrito.')
try:
os.chdir(tmp)
sh("tar xfz %s" % filename)
os.chdir(os.path.join(tmp, realname))
if name == '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, os.path.join(VENVBURRITO_LIB, "python"), egg))
else:
sh("%s setup.py install --home %s --install-scripts %s --no-compile >/dev/null"
% (sys.executable, VENVBURRITO, os.path.join(VENVBURRITO, "bin")))
if name in ['virtualenv', 'virtualenvwrapper']:
fix_bin_virtualenv()
finally:
os.chdir(owd or VENVBURRITO)
shutil.rmtree(tmp)
def check_versions(selfcheck=True):
"""Return packages which can be upgraded."""
try:
fp = urlopen(VERSIONS_URL)
except Exception as e:
sys.stderr.write("\nERROR - Couldn't open versions file at %s: %s %s\n"
% (VERSIONS_URL, type(e), str(e)))
raise SystemExit(1)
ff = StringIO()
ff.write(fp.read().decode('utf8'))
ff.seek(0)
reader = csv.reader(ff)
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.isdir(os.path.join(VENVBURRITO_LIB, "python")):
print("! Removing old v1 packages and doing fresh v2 install")
shutil.rmtree(VENVBURRITO_LIB)
os.mkdir(VENVBURRITO_LIB)
os.mkdir(os.path.join(VENVBURRITO_LIB, "python"))
has_update = check_versions(selfupdated == 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 ['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)