Debian Codegraph @

Lib/venv/__init__.py

raw ◀ ▶
1 Debian patch touch this file
1 """
2 Virtual environment (venv) package for Python. Based on PEP 405.
3
4 Copyright (C) 2011-2014 Vinay Sajip.
5 Licensed to the PSF under a contributor agreement.
6 """
7 import logging
8 import os
9 import shutil
10 import subprocess
11 import sys
12 import sysconfig
13 import types
14 import shlex
15
16
17 CORE_VENV_DEPS = ('pip',)
18 logger = logging.getLogger(__name__)
19
20
21 class EnvBuilder:
22 """
23 This class exists to allow virtual environment creation to be
24 customized. The constructor parameters determine the builder's
25 behaviour when called upon to create a virtual environment.
26
27 By default, the builder makes the system (global) site-packages dir
28 *un*available to the created environment.
29
30 If invoked using the Python -m option, the default is to use copying
31 on Windows platforms but symlinks elsewhere. If instantiated some
32 other way, the default is to *not* use symlinks.
33
34 :param system_site_packages: If True, the system (global) site-packages
35 dir is available to created environments.
36 :param clear: If True, delete the contents of the environment directory if
37 it already exists, before environment creation.
38 :param symlinks: If True, attempt to symlink rather than copy files into
39 virtual environment.
40 :param upgrade: If True, upgrade an existing virtual environment.
41 :param with_pip: If True, ensure pip is installed in the virtual
42 environment
43 :param prompt: Alternative terminal prefix for the environment.
44 :param upgrade_deps: Update the base venv modules to the latest on PyPI
45 :param scm_ignore_files: Create ignore files for the SCMs specified by the
46 iterable.
47 """
48
49 def __init__(self, system_site_packages=False, clear=False,
50 symlinks=False, upgrade=False, with_pip=False, prompt=None,
51 upgrade_deps=False, *, scm_ignore_files=frozenset()):
52 self.system_site_packages = system_site_packages
53 self.clear = clear
54 self.symlinks = symlinks
55 self.upgrade = upgrade
56 self.with_pip = with_pip
57 self.orig_prompt = prompt
58 if prompt == '.': # see bpo-38901
59 prompt = os.path.basename(os.getcwd())
60 self.prompt = prompt
61 self.upgrade_deps = upgrade_deps
62 self.scm_ignore_files = frozenset(map(str.lower, scm_ignore_files))
63
64 def create(self, env_dir):
65 """
66 Create a virtual environment in a directory.
67
68 :param env_dir: The target directory to create an environment in.
69
70 """
71 env_dir = os.path.abspath(env_dir)
72 context = self.ensure_directories(env_dir)
73 for scm in self.scm_ignore_files:
74 getattr(self, f"create_{scm}_ignore_file")(context)
75 # See issue 24875. We need system_site_packages to be False
76 # until after pip is installed.
77 true_system_site_packages = self.system_site_packages
78 self.system_site_packages = False
79 self.create_configuration(context)
80 self.setup_python(context)
81 if self.with_pip:
82 self._setup_pip(context)
83 if not self.upgrade:
84 self.setup_scripts(context)
85 self.post_setup(context)
86 if true_system_site_packages:
87 # We had set it to False before, now
88 # restore it and rewrite the configuration
89 self.system_site_packages = True
90 self.create_configuration(context)
91 if self.upgrade_deps:
92 self.upgrade_dependencies(context)
93
94 def clear_directory(self, path):
95 for fn in os.listdir(path):
96 fn = os.path.join(path, fn)
97 if os.path.islink(fn) or os.path.isfile(fn):
98 os.remove(fn)
99 elif os.path.isdir(fn):
100 shutil.rmtree(fn)
101
102 def _venv_path(self, env_dir, name):
103 vars = {
104 'base': env_dir,
105 'platbase': env_dir,
106 'installed_base': env_dir,
107 'installed_platbase': env_dir,
108 }
109 return sysconfig.get_path(name, scheme='venv', vars=vars)
110
111 @classmethod
112 def _same_path(cls, path1, path2):
113 """Check whether two paths appear the same.
114
115 Whether they refer to the same file is irrelevant; we're testing for
116 whether a human reader would look at the path string and easily tell
117 that they're the same file.
118 """
119 if sys.platform == 'win32':
120 if os.path.normcase(path1) == os.path.normcase(path2):
121 return True
122 # gh-90329: Don't display a warning for short/long names
123 import _winapi
124 try:
125 path1 = _winapi.GetLongPathName(os.fsdecode(path1))
126 except OSError:
127 pass
128 try:
129 path2 = _winapi.GetLongPathName(os.fsdecode(path2))
130 except OSError:
131 pass
132 if os.path.normcase(path1) == os.path.normcase(path2):
133 return True
134 return False
135 else:
136 return path1 == path2
137
138 def ensure_directories(self, env_dir):
139 """
140 Create the directories for the environment.
141
142 Returns a context object which holds paths in the environment,
143 for use by subsequent logic.
144 """
145
146 def create_if_needed(d):
147 if not os.path.exists(d):
148 os.makedirs(d)
149 elif os.path.islink(d) or os.path.isfile(d):
150 raise ValueError('Unable to create directory %r' % d)
151
152 if os.pathsep in os.fspath(env_dir):
153 raise ValueError(f'Refusing to create a venv in {env_dir} because '
154 f'it contains the PATH separator {os.pathsep}.')
155 if os.path.exists(env_dir) and self.clear:
156 self.clear_directory(env_dir)
157 context = types.SimpleNamespace()
158 context.env_dir = env_dir
159 context.env_name = os.path.split(env_dir)[1]
160 context.prompt = self.prompt if self.prompt is not None else context.env_name
161 create_if_needed(env_dir)
162 executable = sys._base_executable
163 if not executable: # see gh-96861
164 raise ValueError('Unable to determine path to the running '
165 'Python interpreter. Provide an explicit path or '
166 'check that your PATH environment variable is '
167 'correctly set.')
168 dirname, exename = os.path.split(os.path.abspath(executable))
169 if sys.platform == 'win32':
170 # Always create the simplest name in the venv. It will either be a
171 # link back to executable, or a copy of the appropriate launcher
172 _d = '_d' if os.path.splitext(exename)[0].endswith('_d') else ''
173 exename = f'python{_d}.exe'
174 context.executable = executable
175 context.python_dir = dirname
176 context.python_exe = exename
177 binpath = self._venv_path(env_dir, 'scripts')
178 incpath = self._venv_path(env_dir, 'include')
179 libpath = self._venv_path(env_dir, 'purelib')
180
181 context.inc_path = incpath
182 create_if_needed(incpath)
183 context.lib_path = libpath
184 create_if_needed(libpath)
185 # Issue 21197: create lib64 as a symlink to lib on 64-bit non-OS X POSIX
186 if ((sys.maxsize > 2**32) and (os.name == 'posix') and
187 (sys.platform != 'darwin')):
188 link_path = os.path.join(env_dir, 'lib64')
189 if not os.path.exists(link_path): # Issue #21643
190 os.symlink('lib', link_path)
191 context.bin_path = binpath
192 context.bin_name = os.path.relpath(binpath, env_dir)
193 context.env_exe = os.path.join(binpath, exename)
194 create_if_needed(binpath)
195 # Assign and update the command to use when launching the newly created
196 # environment, in case it isn't simply the executable script (e.g. bpo-45337)
197 context.env_exec_cmd = context.env_exe
198 if sys.platform == 'win32':
199 # bpo-45337: Fix up env_exec_cmd to account for file system redirections.
200 # Some redirects only apply to CreateFile and not CreateProcess
201 real_env_exe = os.path.realpath(context.env_exe)
202 if not self._same_path(real_env_exe, context.env_exe):
203 logger.warning('Actual environment location may have moved due to '
204 'redirects, links or junctions.\n'
205 ' Requested location: "%s"\n'
206 ' Actual location: "%s"',
207 context.env_exe, real_env_exe)
208 context.env_exec_cmd = real_env_exe
209 return context
210
211 def create_configuration(self, context):
212 """
213 Create a configuration file indicating where the environment's Python
214 was copied from, and whether the system site-packages should be made
215 available in the environment.
216
217 :param context: The information for the environment creation request
218 being processed.
219 """
220 context.cfg_path = path = os.path.join(context.env_dir, 'pyvenv.cfg')
221 with open(path, 'w', encoding='utf-8') as f:
222 f.write('home = %s\n' % context.python_dir)
223 if self.system_site_packages:
224 incl = 'true'
225 else:
226 incl = 'false'
227 f.write('include-system-site-packages = %s\n' % incl)
228 f.write('version = %d.%d.%d\n' % sys.version_info[:3])
229 if self.prompt is not None:
230 f.write(f'prompt = {self.prompt!r}\n')
231 f.write('executable = %s\n' % os.path.realpath(sys.executable))
232 args = []
233 nt = os.name == 'nt'
234 if nt and self.symlinks:
235 args.append('--symlinks')
236 if not nt and not self.symlinks:
237 args.append('--copies')
238 if not self.with_pip:
239 args.append('--without-pip')
240 if self.system_site_packages:
241 args.append('--system-site-packages')
242 if self.clear:
243 args.append('--clear')
244 if self.upgrade:
245 args.append('--upgrade')
246 if self.upgrade_deps:
247 args.append('--upgrade-deps')
248 if self.orig_prompt is not None:
249 args.append(f'--prompt="{self.orig_prompt}"')
250 if not self.scm_ignore_files:
251 args.append('--without-scm-ignore-files')
252
253 args.append(context.env_dir)
254 args = ' '.join(args)
255 f.write(f'command = {sys.executable} -m venv {args}\n')
256
257 def symlink_or_copy(self, src, dst, relative_symlinks_ok=False):
258 """
259 Try symlinking a file, and if that fails, fall back to copying.
260 (Unused on Windows, because we can't just copy a failed symlink file: we
261 switch to a different set of files instead.)
262 """
263 assert os.name != 'nt'
264 force_copy = not self.symlinks
265 if not force_copy:
266 try:
267 if not os.path.islink(dst): # can't link to itself!
268 if relative_symlinks_ok:
269 assert os.path.dirname(src) == os.path.dirname(dst)
270 os.symlink(os.path.basename(src), dst)
271 else:
272 os.symlink(src, dst)
273 except Exception: # may need to use a more specific exception
274 logger.warning('Unable to symlink %r to %r', src, dst)
275 force_copy = True
276 if force_copy:
277 shutil.copyfile(src, dst)
278
279 def create_git_ignore_file(self, context):
280 """
281 Create a .gitignore file in the environment directory.
282
283 The contents of the file cause the entire environment directory to be
284 ignored by git.
285 """
286 gitignore_path = os.path.join(context.env_dir, '.gitignore')
287 with open(gitignore_path, 'w', encoding='utf-8') as file:
288 file.write('# Created by venv; '
289 'see https://docs.python.org/3/library/venv.html\n')
290 file.write('*\n')
291
292 if os.name != 'nt':
293 def setup_python(self, context):
294 """
295 Set up a Python executable in the environment.
296
297 :param context: The information for the environment creation request
298 being processed.
299 """
300 binpath = context.bin_path
301 path = context.env_exe
302 copier = self.symlink_or_copy
303 dirname = context.python_dir
304 copier(context.executable, path)
305 if not os.path.islink(path):
306 os.chmod(path, 0o755)
307 for suffix in ('python', 'python3',
308 f'python3.{sys.version_info[1]}'):
309 path = os.path.join(binpath, suffix)
310 if not os.path.exists(path):
311 # Issue 18807: make copies if
312 # symlinks are not wanted
313 copier(context.env_exe, path, relative_symlinks_ok=True)
314 if not os.path.islink(path):
315 os.chmod(path, 0o755)
316
317 else:
318 def setup_python(self, context):
319 """
320 Set up a Python executable in the environment.
321
322 :param context: The information for the environment creation request
323 being processed.
324 """
325 binpath = context.bin_path
326 dirname = context.python_dir
327 exename = os.path.basename(context.env_exe)
328 exe_stem = os.path.splitext(exename)[0]
329 exe_d = '_d' if os.path.normcase(exe_stem).endswith('_d') else ''
330 if sysconfig.is_python_build():
331 scripts = dirname
332 else:
333 scripts = os.path.join(os.path.dirname(__file__),
334 'scripts', 'nt')
335 if not sysconfig.get_config_var("Py_GIL_DISABLED"):
336 python_exe = os.path.join(dirname, f'python{exe_d}.exe')
337 pythonw_exe = os.path.join(dirname, f'pythonw{exe_d}.exe')
338 link_sources = {
339 'python.exe': python_exe,
340 f'python{exe_d}.exe': python_exe,
341 'pythonw.exe': pythonw_exe,
342 f'pythonw{exe_d}.exe': pythonw_exe,
343 }
344 python_exe = os.path.join(scripts, f'venvlauncher{exe_d}.exe')
345 pythonw_exe = os.path.join(scripts, f'venvwlauncher{exe_d}.exe')
346 copy_sources = {
347 'python.exe': python_exe,
348 f'python{exe_d}.exe': python_exe,
349 'pythonw.exe': pythonw_exe,
350 f'pythonw{exe_d}.exe': pythonw_exe,
351 }
352 else:
353 exe_t = f'3.{sys.version_info[1]}t'
354 python_exe = os.path.join(dirname, f'python{exe_t}{exe_d}.exe')
355 pythonw_exe = os.path.join(dirname, f'pythonw{exe_t}{exe_d}.exe')
356 link_sources = {
357 'python.exe': python_exe,
358 f'python{exe_d}.exe': python_exe,
359 f'python{exe_t}.exe': python_exe,
360 f'python{exe_t}{exe_d}.exe': python_exe,
361 'pythonw.exe': pythonw_exe,
362 f'pythonw{exe_d}.exe': pythonw_exe,
363 f'pythonw{exe_t}.exe': pythonw_exe,
364 f'pythonw{exe_t}{exe_d}.exe': pythonw_exe,
365 }
366 python_exe = os.path.join(scripts, f'venvlaunchert{exe_d}.exe')
367 pythonw_exe = os.path.join(scripts, f'venvwlaunchert{exe_d}.exe')
368 copy_sources = {
369 'python.exe': python_exe,
370 f'python{exe_d}.exe': python_exe,
371 f'python{exe_t}.exe': python_exe,
372 f'python{exe_t}{exe_d}.exe': python_exe,
373 'pythonw.exe': pythonw_exe,
374 f'pythonw{exe_d}.exe': pythonw_exe,
375 f'pythonw{exe_t}.exe': pythonw_exe,
376 f'pythonw{exe_t}{exe_d}.exe': pythonw_exe,
377 }
378
379 do_copies = True
380 if self.symlinks:
381 do_copies = False
382 # For symlinking, we need all the DLLs to be available alongside
383 # the executables.
384 link_sources.update({
385 f: os.path.join(dirname, f) for f in os.listdir(dirname)
386 if os.path.normcase(f).startswith(('python', 'vcruntime'))
387 and os.path.normcase(os.path.splitext(f)[1]) == '.dll'
388 })
389
390 to_unlink = []
391 for dest, src in link_sources.items():
392 dest = os.path.join(binpath, dest)
393 try:
394 os.symlink(src, dest)
395 to_unlink.append(dest)
396 except OSError:
397 logger.warning('Unable to symlink %r to %r', src, dest)
398 do_copies = True
399 for f in to_unlink:
400 try:
401 os.unlink(f)
402 except OSError:
403 logger.warning('Failed to clean up symlink %r',
404 f)
405 logger.warning('Retrying with copies')
406 break
407
408 if do_copies:
409 for dest, src in copy_sources.items():
410 dest = os.path.join(binpath, dest)
411 try:
412 shutil.copy2(src, dest)
413 except OSError:
414 logger.warning('Unable to copy %r to %r', src, dest)
415
416 if sysconfig.is_python_build():
417 # copy init.tcl
418 for root, dirs, files in os.walk(context.python_dir):
419 if 'init.tcl' in files:
420 tcldir = os.path.basename(root)
421 tcldir = os.path.join(context.env_dir, 'Lib', tcldir)
422 if not os.path.exists(tcldir):
423 os.makedirs(tcldir)
424 src = os.path.join(root, 'init.tcl')
425 dst = os.path.join(tcldir, 'init.tcl')
426 shutil.copyfile(src, dst)
427 break
428
429 def _call_new_python(self, context, *py_args, **kwargs):
430 """Executes the newly created Python using safe-ish options"""
431 # gh-98251: We do not want to just use '-I' because that masks
432 # legitimate user preferences (such as not writing bytecode). All we
433 # really need is to ensure that the path variables do not overrule
434 # normal venv handling.
435 args = [context.env_exec_cmd, *py_args]
436 kwargs['env'] = env = os.environ.copy()
437 env['VIRTUAL_ENV'] = context.env_dir
438 env.pop('PYTHONHOME', None)
439 env.pop('PYTHONPATH', None)
440 kwargs['cwd'] = context.env_dir
441 kwargs['executable'] = context.env_exec_cmd
442 subprocess.check_output(args, **kwargs)
443
444 def _setup_pip(self, context):
445 """Installs or upgrades pip in a virtual environment"""
•446 try:
447 self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
448 '--default-pip', stderr=subprocess.STDOUT)
449 except subprocess.CalledProcessError as err:
450 stdlib = sysconfig.get_path('stdlib')
451 if not os.path.exists(f'{stdlib}/ensurepip/__main__.py'):
452 print("""\
453 The virtual environment was not created successfully because ensurepip is not
454 available. On Debian/Ubuntu systems, you need to install the python3-venv
455 package using the following command.
456
457 apt install python{}-venv
458
459 You may need to use sudo with that command. After installing the python3-venv
460 package, recreate your virtual environment.
461
462 Failing command: {}
463 """.format(sysconfig.get_python_version(), context.env_exec_cmd))
464 sys.exit(1)
465 raise
466
467 def setup_scripts(self, context):
468 """
469 Set up scripts into the created environment from a directory.
470
471 This method installs the default scripts into the environment
472 being created. You can prevent the default installation by overriding
473 this method if you really need to, or if you need to specify
474 a different location for the scripts to install. By default, the
475 'scripts' directory in the venv package is used as the source of
476 scripts to install.
477 """
478 path = os.path.abspath(os.path.dirname(__file__))
479 path = os.path.join(path, 'scripts')
480 self.install_scripts(context, path)
481
482 def post_setup(self, context):
483 """
484 Hook for post-setup modification of the venv. Subclasses may install
485 additional packages or scripts here, add activation shell scripts, etc.
486
487 :param context: The information for the environment creation request
488 being processed.
489 """
490 pass
491
492 def replace_variables(self, text, context):
493 """
494 Replace variable placeholders in script text with context-specific
495 variables.
496
497 Return the text passed in , but with variables replaced.
498
499 :param text: The text in which to replace placeholder variables.
500 :param context: The information for the environment creation request
501 being processed.
502 """
503 replacements = {
504 '__VENV_DIR__': context.env_dir,
505 '__VENV_NAME__': context.env_name,
506 '__VENV_PROMPT__': context.prompt,
507 '__VENV_BIN_NAME__': context.bin_name,
508 '__VENV_PYTHON__': context.env_exe,
509 }
510
511 def quote_ps1(s):
512 """
513 This should satisfy PowerShell quoting rules [1], unless the quoted
514 string is passed directly to Windows native commands [2].
515 [1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules
516 [2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing#passing-arguments-that-contain-quote-characters
517 """
518 s = s.replace("'", "''")
519 return f"'{s}'"
520
521 def quote_bat(s):
522 return s
523
524 # gh-124651: need to quote the template strings properly
525 quote = shlex.quote
526 script_path = context.script_path
527 if script_path.endswith('.ps1'):
528 quote = quote_ps1
529 elif script_path.endswith('.bat'):
530 quote = quote_bat
531 else:
532 # fallbacks to POSIX shell compliant quote
533 quote = shlex.quote
534
535 replacements = {key: quote(s) for key, s in replacements.items()}
536 for key, quoted in replacements.items():
537 text = text.replace(key, quoted)
538 return text
539
540 def install_scripts(self, context, path):
541 """
542 Install scripts into the created environment from a directory.
543
544 :param context: The information for the environment creation request
545 being processed.
546 :param path: Absolute pathname of a directory containing script.
547 Scripts in the 'common' subdirectory of this directory,
548 and those in the directory named for the platform
549 being run on, are installed in the created environment.
550 Placeholder variables are replaced with environment-
551 specific values.
552 """
553 binpath = context.bin_path
554 plen = len(path)
555 if os.name == 'nt':
556 def skip_file(f):
557 f = os.path.normcase(f)
558 return (f.startswith(('python', 'venv'))
559 and f.endswith(('.exe', '.pdb')))
560 else:
561 def skip_file(f):
562 return False
563 for root, dirs, files in os.walk(path):
564 if root == path: # at top-level, remove irrelevant dirs
565 for d in dirs[:]:
566 if d not in ('common', os.name):
567 dirs.remove(d)
568 continue # ignore files in top level
569 for f in files:
570 if skip_file(f):
571 continue
572 srcfile = os.path.join(root, f)
573 suffix = root[plen:].split(os.sep)[2:]
574 if not suffix:
575 dstdir = binpath
576 else:
577 dstdir = os.path.join(binpath, *suffix)
578 if not os.path.exists(dstdir):
579 os.makedirs(dstdir)
580 dstfile = os.path.join(dstdir, f)
581 if os.name == 'nt' and srcfile.endswith(('.exe', '.pdb')):
582 shutil.copy2(srcfile, dstfile)
583 continue
584 with open(srcfile, 'rb') as f:
585 data = f.read()
586 try:
587 context.script_path = srcfile
588 new_data = (
589 self.replace_variables(data.decode('utf-8'), context)
590 .encode('utf-8')
591 )
592 except UnicodeError as e:
593 logger.warning('unable to copy script %r, '
594 'may be binary: %s', srcfile, e)
595 continue
596 if new_data == data:
597 shutil.copy2(srcfile, dstfile)
598 else:
599 with open(dstfile, 'wb') as f:
600 f.write(new_data)
601 shutil.copymode(srcfile, dstfile)
602
603 def upgrade_dependencies(self, context):
604 logger.debug(
605 f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
606 )
607 self._call_new_python(context, '-m', 'pip', 'install', '--upgrade',
608 *CORE_VENV_DEPS)
609
610
611 def create(env_dir, system_site_packages=False, clear=False,
612 symlinks=False, with_pip=False, prompt=None, upgrade_deps=False,
613 *, scm_ignore_files=frozenset()):
614 """Create a virtual environment in a directory."""
615 builder = EnvBuilder(system_site_packages=system_site_packages,
616 clear=clear, symlinks=symlinks, with_pip=with_pip,
617 prompt=prompt, upgrade_deps=upgrade_deps,
618 scm_ignore_files=scm_ignore_files)
619 builder.create(env_dir)
620
621
622 def main(args=None):
623 import argparse
624
625 parser = argparse.ArgumentParser(prog=__name__,
626 description='Creates virtual Python '
627 'environments in one or '
628 'more target '
629 'directories.',
630 epilog='Once an environment has been '
631 'created, you may wish to '
632 'activate it, e.g. by '
633 'sourcing an activate script '
634 'in its bin directory.')
635 parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
636 help='A directory to create the environment in.')
637 parser.add_argument('--system-site-packages', default=False,
638 action='store_true', dest='system_site',
639 help='Give the virtual environment access to the '
640 'system site-packages dir.')
641 if os.name == 'nt':
642 use_symlinks = False
643 else:
644 use_symlinks = True
645 group = parser.add_mutually_exclusive_group()
646 group.add_argument('--symlinks', default=use_symlinks,
647 action='store_true', dest='symlinks',
648 help='Try to use symlinks rather than copies, '
649 'when symlinks are not the default for '
650 'the platform.')
651 group.add_argument('--copies', default=not use_symlinks,
652 action='store_false', dest='symlinks',
653 help='Try to use copies rather than symlinks, '
654 'even when symlinks are the default for '
655 'the platform.')
656 parser.add_argument('--clear', default=False, action='store_true',
657 dest='clear', help='Delete the contents of the '
658 'environment directory if it '
659 'already exists, before '
660 'environment creation.')
661 parser.add_argument('--upgrade', default=False, action='store_true',
662 dest='upgrade', help='Upgrade the environment '
663 'directory to use this version '
664 'of Python, assuming Python '
665 'has been upgraded in-place.')
666 parser.add_argument('--without-pip', dest='with_pip',
667 default=True, action='store_false',
668 help='Skips installing or upgrading pip in the '
669 'virtual environment (pip is bootstrapped '
670 'by default)')
671 parser.add_argument('--prompt',
672 help='Provides an alternative prompt prefix for '
673 'this environment.')
674 parser.add_argument('--upgrade-deps', default=False, action='store_true',
675 dest='upgrade_deps',
676 help=f'Upgrade core dependencies ({", ".join(CORE_VENV_DEPS)}) '
677 'to the latest version in PyPI')
678 parser.add_argument('--without-scm-ignore-files', dest='scm_ignore_files',
679 action='store_const', const=frozenset(),
680 default=frozenset(['git']),
681 help='Skips adding SCM ignore files to the environment '
682 'directory (Git is supported by default).')
683 options = parser.parse_args(args)
684 if options.upgrade and options.clear:
685 raise ValueError('you cannot supply --upgrade and --clear together.')
686 builder = EnvBuilder(system_site_packages=options.system_site,
687 clear=options.clear,
688 symlinks=options.symlinks,
689 upgrade=options.upgrade,
690 with_pip=options.with_pip,
691 prompt=options.prompt,
692 upgrade_deps=options.upgrade_deps,
693 scm_ignore_files=options.scm_ignore_files)
694 for d in options.dirs:
695 builder.create(d)
696
697
698 if __name__ == '__main__':
699 rc = 1
700 try:
701 main()
702 rc = 0
703 except Exception as e:
704 print('Error: %s' % e, file=sys.stderr)
705 sys.exit(rc)