-
Notifications
You must be signed in to change notification settings - Fork 251
/
dependency.py
547 lines (448 loc) · 18.5 KB
/
dependency.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
from __future__ import annotations
import os
import re
import warnings
from contextlib import suppress
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Sequence
from typing import TypeVar
from packaging.utils import canonicalize_name
from poetry.core.constraints.generic import parse_constraint as parse_generic_constraint
from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.specification import PackageSpecification
from poetry.core.packages.utils.utils import contains_group_without_marker
from poetry.core.packages.utils.utils import create_nested_marker
from poetry.core.packages.utils.utils import normalize_python_version_markers
from poetry.core.version.markers import parse_marker
if TYPE_CHECKING:
from collections.abc import Iterable
from packaging.utils import NormalizedName
from poetry.core.constraints.version import VersionConstraint
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.version.markers import BaseMarker
T = TypeVar("T", bound="Dependency")
class Dependency(PackageSpecification):
def __init__(
self,
name: str,
constraint: str | VersionConstraint,
optional: bool = False,
groups: Iterable[str] | None = None,
allows_prereleases: bool = False,
extras: Iterable[str] | None = None,
source_type: str | None = None,
source_url: str | None = None,
source_reference: str | None = None,
source_resolved_reference: str | None = None,
source_subdirectory: str | None = None,
) -> None:
from poetry.core.version.markers import AnyMarker
super().__init__(
name,
source_type=source_type,
source_url=source_url,
source_reference=source_reference,
source_resolved_reference=source_resolved_reference,
source_subdirectory=source_subdirectory,
features=extras,
)
# Attributes must be immutable for clone() to be safe!
# (For performance reasons, clone only creates a copy instead of a deep copy).
self._constraint: VersionConstraint
self._pretty_constraint: str
self.constraint = constraint # type: ignore[assignment]
self._optional = optional
if not groups:
groups = [MAIN_GROUP]
self._groups = frozenset(groups)
self._allows_prereleases = allows_prereleases
# "_develop" is only required for enriching [project] dependencies
self._develop = False
self._python_versions = "*"
self._python_constraint = parse_constraint("*")
self._transitive_python_versions: str | None = None
self._transitive_python_constraint: VersionConstraint | None = None
self._transitive_marker: BaseMarker | None = None
self._in_extras: Sequence[NormalizedName] = []
self._activated = not self._optional
self.is_root = False
self._marker: BaseMarker = AnyMarker()
self.source_name: str | None = None
@property
def name(self) -> NormalizedName:
return self._name
@property
def constraint(self) -> VersionConstraint:
return self._constraint
@constraint.setter
def constraint(self, constraint: str | VersionConstraint) -> None:
if isinstance(constraint, str):
self._constraint = parse_constraint(constraint)
else:
self._constraint = constraint
self._pretty_constraint = str(constraint)
@property
def pretty_constraint(self) -> str:
return self._pretty_constraint
@property
def pretty_name(self) -> str:
return self._pretty_name
@property
def groups(self) -> frozenset[str]:
return self._groups
@property
def python_versions(self) -> str:
return self._python_versions
@python_versions.setter
def python_versions(self, value: str) -> None:
self._python_versions = value
self._python_constraint = parse_constraint(value)
if not self._python_constraint.is_any():
self._marker = self._marker.intersect(
parse_marker(
create_nested_marker("python_version", self._python_constraint)
)
)
@property
def transitive_python_versions(self) -> str:
warnings.warn(
"'transitive_python_versions' is deprecated and will be removed.",
DeprecationWarning,
stacklevel=2,
)
if self._transitive_python_versions is None:
return self._python_versions
return self._transitive_python_versions
@transitive_python_versions.setter
def transitive_python_versions(self, value: str) -> None:
warnings.warn(
"'transitive_python_versions' is deprecated and will be removed.",
DeprecationWarning,
stacklevel=2,
)
self._transitive_python_versions = value
self._transitive_python_constraint = parse_constraint(value)
@property
def marker(self) -> BaseMarker:
return self._marker
@marker.setter
def marker(self, marker: str | BaseMarker) -> None:
from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.utils.utils import convert_markers
from poetry.core.version.markers import BaseMarker
from poetry.core.version.markers import parse_marker
if not isinstance(marker, BaseMarker):
marker = parse_marker(marker)
self._marker = marker
markers = convert_markers(marker)
if "extra" in markers:
# If we have extras, the dependency is optional
self.deactivate()
new_in_extras = []
for or_ in markers["extra"]:
for op, extra in or_:
if op == "==":
new_in_extras.append(canonicalize_name(extra))
elif op == "" and "||" in extra:
for _extra in extra.split(" || "):
new_in_extras.append(canonicalize_name(_extra))
self._in_extras = [*self._in_extras, *new_in_extras]
# Recalculate python versions.
self._python_versions = "*"
if not contains_group_without_marker(markers, "python_version"):
python_version_markers = markers["python_version"]
self._python_versions = normalize_python_version_markers(
python_version_markers
)
self._python_constraint = parse_constraint(self._python_versions)
@property
def transitive_marker(self) -> BaseMarker:
if self._transitive_marker is None:
return self.marker
return self._transitive_marker
@transitive_marker.setter
def transitive_marker(self, value: BaseMarker) -> None:
self._transitive_marker = value
@property
def python_constraint(self) -> VersionConstraint:
return self._python_constraint
@property
def transitive_python_constraint(self) -> VersionConstraint:
warnings.warn(
"'transitive_python_constraint' is deprecated and will be removed.",
DeprecationWarning,
stacklevel=2,
)
if self._transitive_python_constraint is None:
return self._python_constraint
return self._transitive_python_constraint
@property
def extras(self) -> frozenset[NormalizedName]:
# extras activated in a dependency is the same as features
return self._features
@property
def in_extras(self) -> Sequence[NormalizedName]:
return self._in_extras
@property
def base_pep_508_name(self) -> str:
from poetry.core.constraints.version import Version
from poetry.core.constraints.version import VersionUnion
requirement = self.complete_pretty_name
constraint = self.constraint
if isinstance(constraint, VersionUnion):
if (
constraint.excludes_single_version
or constraint.excludes_single_wildcard_range
):
# This branch is a short-circuit logic for special cases and
# avoids having to split and parse constraint again. This has
# no functional difference with the logic in the else branch.
requirement += f" ({constraint})"
else:
constraints = ",".join(
str(parse_constraint(c)) for c in self.pretty_constraint.split(",")
)
requirement += f" ({constraints})"
elif isinstance(constraint, Version):
requirement += f" (=={constraint.text})"
elif not constraint.is_any():
requirement += f" ({str(constraint).replace(' ', '')})"
return requirement
@property
def base_pep_508_name_resolved(self) -> str:
return self.base_pep_508_name
def allows_prereleases(self) -> bool:
return self._allows_prereleases
def is_optional(self) -> bool:
return self._optional
def is_activated(self) -> bool:
return self._activated
def is_vcs(self) -> bool:
return False
def is_file(self) -> bool:
return False
def is_directory(self) -> bool:
return False
def is_url(self) -> bool:
return False
def to_pep_508(self, with_extras: bool = True, *, resolved: bool = False) -> str:
from poetry.core.packages.utils.utils import convert_markers
if resolved:
requirement = self.base_pep_508_name_resolved
else:
requirement = self.base_pep_508_name
markers = []
has_extras = False
if not self.marker.is_any():
marker = self.marker
if not with_extras:
marker = marker.without_extras()
# we re-check for any marker here since the without extra marker might
# return an any marker again
if not (marker.is_empty() or marker.is_any()):
markers.append(str(marker))
has_extras = "extra" in convert_markers(marker)
else:
# Python marker
if self.python_versions != "*":
python_constraint = self.python_constraint
markers.append(
create_nested_marker("python_version", python_constraint)
)
in_extras = " || ".join(self._in_extras)
if in_extras and with_extras and not has_extras:
markers.append(
create_nested_marker("extra", parse_generic_constraint(in_extras))
)
if markers:
if len(markers) > 1:
marker_str = " and ".join(f"({m})" for m in markers)
else:
marker_str = markers[0]
requirement += f" ; {marker_str}"
return requirement
def activate(self) -> None:
"""
Set the dependency as mandatory.
"""
self._activated = True
def deactivate(self) -> None:
"""
Set the dependency as optional.
"""
if not self._optional:
self._optional = True
self._activated = False
def with_constraint(self: T, constraint: str | VersionConstraint) -> T:
dependency = self.clone()
dependency.constraint = constraint # type: ignore[assignment]
return dependency
@classmethod
def create_from_pep_508(
cls, name: str, relative_to: Path | None = None
) -> Dependency:
"""
Resolve a PEP-508 requirement string to a `Dependency` instance. If a
`relative_to` path is specified, this is used as the base directory if the
identified dependency is of file or directory type.
"""
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.utils.link import Link
from poetry.core.packages.utils.utils import is_archive_file
from poetry.core.packages.utils.utils import is_python_project
from poetry.core.packages.utils.utils import is_url
from poetry.core.packages.utils.utils import path_to_url
from poetry.core.packages.utils.utils import strip_extras
from poetry.core.packages.utils.utils import url_to_path
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.utils.patterns import wheel_file_re
from poetry.core.vcs.git import ParsedUrl
from poetry.core.version.requirements import Requirement
# Removing comments
parts = name.split(" #", 1)
name = parts[0].strip()
if len(parts) > 1:
rest = parts[1]
if " ;" in rest:
name += " ;" + rest.split(" ;", 1)[1]
req = Requirement(name)
name = req.name
link = None
if is_url(name):
link = Link(name)
elif req.url:
link = Link(req.url)
else:
path_str = os.path.normpath(os.path.abspath(name))
p, extras = strip_extras(path_str)
if os.path.isdir(p) and (os.path.sep in name or name.startswith(".")):
if not is_python_project(Path(name)):
raise ValueError(
f"Directory {name!r} is not installable. Not a Python project."
)
link = Link(path_to_url(p))
elif is_archive_file(p):
link = Link(path_to_url(p))
# it's a local file, dir, or url
if link:
is_file_uri = link.scheme == "file"
is_relative_uri = is_file_uri and re.search(r"\.\./", link.url)
# Handle relative file URLs
if is_file_uri and is_relative_uri:
path = Path(link.path)
if relative_to:
path = relative_to / path
link = Link(path_to_url(path))
# wheel file
version = None
if link.is_wheel:
m = wheel_file_re.match(link.filename)
if not m:
raise ValueError(f"Invalid wheel name: {link.filename}")
name = m.group("name")
version = m.group("ver")
dep: Dependency | None = None
if link.scheme.startswith("git+"):
url = ParsedUrl.parse(link.url)
dep = VCSDependency(
name,
"git",
url.url,
rev=url.rev,
directory=url.subdirectory,
extras=req.extras,
)
elif link.scheme == "git":
dep = VCSDependency(
name, "git", link.url_without_fragment, extras=req.extras
)
elif link.scheme in ("http", "https"):
dep = URLDependency(
name,
link.url_without_fragment,
directory=link.subdirectory_fragment,
extras=req.extras,
)
elif is_file_uri:
# handle RFC 8089 references
path = url_to_path(req.url)
dep = _make_file_or_dir_dep(
name=name,
path=path,
base=relative_to,
subdirectory=link.subdirectory_fragment,
extras=req.extras,
)
else:
with suppress(ValueError):
# this is a local path not using the file URI scheme
dep = _make_file_or_dir_dep(
name=name,
path=Path(req.url),
base=relative_to,
extras=req.extras,
)
if dep is None:
dep = Dependency(name, version or "*", extras=req.extras)
if version:
dep._constraint = parse_constraint(version)
else:
constraint: VersionConstraint | str
constraint = req.constraint if req.pretty_constraint else "*"
dep = Dependency(name, constraint, extras=req.extras)
if req.marker:
dep.marker = req.marker
return dep
def __eq__(self, other: object) -> bool:
if not isinstance(other, Dependency):
return NotImplemented
# "constraint" is implicitly given for direct origin dependencies and might not
# be set yet ("*"). Thus, it shouldn't be used to determine if two direct origin
# dependencies are equal.
# Calling is_direct_origin() for one dependency is sufficient because
# super().__eq__() returns False for different origins.
return super().__eq__(other) and (
self._constraint == other.constraint or self.is_direct_origin()
)
def __hash__(self) -> int:
# don't include _constraint in hash because it is mutable!
return super().__hash__()
def __str__(self) -> str:
if self.is_root:
return self._pretty_name
if self.is_direct_origin():
# adding version since this information is especially useful in debug output
parts = [p.strip() for p in self.base_pep_508_name.split("@", 1)]
return f"{parts[0]} ({self._pretty_constraint}) @ {parts[1]}"
return self.base_pep_508_name
def __repr__(self) -> str:
return f"<{self.__class__.__name__} {self}>"
def _make_file_or_dir_dep(
name: str,
path: Path,
base: Path | None = None,
subdirectory: str | None = None,
extras: list[str] | None = None,
) -> FileDependency | DirectoryDependency | None:
"""
Helper function to create a file or directoru dependency with the given arguments.
If path is not a file or directory that exists, `None` is returned.
"""
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
_path = path
if not path.is_absolute() and base:
# a base path was specified, so we should respect that
_path = Path(base) / path
if _path.is_file():
return FileDependency(
name, path, base=base, directory=subdirectory, extras=extras
)
elif _path.is_dir():
if subdirectory:
path = path / subdirectory
return DirectoryDependency(name, path, base=base, extras=extras)
return None