/
pypyenv.py
127 lines (106 loc) · 3.47 KB
/
pypyenv.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
import os
import sys
import urllib2
import tarfile
import shutil
import stat
import platform
__version__ = "0.1.4"
base = sys.prefix
srcdir = "src"
bindir = "bin"
binpypy = os.path.join(bindir, "pypy")
pypydir = "pypy"
version = sys.version[:3]
yes = ("y", "yes")
def out(s):
print " * " + s
def install(download):
os.chdir(base)
# Clean up if anything was left over
uninstall()
# Create src directory
if not os.path.exists(srcdir):
out("creating ENV/%s directory" % srcdir)
os.mkdir(srcdir)
archive = os.path.join(srcdir, download.split("/")[-1])
# Download archive
if os.path.exists(archive):
out("using previously downloaded ENV/%s" % archive)
else:
out("downloading PyPy")
urlfile = urllib2.urlopen(download)
archivefile = open(archive, "w")
while True:
data = urlfile.read(4096)
if not data:
break
archivefile.write(data)
archivefile.close()
# Unpack archive
archiveobj = tarfile.open(archive, mode="r:bz2")
unpacked = os.path.join(srcdir, archiveobj.next().name)
if os.path.exists(unpacked):
out("deleting ENV/%s" % unpacked)
shutil.rmtree(unpacked)
out("unpacking archive")
archiveobj.extractall(srcdir)
# Copying PyPy directory into virtualenv root
out("copying PyPy directory into root")
shutil.copytree(unpacked, pypydir)
# Install PyPy executable symlink
out("installing ENV/%s symlink" % binpypy)
os.symlink(os.path.join("..", "pypy", "bin", "pypy"), binpypy)
out("giving it execute permissions")
os.chmod(binpypy, stat.S_IXUSR)
# Install site-packages symlink
pythonsite = os.path.join("..", "lib", "python" + version, "site-packages")
pypysite = os.path.join("pypy", "site-packages")
out("installing ENV/%s symlink" % pypysite)
shutil.rmtree(pypysite)
os.symlink(pythonsite, pypysite)
def uninstall():
os.chdir(base)
if os.path.exists(binpypy):
if confirm_deletion(binpypy):
os.remove(binpypy)
else:
sys.exit(1)
if os.path.exists(pypydir):
if confirm_deletion(pypydir):
shutil.rmtree(pypydir)
else:
sys.exit(1)
def confirm_deletion(tree):
return raw_input("Delete ENV/%s? [y/N] " % tree) in yes
help_message = """ pypyenv install - installs PyPy in this virtualenv
pypyenv uninstall - uninstalls PyPy from this virtualenv"""
def main():
args = sys.argv[1:]
if version != "2.7":
if not raw_input("PyPy implements Python 2.7, you are using a "
"different version, continue? [y/N] ") in yes:
sys.exit(1)
if len(args) != 1:
print help_message
sys.exit(2)
osx = platform.system() == "Darwin"
win = platform.system() == "Windows"
linux = not osx and not win
x86_64 = platform.machine() == "x86_64"
x86 = not x86_64
if win:
print "Supports only Linux and OS X at this point"
sys.exit(2)
if linux and x86:
download = "https://bitbucket.org/pypy/pypy/downloads/pypy-1.9-linux.tar.bz2"
elif linux and x86_64:
download = "https://bitbucket.org/pypy/pypy/downloads/pypy-1.9-linux64.tar.bz2"
elif osx:
download = "https://bitbucket.org/pypy/pypy/downloads/pypy-1.9-osx64.tar.bz2"
if args[0] == "install":
install(download)
elif args[0] == "uninstall":
uninstall()
else:
print help_message