1 | n/a | """ |
---|
2 | n/a | Virtual environment (venv) package for Python. Based on PEP 405. |
---|
3 | n/a | |
---|
4 | n/a | Copyright (C) 2011-2014 Vinay Sajip. |
---|
5 | n/a | Licensed to the PSF under a contributor agreement. |
---|
6 | n/a | """ |
---|
7 | n/a | import logging |
---|
8 | n/a | import os |
---|
9 | n/a | import shutil |
---|
10 | n/a | import subprocess |
---|
11 | n/a | import sys |
---|
12 | n/a | import types |
---|
13 | n/a | |
---|
14 | n/a | logger = logging.getLogger(__name__) |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class EnvBuilder: |
---|
18 | n/a | """ |
---|
19 | n/a | This class exists to allow virtual environment creation to be |
---|
20 | n/a | customized. The constructor parameters determine the builder's |
---|
21 | n/a | behaviour when called upon to create a virtual environment. |
---|
22 | n/a | |
---|
23 | n/a | By default, the builder makes the system (global) site-packages dir |
---|
24 | n/a | *un*available to the created environment. |
---|
25 | n/a | |
---|
26 | n/a | If invoked using the Python -m option, the default is to use copying |
---|
27 | n/a | on Windows platforms but symlinks elsewhere. If instantiated some |
---|
28 | n/a | other way, the default is to *not* use symlinks. |
---|
29 | n/a | |
---|
30 | n/a | :param system_site_packages: If True, the system (global) site-packages |
---|
31 | n/a | dir is available to created environments. |
---|
32 | n/a | :param clear: If True, delete the contents of the environment directory if |
---|
33 | n/a | it already exists, before environment creation. |
---|
34 | n/a | :param symlinks: If True, attempt to symlink rather than copy files into |
---|
35 | n/a | virtual environment. |
---|
36 | n/a | :param upgrade: If True, upgrade an existing virtual environment. |
---|
37 | n/a | :param with_pip: If True, ensure pip is installed in the virtual |
---|
38 | n/a | environment |
---|
39 | n/a | :param prompt: Alternative terminal prefix for the environment. |
---|
40 | n/a | """ |
---|
41 | n/a | |
---|
42 | n/a | def __init__(self, system_site_packages=False, clear=False, |
---|
43 | n/a | symlinks=False, upgrade=False, with_pip=False, prompt=None): |
---|
44 | n/a | self.system_site_packages = system_site_packages |
---|
45 | n/a | self.clear = clear |
---|
46 | n/a | self.symlinks = symlinks |
---|
47 | n/a | self.upgrade = upgrade |
---|
48 | n/a | self.with_pip = with_pip |
---|
49 | n/a | self.prompt = prompt |
---|
50 | n/a | |
---|
51 | n/a | def create(self, env_dir): |
---|
52 | n/a | """ |
---|
53 | n/a | Create a virtual environment in a directory. |
---|
54 | n/a | |
---|
55 | n/a | :param env_dir: The target directory to create an environment in. |
---|
56 | n/a | |
---|
57 | n/a | """ |
---|
58 | n/a | env_dir = os.path.abspath(env_dir) |
---|
59 | n/a | context = self.ensure_directories(env_dir) |
---|
60 | n/a | # See issue 24875. We need system_site_packages to be False |
---|
61 | n/a | # until after pip is installed. |
---|
62 | n/a | true_system_site_packages = self.system_site_packages |
---|
63 | n/a | self.system_site_packages = False |
---|
64 | n/a | self.create_configuration(context) |
---|
65 | n/a | self.setup_python(context) |
---|
66 | n/a | if self.with_pip: |
---|
67 | n/a | self._setup_pip(context) |
---|
68 | n/a | if not self.upgrade: |
---|
69 | n/a | self.setup_scripts(context) |
---|
70 | n/a | self.post_setup(context) |
---|
71 | n/a | if true_system_site_packages: |
---|
72 | n/a | # We had set it to False before, now |
---|
73 | n/a | # restore it and rewrite the configuration |
---|
74 | n/a | self.system_site_packages = True |
---|
75 | n/a | self.create_configuration(context) |
---|
76 | n/a | |
---|
77 | n/a | def clear_directory(self, path): |
---|
78 | n/a | for fn in os.listdir(path): |
---|
79 | n/a | fn = os.path.join(path, fn) |
---|
80 | n/a | if os.path.islink(fn) or os.path.isfile(fn): |
---|
81 | n/a | os.remove(fn) |
---|
82 | n/a | elif os.path.isdir(fn): |
---|
83 | n/a | shutil.rmtree(fn) |
---|
84 | n/a | |
---|
85 | n/a | def ensure_directories(self, env_dir): |
---|
86 | n/a | """ |
---|
87 | n/a | Create the directories for the environment. |
---|
88 | n/a | |
---|
89 | n/a | Returns a context object which holds paths in the environment, |
---|
90 | n/a | for use by subsequent logic. |
---|
91 | n/a | """ |
---|
92 | n/a | |
---|
93 | n/a | def create_if_needed(d): |
---|
94 | n/a | if not os.path.exists(d): |
---|
95 | n/a | os.makedirs(d) |
---|
96 | n/a | elif os.path.islink(d) or os.path.isfile(d): |
---|
97 | n/a | raise ValueError('Unable to create directory %r' % d) |
---|
98 | n/a | |
---|
99 | n/a | if os.path.exists(env_dir) and self.clear: |
---|
100 | n/a | self.clear_directory(env_dir) |
---|
101 | n/a | context = types.SimpleNamespace() |
---|
102 | n/a | context.env_dir = env_dir |
---|
103 | n/a | context.env_name = os.path.split(env_dir)[1] |
---|
104 | n/a | prompt = self.prompt if self.prompt is not None else context.env_name |
---|
105 | n/a | context.prompt = '(%s) ' % prompt |
---|
106 | n/a | create_if_needed(env_dir) |
---|
107 | n/a | env = os.environ |
---|
108 | n/a | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in env: |
---|
109 | n/a | executable = os.environ['__PYVENV_LAUNCHER__'] |
---|
110 | n/a | else: |
---|
111 | n/a | executable = sys.executable |
---|
112 | n/a | dirname, exename = os.path.split(os.path.abspath(executable)) |
---|
113 | n/a | context.executable = executable |
---|
114 | n/a | context.python_dir = dirname |
---|
115 | n/a | context.python_exe = exename |
---|
116 | n/a | if sys.platform == 'win32': |
---|
117 | n/a | binname = 'Scripts' |
---|
118 | n/a | incpath = 'Include' |
---|
119 | n/a | libpath = os.path.join(env_dir, 'Lib', 'site-packages') |
---|
120 | n/a | else: |
---|
121 | n/a | binname = 'bin' |
---|
122 | n/a | incpath = 'include' |
---|
123 | n/a | libpath = os.path.join(env_dir, 'lib', |
---|
124 | n/a | 'python%d.%d' % sys.version_info[:2], |
---|
125 | n/a | 'site-packages') |
---|
126 | n/a | context.inc_path = path = os.path.join(env_dir, incpath) |
---|
127 | n/a | create_if_needed(path) |
---|
128 | n/a | create_if_needed(libpath) |
---|
129 | n/a | # Issue 21197: create lib64 as a symlink to lib on 64-bit non-OS X POSIX |
---|
130 | n/a | if ((sys.maxsize > 2**32) and (os.name == 'posix') and |
---|
131 | n/a | (sys.platform != 'darwin')): |
---|
132 | n/a | link_path = os.path.join(env_dir, 'lib64') |
---|
133 | n/a | if not os.path.exists(link_path): # Issue #21643 |
---|
134 | n/a | os.symlink('lib', link_path) |
---|
135 | n/a | context.bin_path = binpath = os.path.join(env_dir, binname) |
---|
136 | n/a | context.bin_name = binname |
---|
137 | n/a | context.env_exe = os.path.join(binpath, exename) |
---|
138 | n/a | create_if_needed(binpath) |
---|
139 | n/a | return context |
---|
140 | n/a | |
---|
141 | n/a | def create_configuration(self, context): |
---|
142 | n/a | """ |
---|
143 | n/a | Create a configuration file indicating where the environment's Python |
---|
144 | n/a | was copied from, and whether the system site-packages should be made |
---|
145 | n/a | available in the environment. |
---|
146 | n/a | |
---|
147 | n/a | :param context: The information for the environment creation request |
---|
148 | n/a | being processed. |
---|
149 | n/a | """ |
---|
150 | n/a | context.cfg_path = path = os.path.join(context.env_dir, 'pyvenv.cfg') |
---|
151 | n/a | with open(path, 'w', encoding='utf-8') as f: |
---|
152 | n/a | f.write('home = %s\n' % context.python_dir) |
---|
153 | n/a | if self.system_site_packages: |
---|
154 | n/a | incl = 'true' |
---|
155 | n/a | else: |
---|
156 | n/a | incl = 'false' |
---|
157 | n/a | f.write('include-system-site-packages = %s\n' % incl) |
---|
158 | n/a | f.write('version = %d.%d.%d\n' % sys.version_info[:3]) |
---|
159 | n/a | |
---|
160 | n/a | if os.name == 'nt': |
---|
161 | n/a | def include_binary(self, f): |
---|
162 | n/a | if f.endswith(('.pyd', '.dll')): |
---|
163 | n/a | result = True |
---|
164 | n/a | else: |
---|
165 | n/a | result = f.startswith('python') and f.endswith('.exe') |
---|
166 | n/a | return result |
---|
167 | n/a | |
---|
168 | n/a | def symlink_or_copy(self, src, dst, relative_symlinks_ok=False): |
---|
169 | n/a | """ |
---|
170 | n/a | Try symlinking a file, and if that fails, fall back to copying. |
---|
171 | n/a | """ |
---|
172 | n/a | force_copy = not self.symlinks |
---|
173 | n/a | if not force_copy: |
---|
174 | n/a | try: |
---|
175 | n/a | if not os.path.islink(dst): # can't link to itself! |
---|
176 | n/a | if relative_symlinks_ok: |
---|
177 | n/a | assert os.path.dirname(src) == os.path.dirname(dst) |
---|
178 | n/a | os.symlink(os.path.basename(src), dst) |
---|
179 | n/a | else: |
---|
180 | n/a | os.symlink(src, dst) |
---|
181 | n/a | except Exception: # may need to use a more specific exception |
---|
182 | n/a | logger.warning('Unable to symlink %r to %r', src, dst) |
---|
183 | n/a | force_copy = True |
---|
184 | n/a | if force_copy: |
---|
185 | n/a | shutil.copyfile(src, dst) |
---|
186 | n/a | |
---|
187 | n/a | def setup_python(self, context): |
---|
188 | n/a | """ |
---|
189 | n/a | Set up a Python executable in the environment. |
---|
190 | n/a | |
---|
191 | n/a | :param context: The information for the environment creation request |
---|
192 | n/a | being processed. |
---|
193 | n/a | """ |
---|
194 | n/a | binpath = context.bin_path |
---|
195 | n/a | path = context.env_exe |
---|
196 | n/a | copier = self.symlink_or_copy |
---|
197 | n/a | copier(context.executable, path) |
---|
198 | n/a | dirname = context.python_dir |
---|
199 | n/a | if os.name != 'nt': |
---|
200 | n/a | if not os.path.islink(path): |
---|
201 | n/a | os.chmod(path, 0o755) |
---|
202 | n/a | for suffix in ('python', 'python3'): |
---|
203 | n/a | path = os.path.join(binpath, suffix) |
---|
204 | n/a | if not os.path.exists(path): |
---|
205 | n/a | # Issue 18807: make copies if |
---|
206 | n/a | # symlinks are not wanted |
---|
207 | n/a | copier(context.env_exe, path, relative_symlinks_ok=True) |
---|
208 | n/a | if not os.path.islink(path): |
---|
209 | n/a | os.chmod(path, 0o755) |
---|
210 | n/a | else: |
---|
211 | n/a | subdir = 'DLLs' |
---|
212 | n/a | include = self.include_binary |
---|
213 | n/a | files = [f for f in os.listdir(dirname) if include(f)] |
---|
214 | n/a | for f in files: |
---|
215 | n/a | src = os.path.join(dirname, f) |
---|
216 | n/a | dst = os.path.join(binpath, f) |
---|
217 | n/a | if dst != context.env_exe: # already done, above |
---|
218 | n/a | copier(src, dst) |
---|
219 | n/a | dirname = os.path.join(dirname, subdir) |
---|
220 | n/a | if os.path.isdir(dirname): |
---|
221 | n/a | files = [f for f in os.listdir(dirname) if include(f)] |
---|
222 | n/a | for f in files: |
---|
223 | n/a | src = os.path.join(dirname, f) |
---|
224 | n/a | dst = os.path.join(binpath, f) |
---|
225 | n/a | copier(src, dst) |
---|
226 | n/a | # copy init.tcl over |
---|
227 | n/a | for root, dirs, files in os.walk(context.python_dir): |
---|
228 | n/a | if 'init.tcl' in files: |
---|
229 | n/a | tcldir = os.path.basename(root) |
---|
230 | n/a | tcldir = os.path.join(context.env_dir, 'Lib', tcldir) |
---|
231 | n/a | if not os.path.exists(tcldir): |
---|
232 | n/a | os.makedirs(tcldir) |
---|
233 | n/a | src = os.path.join(root, 'init.tcl') |
---|
234 | n/a | dst = os.path.join(tcldir, 'init.tcl') |
---|
235 | n/a | shutil.copyfile(src, dst) |
---|
236 | n/a | break |
---|
237 | n/a | |
---|
238 | n/a | def _setup_pip(self, context): |
---|
239 | n/a | """Installs or upgrades pip in a virtual environment""" |
---|
240 | n/a | # We run ensurepip in isolated mode to avoid side effects from |
---|
241 | n/a | # environment vars, the current directory and anything else |
---|
242 | n/a | # intended for the global Python environment |
---|
243 | n/a | cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade', |
---|
244 | n/a | '--default-pip'] |
---|
245 | n/a | subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
---|
246 | n/a | |
---|
247 | n/a | def setup_scripts(self, context): |
---|
248 | n/a | """ |
---|
249 | n/a | Set up scripts into the created environment from a directory. |
---|
250 | n/a | |
---|
251 | n/a | This method installs the default scripts into the environment |
---|
252 | n/a | being created. You can prevent the default installation by overriding |
---|
253 | n/a | this method if you really need to, or if you need to specify |
---|
254 | n/a | a different location for the scripts to install. By default, the |
---|
255 | n/a | 'scripts' directory in the venv package is used as the source of |
---|
256 | n/a | scripts to install. |
---|
257 | n/a | """ |
---|
258 | n/a | path = os.path.abspath(os.path.dirname(__file__)) |
---|
259 | n/a | path = os.path.join(path, 'scripts') |
---|
260 | n/a | self.install_scripts(context, path) |
---|
261 | n/a | |
---|
262 | n/a | def post_setup(self, context): |
---|
263 | n/a | """ |
---|
264 | n/a | Hook for post-setup modification of the venv. Subclasses may install |
---|
265 | n/a | additional packages or scripts here, add activation shell scripts, etc. |
---|
266 | n/a | |
---|
267 | n/a | :param context: The information for the environment creation request |
---|
268 | n/a | being processed. |
---|
269 | n/a | """ |
---|
270 | n/a | pass |
---|
271 | n/a | |
---|
272 | n/a | def replace_variables(self, text, context): |
---|
273 | n/a | """ |
---|
274 | n/a | Replace variable placeholders in script text with context-specific |
---|
275 | n/a | variables. |
---|
276 | n/a | |
---|
277 | n/a | Return the text passed in , but with variables replaced. |
---|
278 | n/a | |
---|
279 | n/a | :param text: The text in which to replace placeholder variables. |
---|
280 | n/a | :param context: The information for the environment creation request |
---|
281 | n/a | being processed. |
---|
282 | n/a | """ |
---|
283 | n/a | text = text.replace('__VENV_DIR__', context.env_dir) |
---|
284 | n/a | text = text.replace('__VENV_NAME__', context.env_name) |
---|
285 | n/a | text = text.replace('__VENV_PROMPT__', context.prompt) |
---|
286 | n/a | text = text.replace('__VENV_BIN_NAME__', context.bin_name) |
---|
287 | n/a | text = text.replace('__VENV_PYTHON__', context.env_exe) |
---|
288 | n/a | return text |
---|
289 | n/a | |
---|
290 | n/a | def install_scripts(self, context, path): |
---|
291 | n/a | """ |
---|
292 | n/a | Install scripts into the created environment from a directory. |
---|
293 | n/a | |
---|
294 | n/a | :param context: The information for the environment creation request |
---|
295 | n/a | being processed. |
---|
296 | n/a | :param path: Absolute pathname of a directory containing script. |
---|
297 | n/a | Scripts in the 'common' subdirectory of this directory, |
---|
298 | n/a | and those in the directory named for the platform |
---|
299 | n/a | being run on, are installed in the created environment. |
---|
300 | n/a | Placeholder variables are replaced with environment- |
---|
301 | n/a | specific values. |
---|
302 | n/a | """ |
---|
303 | n/a | binpath = context.bin_path |
---|
304 | n/a | plen = len(path) |
---|
305 | n/a | for root, dirs, files in os.walk(path): |
---|
306 | n/a | if root == path: # at top-level, remove irrelevant dirs |
---|
307 | n/a | for d in dirs[:]: |
---|
308 | n/a | if d not in ('common', os.name): |
---|
309 | n/a | dirs.remove(d) |
---|
310 | n/a | continue # ignore files in top level |
---|
311 | n/a | for f in files: |
---|
312 | n/a | srcfile = os.path.join(root, f) |
---|
313 | n/a | suffix = root[plen:].split(os.sep)[2:] |
---|
314 | n/a | if not suffix: |
---|
315 | n/a | dstdir = binpath |
---|
316 | n/a | else: |
---|
317 | n/a | dstdir = os.path.join(binpath, *suffix) |
---|
318 | n/a | if not os.path.exists(dstdir): |
---|
319 | n/a | os.makedirs(dstdir) |
---|
320 | n/a | dstfile = os.path.join(dstdir, f) |
---|
321 | n/a | with open(srcfile, 'rb') as f: |
---|
322 | n/a | data = f.read() |
---|
323 | n/a | if not srcfile.endswith('.exe'): |
---|
324 | n/a | try: |
---|
325 | n/a | data = data.decode('utf-8') |
---|
326 | n/a | data = self.replace_variables(data, context) |
---|
327 | n/a | data = data.encode('utf-8') |
---|
328 | n/a | except UnicodeError as e: |
---|
329 | n/a | data = None |
---|
330 | n/a | logger.warning('unable to copy script %r, ' |
---|
331 | n/a | 'may be binary: %s', srcfile, e) |
---|
332 | n/a | if data is not None: |
---|
333 | n/a | with open(dstfile, 'wb') as f: |
---|
334 | n/a | f.write(data) |
---|
335 | n/a | shutil.copymode(srcfile, dstfile) |
---|
336 | n/a | |
---|
337 | n/a | |
---|
338 | n/a | def create(env_dir, system_site_packages=False, clear=False, |
---|
339 | n/a | symlinks=False, with_pip=False, prompt=None): |
---|
340 | n/a | """Create a virtual environment in a directory.""" |
---|
341 | n/a | builder = EnvBuilder(system_site_packages=system_site_packages, |
---|
342 | n/a | clear=clear, symlinks=symlinks, with_pip=with_pip, |
---|
343 | n/a | prompt=prompt) |
---|
344 | n/a | builder.create(env_dir) |
---|
345 | n/a | |
---|
346 | n/a | def main(args=None): |
---|
347 | n/a | compatible = True |
---|
348 | n/a | if sys.version_info < (3, 3): |
---|
349 | n/a | compatible = False |
---|
350 | n/a | elif not hasattr(sys, 'base_prefix'): |
---|
351 | n/a | compatible = False |
---|
352 | n/a | if not compatible: |
---|
353 | n/a | raise ValueError('This script is only for use with Python >= 3.3') |
---|
354 | n/a | else: |
---|
355 | n/a | import argparse |
---|
356 | n/a | |
---|
357 | n/a | parser = argparse.ArgumentParser(prog=__name__, |
---|
358 | n/a | description='Creates virtual Python ' |
---|
359 | n/a | 'environments in one or ' |
---|
360 | n/a | 'more target ' |
---|
361 | n/a | 'directories.', |
---|
362 | n/a | epilog='Once an environment has been ' |
---|
363 | n/a | 'created, you may wish to ' |
---|
364 | n/a | 'activate it, e.g. by ' |
---|
365 | n/a | 'sourcing an activate script ' |
---|
366 | n/a | 'in its bin directory.') |
---|
367 | n/a | parser.add_argument('dirs', metavar='ENV_DIR', nargs='+', |
---|
368 | n/a | help='A directory to create the environment in.') |
---|
369 | n/a | parser.add_argument('--system-site-packages', default=False, |
---|
370 | n/a | action='store_true', dest='system_site', |
---|
371 | n/a | help='Give the virtual environment access to the ' |
---|
372 | n/a | 'system site-packages dir.') |
---|
373 | n/a | if os.name == 'nt': |
---|
374 | n/a | use_symlinks = False |
---|
375 | n/a | else: |
---|
376 | n/a | use_symlinks = True |
---|
377 | n/a | group = parser.add_mutually_exclusive_group() |
---|
378 | n/a | group.add_argument('--symlinks', default=use_symlinks, |
---|
379 | n/a | action='store_true', dest='symlinks', |
---|
380 | n/a | help='Try to use symlinks rather than copies, ' |
---|
381 | n/a | 'when symlinks are not the default for ' |
---|
382 | n/a | 'the platform.') |
---|
383 | n/a | group.add_argument('--copies', default=not use_symlinks, |
---|
384 | n/a | action='store_false', dest='symlinks', |
---|
385 | n/a | help='Try to use copies rather than symlinks, ' |
---|
386 | n/a | 'even when symlinks are the default for ' |
---|
387 | n/a | 'the platform.') |
---|
388 | n/a | parser.add_argument('--clear', default=False, action='store_true', |
---|
389 | n/a | dest='clear', help='Delete the contents of the ' |
---|
390 | n/a | 'environment directory if it ' |
---|
391 | n/a | 'already exists, before ' |
---|
392 | n/a | 'environment creation.') |
---|
393 | n/a | parser.add_argument('--upgrade', default=False, action='store_true', |
---|
394 | n/a | dest='upgrade', help='Upgrade the environment ' |
---|
395 | n/a | 'directory to use this version ' |
---|
396 | n/a | 'of Python, assuming Python ' |
---|
397 | n/a | 'has been upgraded in-place.') |
---|
398 | n/a | parser.add_argument('--without-pip', dest='with_pip', |
---|
399 | n/a | default=True, action='store_false', |
---|
400 | n/a | help='Skips installing or upgrading pip in the ' |
---|
401 | n/a | 'virtual environment (pip is bootstrapped ' |
---|
402 | n/a | 'by default)') |
---|
403 | n/a | parser.add_argument('--prompt', |
---|
404 | n/a | help='Provides an alternative prompt prefix for ' |
---|
405 | n/a | 'this environment.') |
---|
406 | n/a | options = parser.parse_args(args) |
---|
407 | n/a | if options.upgrade and options.clear: |
---|
408 | n/a | raise ValueError('you cannot supply --upgrade and --clear together.') |
---|
409 | n/a | builder = EnvBuilder(system_site_packages=options.system_site, |
---|
410 | n/a | clear=options.clear, |
---|
411 | n/a | symlinks=options.symlinks, |
---|
412 | n/a | upgrade=options.upgrade, |
---|
413 | n/a | with_pip=options.with_pip, |
---|
414 | n/a | prompt=options.prompt) |
---|
415 | n/a | for d in options.dirs: |
---|
416 | n/a | builder.create(d) |
---|
417 | n/a | |
---|
418 | n/a | if __name__ == '__main__': |
---|
419 | n/a | rc = 1 |
---|
420 | n/a | try: |
---|
421 | n/a | main() |
---|
422 | n/a | rc = 0 |
---|
423 | n/a | except Exception as e: |
---|
424 | n/a | print('Error: %s' % e, file=sys.stderr) |
---|
425 | n/a | sys.exit(rc) |
---|