-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
installers.py
289 lines (251 loc) · 11.2 KB
/
installers.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
from __future__ import annotations
import io
import itertools
import json
import os
import shutil
import warnings
import zipfile
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING, Iterator
from installer._core import _determine_scheme, _process_WHEEL_file, install
from installer.destinations import SchemeDictionaryDestination
from installer.exceptions import InvalidWheelSource
from installer.records import RecordEntry
from installer.sources import WheelFile as _WheelFile
from installer.sources import _WheelFileValidationError
from pdm.compat import cached_property
from pdm.exceptions import PDMWarning
from pdm.installers.packages import CachedPackage
from pdm.termui import logger
from pdm.utils import fs_supports_symlink
if TYPE_CHECKING:
from typing import Any, BinaryIO, Callable, Iterable
from installer.destinations import Scheme
from installer.sources import WheelContentElement
from pdm.environments import BaseEnvironment
@lru_cache()
def _is_python_package(root: str | Path) -> bool:
for child in Path(root).iterdir():
if (
child.is_file()
and child.suffix in (".py", ".pyc", ".pyo", ".pyd")
or child.is_dir()
and _is_python_package(child)
):
return True
return False
_namespace_package_lines = frozenset(
[
# pkg_resources style
"__import__('pkg_resources').declare_namespace(__name__)",
"pkg_resources.declare_namespace(__name__)",
"declare_namespace(__name__)",
# pkgutil style
"__path__ = __import__('pkgutil').extend_path(__path__, __name__)",
"__path__ = pkgutil.extend_path(__path__, __name__)",
"__path__ = extend_path(__path__, __name__)",
]
)
_namespace_package_lines = _namespace_package_lines.union(line.replace("'", '"') for line in _namespace_package_lines)
@lru_cache()
def _is_namespace_package(root: str) -> bool:
if not _is_python_package(root):
return False
if not os.path.exists(os.path.join(root, "__init__.py")): # PEP 420 style
return True
with Path(root, "__init__.py").open(encoding="utf-8") as f:
init_py_lines = [line.strip() for line in f if line.strip() and not line.strip().startswith("#")]
return not _namespace_package_lines.isdisjoint(init_py_lines)
def _create_symlinks_recursively(source: str, destination: str) -> Iterable[str]:
"""Create symlinks recursively from source to destination. In the following ways:
package <-- link
__init__.py
namespace_package <-- mkdir
foo.py <-- link
bar.py <-- link
"""
is_top = True
for root, dirs, files in os.walk(source):
bn = os.path.basename(root)
if bn == "__pycache__" or bn.endswith(".dist-info"):
dirs[:] = []
continue
relpath = os.path.relpath(root, source)
destination_root = os.path.join(destination, relpath)
if is_top:
is_top = False
elif not _is_namespace_package(root):
# A package, create link for the parent dir and don't proceed
# for child directories
if os.path.exists(destination_root):
warnings.warn(f"Overwriting existing package: {destination_root}", PDMWarning, stacklevel=2)
if os.path.isdir(destination_root) and not os.path.islink(destination_root):
shutil.rmtree(destination_root)
else:
os.remove(destination_root)
os.symlink(root, destination_root, True)
yield relpath
dirs[:] = []
continue
# Otherwise, the directory is likely a namespace package,
# mkdir and create links for all files inside.
if not os.path.exists(destination_root):
os.makedirs(destination_root)
for f in files:
if f.endswith(".pyc"):
continue
source_path = os.path.join(root, f)
destination_path = os.path.join(destination_root, f)
if os.path.exists(destination_path):
os.remove(destination_path)
os.symlink(source_path, destination_path, False)
yield os.path.join(relpath, f)
class WheelFile(_WheelFile):
def __init__(self, f: zipfile.ZipFile) -> None:
super().__init__(f)
self.exclude: Callable[[WheelFile, WheelContentElement], bool] | None = None
self.additional_contents: Iterable[WheelContentElement] = []
@cached_property
def dist_info_dir(self) -> str:
namelist = self._zipfile.namelist()
try:
return next(name.split("/")[0] for name in namelist if name.split("/")[0].endswith(".dist-info"))
except StopIteration: # pragma: no cover
canonical_name = super().dist_info_dir
raise InvalidWheelSource(f"The wheel doesn't contain metadata {canonical_name!r}") from None
def get_contents(self) -> Iterator[WheelContentElement]:
for element in super().get_contents():
if self.exclude and self.exclude(self, element):
continue
yield element
yield from self.additional_contents
class InstallDestination(SchemeDictionaryDestination):
def __init__(self, *args: Any, symlink_to: str | None = None, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.symlink_to = symlink_to
def write_to_fs(self, scheme: Scheme, path: str | Path, stream: BinaryIO, is_executable: bool) -> RecordEntry:
target_path = os.path.join(self.scheme_dict[scheme], path)
if os.path.exists(target_path):
os.unlink(target_path)
return super().write_to_fs(scheme, path, stream, is_executable)
def finalize_installation(
self,
scheme: Scheme,
record_file_path: str | Path,
records: Iterable[tuple[Scheme, RecordEntry]],
) -> None:
if self.symlink_to:
# Create symlinks to the cached location
def _symlink_files(symlink_to: str) -> Iterator[tuple[Scheme, RecordEntry]]:
for relpath in _create_symlinks_recursively(symlink_to, self.scheme_dict[scheme]):
yield (scheme, RecordEntry(relpath.replace("\\", "/"), None, None))
records = itertools.chain(records, _symlink_files(self.symlink_to))
return super().finalize_installation(scheme, record_file_path, records)
def install_wheel(wheel: str, environment: BaseEnvironment, direct_url: dict[str, Any] | None = None) -> None:
"""Install a normal wheel file into the environment."""
additional_metadata = None
if direct_url is not None:
additional_metadata = {"direct_url.json": json.dumps(direct_url, indent=2).encode()}
destination = InstallDestination(
scheme_dict=environment.get_paths(),
interpreter=str(environment.interpreter.executable),
script_kind=_get_kind(environment),
)
_install_wheel(wheel=wheel, destination=destination, additional_metadata=additional_metadata)
def install_wheel_with_cache(
wheel: str, environment: BaseEnvironment, direct_url: dict[str, Any] | None = None
) -> None:
"""Only create .pth files referring to the cached package.
If the cache doesn't exist, create one.
"""
wheel_stem = Path(wheel).stem
cache_path = environment.project.cache("packages") / wheel_stem
package_cache = CachedPackage(cache_path)
interpreter = str(environment.interpreter.executable)
script_kind = _get_kind(environment)
use_symlink = environment.project.config["install.cache_method"] == "symlink" and fs_supports_symlink()
if not cache_path.is_dir():
logger.info("Installing wheel into cached location %s", cache_path)
cache_path.mkdir(exist_ok=True)
destination = InstallDestination(
scheme_dict=package_cache.scheme(),
interpreter=interpreter,
script_kind=script_kind,
)
_install_wheel(wheel=wheel, destination=destination)
additional_metadata = {"REFER_TO": package_cache.path.as_posix().encode()}
if direct_url is not None:
additional_metadata["direct_url.json"] = json.dumps(direct_url, indent=2).encode()
def skip_files(source: WheelFile, element: WheelContentElement) -> bool:
root_scheme = _process_WHEEL_file(source)
scheme, path = _determine_scheme(element[0][0], source, root_scheme)
return not (
scheme not in ("purelib", "platlib")
or path.split("/")[0].endswith(".dist-info")
# We need to skip the *-nspkg.pth files generated by setuptools'
# namespace_packages merchanism. See issue#623 for details
or not use_symlink
and path.endswith(".pth")
and not path.endswith("-nspkg.pth")
)
additional_contents: list[WheelContentElement] = []
lib_path = package_cache.scheme()["purelib"]
if not use_symlink:
# HACK: Prefix with aaa_ to make it processed as early as possible
filename = "aaa_" + wheel_stem.split("-")[0] + ".pth"
# use site.addsitedir() rather than a plain path to properly process .pth files
stream = io.BytesIO(f"import site;site.addsitedir({lib_path!r})\n".encode())
additional_contents.append(((filename, "", str(len(stream.getvalue()))), stream, False))
destination = InstallDestination(
scheme_dict=environment.get_paths(),
interpreter=interpreter,
script_kind=script_kind,
symlink_to=lib_path if use_symlink else None,
)
dist_info_dir = _install_wheel(
wheel=wheel,
destination=destination,
excludes=skip_files,
additional_contents=additional_contents,
additional_metadata=additional_metadata,
)
package_cache.add_referrer(dist_info_dir)
def _install_wheel(
wheel: str,
destination: InstallDestination,
excludes: Callable[[WheelFile, WheelContentElement], bool] | None = None,
additional_contents: Iterable[WheelContentElement] | None = None,
additional_metadata: dict[str, bytes] | None = None,
) -> str:
"""A lower level installation method that is copied from installer
but is controlled by extra parameters.
Return the .dist-info path
"""
with WheelFile.open(wheel) as source:
try:
source.validate_record()
except _WheelFileValidationError as e:
formatted_issues = "\n".join(e.issues)
warning = (
f"Validation of the RECORD file of {wheel} failed."
" Please report to the maintainers of that package so they can fix"
f" their build process. Details:\n{formatted_issues}\n"
)
warnings.warn(warning, PDMWarning, stacklevel=2)
root_scheme = _process_WHEEL_file(source)
source.exclude = excludes
if additional_contents:
source.additional_contents = additional_contents
install(source, destination, additional_metadata=additional_metadata or {})
return os.path.join(destination.scheme_dict[root_scheme], source.dist_info_dir)
def _get_kind(environment: BaseEnvironment) -> str:
if os.name != "nt":
return "posix"
is_32bit = environment.interpreter.is_32bit
# TODO: support win arm64
if is_32bit:
return "win-ia32"
else:
return "win-amd64"