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
|
import sys, os
import py
import tox
from tox._config import DepConfig
class CreationConfig:
def __init__(self, md5, python, version, distribute, sitepackages, deps):
self.md5 = md5
self.python = python
self.version = version
self.distribute = distribute
self.sitepackages = sitepackages
self.deps = deps
def writeconfig(self, path):
lines = ["%s %s" % (self.md5, self.python)]
lines.append("%s %d %d" % (self.version, self.distribute,
self.sitepackages))
for dep in self.deps:
lines.append("%s %s" % dep)
path.write("\n".join(lines))
@classmethod
def readconfig(cls, path):
try:
lines = path.readlines(cr=0)
value = lines.pop(0).split(None, 1)
md5, python = value
version, distribute, sitepackages = lines.pop(0).split(None, 2)
distribute = bool(int(distribute))
sitepackages = bool(int(sitepackages))
deps = []
for line in lines:
md5, depstring = line.split(None, 1)
deps.append((md5, depstring))
return CreationConfig(md5, python, version,
distribute, sitepackages, deps)
except KeyboardInterrupt:
raise
except:
return None
def matches(self, other):
return (other and self.md5 == other.md5
and self.python == other.python
and self.version == other.version
and self.distribute == other.distribute
and self.sitepackages == other.sitepackages
and self.deps == other.deps)
class VirtualEnv(object):
def __init__(self, envconfig=None, session=None):
self.envconfig = envconfig
self.session = session
self.path = envconfig.envdir
self.path_config = self.path.join(".tox-config1")
def __repr__(self):
return "<VirtualEnv at %r>" %(self.path)
def getcommandpath(self, name=None):
if name is None:
return self.envconfig.envpython
if os.path.isabs(name):
return name
p = py.path.local.sysfind(name)
if p is None:
raise tox.exception.InvocationError("could not find executable %r"
% (name,))
if p.relto(self.envconfig.envdir):
return p
return str(p) # will not be rewritten for reporting
def _ispython3(self):
return "python3" in str(self.envconfig.basepython)
def update(self):
""" return status string for updating actual venv to match configuration.
if status string is empty, all is ok.
"""
report = self.session.report
name = self.envconfig.envname
rconfig = CreationConfig.readconfig(self.path_config)
if not self.envconfig.recreate and rconfig and \
rconfig.matches(self._getliveconfig()):
report.action("reusing existing matching virtualenv %s" %
(self.envconfig.envname,))
return
if rconfig is None:
report.action("creating virtualenv %s" % name)
else:
report.action("recreating virtualenv %s "
"(configchange/incomplete install detected)" % name)
try:
self.create()
except tox.exception.UnsupportedInterpreter:
return sys.exc_info()[1]
except tox.exception.InterpreterNotFound:
return sys.exc_info()[1]
try:
self.install_deps()
except tox.exception.InvocationError:
v = sys.exc_info()[1]
return "could not install deps %s" %(self.envconfig.deps,)
def _getliveconfig(self):
python = self.getconfigexecutable()
md5 = getdigest(python)
version = tox.__version__
distribute = self.envconfig.distribute
sitepackages = self.envconfig.sitepackages
deps = []
for dep in self._getresolvedeps():
raw_dep = dep.name
md5 = getdigest(raw_dep)
deps.append((md5, raw_dep))
return CreationConfig(md5, python, version,
distribute, sitepackages, deps)
def _getresolvedeps(self):
l = []
for dep in self.envconfig.deps:
if dep.indexserver.url is None:
res = self.session._resolve_pkg(dep.name)
if res != dep.name:
dep = dep.__class__(res)
l.append(dep)
return l
def getconfigexecutable(self):
python = self.envconfig.basepython
if not python:
python = sys.executable
x = find_executable(str(python))
if x:
x = x.realpath()
return x
def getsupportedinterpreter(self):
if sys.platform == "win32" and self.envconfig.basepython and \
"jython" in self.envconfig.basepython:
raise tox.exception.UnsupportedInterpreter(
"Jython/Windows does not support installing scripts")
config_executable = self.getconfigexecutable()
if not config_executable:
raise tox.exception.InterpreterNotFound(self.envconfig.basepython)
return config_executable
def create(self):
#if self.getcommandpath("activate").dirpath().check():
# return
config_interpreter = self.getsupportedinterpreter()
args = ['virtualenv']
if not self._ispython3() and self.envconfig.distribute:
args.append('--distribute')
if not self.envconfig.sitepackages:
args.append('--no-site-packages')
if sys.platform == "win32":
f, path, _ = py.std.imp.find_module("virtualenv")
f.close()
args[:1] = [str(config_interpreter), str(path)]
else:
args.extend(["-p", str(config_interpreter)])
self.session.make_emptydir(self.path)
basepath = self.path.dirpath()
basepath.ensure(dir=1)
old = py.path.local()
try:
basepath.chdir()
args.append(self.path.basename)
self._pcall(args, venv=False)
#if self._ispython3():
# self.easy_install(["-U", "distribute"])
finally:
old.chdir()
self._getliveconfig().writeconfig(self.path_config)
def install_sdist(self, sdistpath):
#self._install(['-U', sdistpath])
self._install([sdistpath])
def install_deps(self):
deps = self._getresolvedeps()
if deps:
depinfo = ", ".join(map(str, deps))
self.session.report.action("installing dependencies: %s" % depinfo)
self._install(deps)
def _commoninstallopts(self, indexserver):
l = []
if indexserver:
l += ["-i", indexserver]
return l
def easy_install(self, args, indexserver=None):
argv = ["easy_install"] + self._commoninstallopts(indexserver) + args
self._pcall(argv, cwd=self.envconfig.envlogdir)
def pip_install(self, args, indexserver=None):
argv = ["pip", "install"] + self._commoninstallopts(indexserver)
if self.envconfig.downloadcache:
self.envconfig.downloadcache.ensure(dir=1)
argv.append("--download-cache=%s" %
self.envconfig.downloadcache)
try:
del os.environ['PIP_RESPECT_VIRTUALENV']
except KeyError:
pass
argv += args
self._pcall(argv, cwd=self.envconfig.envlogdir)
def _install(self, deps):
if not deps:
return
d = {}
l = []
for dep in deps:
if not hasattr(dep, 'indexserver'):
iserver = self.envconfig.config.indexserver['default']
dep = DepConfig(dep, iserver)
url = dep.indexserver.url
d.setdefault(url, []).append(dep.name)
if url not in l:
l.append(url)
for repo in l:
args = d[repo]
self.pip_install(args, repo)
def test(self):
self.session.make_emptydir(self.envconfig.envtmpdir)
cwd = self.envconfig.changedir
for argv in self.envconfig.commands:
try:
self._pcall(argv, log=-1, cwd=cwd)
except tox.exception.InvocationError:
self.session.report.error(str(sys.exc_info()[1]))
return True
def _pcall(self, args, venv=True, log=None, cwd=None):
try:
del os.environ['PYTHONDONTWRITEBYTECODE']
except KeyError:
pass
old = self.patchPATH()
try:
if venv:
args = [self.getcommandpath(args[0])] + args[1:]
if log is None:
log = self.path.ensure("log", dir=1)
return self.session.pcall(args, log=log, cwd=cwd)
finally:
os.environ['PATH'] = old
def patchPATH(self):
oldPATH = os.environ['PATH']
bindir = str(self.envconfig.envbindir)
os.environ['PATH'] = os.pathsep.join([bindir, oldPATH])
return oldPATH
def getdigest(path):
path = py.path.local(path)
if not path.check(file=1):
return "0" * 32
return path.computehash()
if sys.platform != "win32":
def find_executable(name):
return py.path.local.sysfind(name)
else:
win32map = {
'python': sys.executable,
'python2.4': "c:\python24\python.exe",
'python2.5': "c:\python25\python.exe",
'python2.6': "c:\python26\python.exe",
'python2.7': "c:\python27\python.exe",
'python3.1': "c:\python31\python.exe",
'python3.2': "c:\python32\python.exe",
'jython': "c:\jython2.5.1\jython.bat",
}
def find_executable(name):
p = py.path.local(name)
if p.check(file=1):
return p
actual = win32map.get(name, None)
if actual:
actual = py.path.local(actual)
if actual.check():
return actual
|