comparison env/lib/python3.9/site-packages/virtualenv/create/via_global_ref/api.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from __future__ import absolute_import, unicode_literals
2
3 import logging
4 import os
5 from abc import ABCMeta
6
7 from six import add_metaclass
8
9 from virtualenv.info import fs_supports_symlink
10 from virtualenv.util.path import Path
11 from virtualenv.util.six import ensure_text
12
13 from ..creator import Creator, CreatorMeta
14
15
16 class ViaGlobalRefMeta(CreatorMeta):
17 def __init__(self):
18 super(ViaGlobalRefMeta, self).__init__()
19 self.copy_error = None
20 self.symlink_error = None
21 if not fs_supports_symlink():
22 self.symlink_error = "the filesystem does not supports symlink"
23
24 @property
25 def can_copy(self):
26 return not self.copy_error
27
28 @property
29 def can_symlink(self):
30 return not self.symlink_error
31
32
33 @add_metaclass(ABCMeta)
34 class ViaGlobalRefApi(Creator):
35 def __init__(self, options, interpreter):
36 super(ViaGlobalRefApi, self).__init__(options, interpreter)
37 self.symlinks = self._should_symlink(options)
38 self.enable_system_site_package = options.system_site
39
40 @staticmethod
41 def _should_symlink(options):
42 # Priority of where the option is set to follow the order: CLI, env var, file, hardcoded.
43 # If both set at same level prefers copy over symlink.
44 copies, symlinks = getattr(options, "copies", False), getattr(options, "symlinks", False)
45 copy_src, sym_src = options.get_source("copies"), options.get_source("symlinks")
46 for level in ["cli", "env var", "file", "default"]:
47 s_opt = symlinks if sym_src == level else None
48 c_opt = copies if copy_src == level else None
49 if s_opt is True and c_opt is True:
50 return False
51 if s_opt is True:
52 return True
53 if c_opt is True:
54 return False
55 return False # fallback to copy
56
57 @classmethod
58 def add_parser_arguments(cls, parser, interpreter, meta, app_data):
59 super(ViaGlobalRefApi, cls).add_parser_arguments(parser, interpreter, meta, app_data)
60 parser.add_argument(
61 "--system-site-packages",
62 default=False,
63 action="store_true",
64 dest="system_site",
65 help="give the virtual environment access to the system site-packages dir",
66 )
67 group = parser.add_mutually_exclusive_group()
68 if not meta.can_symlink and not meta.can_copy:
69 raise RuntimeError("neither symlink or copy method supported")
70 if meta.can_symlink:
71 group.add_argument(
72 "--symlinks",
73 default=True,
74 action="store_true",
75 dest="symlinks",
76 help="try to use symlinks rather than copies, when symlinks are not the default for the platform",
77 )
78 if meta.can_copy:
79 group.add_argument(
80 "--copies",
81 "--always-copy",
82 default=not meta.can_symlink,
83 action="store_true",
84 dest="copies",
85 help="try to use copies rather than symlinks, even when symlinks are the default for the platform",
86 )
87
88 def create(self):
89 self.install_patch()
90
91 def install_patch(self):
92 text = self.env_patch_text()
93 if text:
94 pth = self.purelib / "_virtualenv.pth"
95 logging.debug("create virtualenv import hook file %s", ensure_text(str(pth)))
96 pth.write_text("import _virtualenv")
97 dest_path = self.purelib / "_virtualenv.py"
98 logging.debug("create %s", ensure_text(str(dest_path)))
99 dest_path.write_text(text)
100
101 def env_patch_text(self):
102 """Patch the distutils package to not be derailed by its configuration files"""
103 with self.app_data.ensure_extracted(Path(__file__).parent / "_virtualenv.py") as resolved_path:
104 text = resolved_path.read_text()
105 return text.replace('"__SCRIPT_DIR__"', repr(os.path.relpath(str(self.script_dir), str(self.purelib))))
106
107 def _args(self):
108 return super(ViaGlobalRefApi, self)._args() + [("global", self.enable_system_site_package)]
109
110 def set_pyenv_cfg(self):
111 super(ViaGlobalRefApi, self).set_pyenv_cfg()
112 self.pyenv_cfg["include-system-site-packages"] = "true" if self.enable_system_site_package else "false"