-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathvenv.py
338 lines (263 loc) · 9.46 KB
/
venv.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import os
import platform
import subprocess
import sys
import sysconfig
import warnings
from contextlib import contextmanager
from subprocess import CalledProcessError
from poetry.config import Config
from poetry.locations import CACHE_DIR
from poetry.utils._compat import Path
from poetry.utils._compat import decode
class VenvError(Exception):
pass
class VenvCommandError(VenvError):
def __init__(self, e): # type: (CalledProcessError) -> None
message = 'Command {} errored with the following output: \n{}'.format(
e.cmd, e.output.decode()
)
super(VenvCommandError, self).__init__(message)
class Venv(object):
def __init__(self, venv=None):
self._venv = venv
if self._venv:
self._venv = Path(self._venv)
self._windows = sys.platform == 'win32'
self._bin_dir = None
if venv:
bin_dir = 'bin' if not self._windows else 'Scripts'
self._bin_dir = self._venv / bin_dir
self._version_info = None
self._python_implementation = None
@classmethod
def create(cls, io, name=None, cwd=None): # type: (...) -> Venv
if 'VIRTUAL_ENV' not in os.environ:
# Not in a virtualenv
# Checking if we need to create one
# First we check if there is a .venv
# at the root of the project.
if cwd and (cwd / '.venv').exists():
venv = cwd / '.venv'
else:
config = Config.create('config.toml')
create_venv = config.setting('settings.virtualenvs.create')
venv_path = config.setting('settings.virtualenvs.path')
if venv_path is None:
venv_path = Path(CACHE_DIR) / 'virtualenvs'
else:
venv_path = Path(venv_path)
if not name:
name = Path.cwd().name
name = '{}-py{}'.format(
name, '.'.join([str(v) for v in sys.version_info[:2]])
)
venv = venv_path / name
if not venv.exists():
if create_venv is False:
io.writeln(
'<fg=black;bg=yellow>'
'Skipping virtualenv creation, '
'as specified in config file.'
'</>'
)
return cls()
io.writeln(
'Creating virtualenv <info>{}</> in {}'.format(
name, str(venv_path)
)
)
cls.build(str(venv))
else:
if io.is_very_verbose():
io.writeln(
'Virtualenv <info>{}</> already exists.'.format(name)
)
os.environ['VIRTUAL_ENV'] = str(venv)
# venv detection:
# stdlib venv may symlink sys.executable, so we can't use realpath.
# but others can symlink *to* the venv Python,
# so we can't just use sys.executable.
# So we just check every item in the symlink tree (generally <= 3)
p = os.path.normcase(sys.executable)
paths = [p]
while os.path.islink(p):
p = os.path.normcase(
os.path.join(os.path.dirname(p), os.readlink(p)))
paths.append(p)
p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
if any(p.startswith(p_venv) for p in paths):
# Running properly in the virtualenv, don't need to do anything
return cls()
venv = os.environ['VIRTUAL_ENV']
return cls(venv)
@classmethod
def build(cls, path):
try:
from venv import EnvBuilder
builder = EnvBuilder(with_pip=True)
build = builder.create
except ImportError:
# We fallback on virtualenv for Python 2.7
from virtualenv import create_environment
build = create_environment
build(path)
@property
def venv(self):
return self._venv
@property
def python(self): # type: () -> str
"""
Path to current python executable
"""
return self._bin('python')
@property
def pip(self): # type: () -> str
"""
Path to current pip executable
"""
return self._bin('pip')
@property
def version_info(self): # type: () -> tuple
if self._version_info is not None:
return self._version_info
if not self.is_venv():
self._version_info = sys.version_info
else:
output = self.run(
'python', '-c',
'"import sys; print(\'.\'.join([str(s) for s in sys.version_info[:3]]))"',
shell=True
)
self._version_info = tuple([
int(s) for s in output.strip().split('.')
])
return self._version_info
@property
def python_implementation(self):
if self._python_implementation is not None:
return self._python_implementation
if not self.is_venv():
impl = platform.python_implementation()
else:
impl = self.run(
'python', '-c',
'"import platform; print(platform.python_implementation())"',
shell=True
).strip()
self._python_implementation = impl
return self._python_implementation
def config_var(self, var):
if not self.is_venv():
try:
return sysconfig.get_config_var(var)
except IOError as e:
warnings.warn("{0}".format(e), RuntimeWarning)
return None
try:
value = self.run(
'python', '-c',
'"import sysconfig; '
'print(sysconfig.get_config_var(\'{}\'))"'.format(var),
shell=True
).strip()
except VenvCommandError as e:
warnings.warn("{0}".format(e), RuntimeWarning)
return None
if value == 'None':
value = None
elif value == '1':
value = 1
elif value == '0':
value = 0
return value
def run(self, bin, *args, **kwargs):
"""
Run a command inside the virtual env.
"""
cmd = [bin] + list(args)
shell = kwargs.get('shell', False)
call = kwargs.pop('call', False)
if shell:
cmd = ' '.join(cmd)
try:
if not self.is_venv():
if call:
return subprocess.call(
cmd, stderr=subprocess.STDOUT,
**kwargs
)
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT,
**kwargs
)
else:
if self._windows:
kwargs['shell'] = True
with self.temp_environ():
os.environ['PATH'] = self._path()
os.environ['VIRTUAL_ENV'] = str(self._venv)
self.unset_env('PYTHONHOME')
self.unset_env('__PYVENV_LAUNCHER__')
if call:
return subprocess.call(
cmd, stderr=subprocess.STDOUT,
**kwargs
)
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT,
**kwargs
)
except CalledProcessError as e:
raise VenvCommandError(e)
return decode(output)
def execute(self, bin, *args, **kwargs):
if not self.is_venv():
return subprocess.call([bin] + list(args))
else:
with self.temp_environ():
os.environ['PATH'] = self._path()
os.environ['VIRTUAL_ENV'] = str(self._venv)
self.unset_env('PYTHONHOME')
self.unset_env('__PYVENV_LAUNCHER__')
return subprocess.call([bin] + list(args), **kwargs)
@contextmanager
def temp_environ(self):
environ = dict(os.environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(environ)
def _path(self):
return os.pathsep.join([
str(self._bin_dir),
os.environ['PATH'],
])
def unset_env(self, key):
if key in os.environ:
del os.environ[key]
def get_shell(self):
shell = Path(os.environ.get('SHELL', '')).stem
if shell in ('bash', 'zsh', 'fish'):
return shell
def _bin(self, bin): # type: (str) -> str
"""
Return path to the given executable.
"""
if not self.is_venv():
return bin
return str(self._bin_dir / bin) + ('.exe' if self._windows else '')
def is_venv(self): # type: () -> bool
return self._venv is not None
class NullVenv(Venv):
def __init__(self, execute=False):
super(NullVenv, self).__init__()
self.executed = []
self._execute = execute
def run(self, bin, *args):
self.executed.append([bin] + list(args))
if self._execute:
return super(NullVenv, self).run(bin, *args)
def _bin(self, bin):
return bin