Debian Codegraph @

src/build/_util.py

raw
1 from __future__ import annotations
2
3
4 __lazy_modules__ = [
5 f'{__spec__.parent}._compat',
6 'packaging',
7 'packaging.requirements',
8 ]
9
10 import sys
11
12 import packaging.requirements
13
14 from ._compat import importlib
15
16
17 TYPE_CHECKING = False
18
19 if TYPE_CHECKING:
20 from collections.abc import Iterator
21 from collections.abc import Set as AbstractSet
22
23
24 def check_dependency(
25 req_string: str, ancestral_req_strings: tuple[str, ...] = (), parent_extras: AbstractSet[str] = frozenset()
26 ) -> Iterator[tuple[str, ...]]:
27 """
28 Verify that a dependency and all of its dependencies are met.
29
30 :param req_string: Requirement string
31 :param parent_extras: Extras (eg. "test" in myproject[test])
32 :yields: Unmet dependencies
33 """
34 yield from _check_dependency(req_string, ancestral_req_strings, parent_extras, set())
35
36
37 def _check_dependency(
38 req_string: str, ancestral_req_strings: tuple[str, ...], parent_extras: AbstractSet[str], seen: set[str]
39 ) -> Iterator[tuple[str, ...]]:
40 req = packaging.requirements.Requirement(req_string)
41 normalised_req_string = str(req)
42
43 # ``seen`` holds requirements already verified as fully satisfied (nothing
44 # yielded), so shared subtrees of a diamond dependency graph are walked once.
45 if normalised_req_string in seen:
46 return
47
48 # ``Requirement`` doesn't implement ``__eq__`` so we cannot compare reqs for
49 # equality directly but the string representation is stable.
50 if normalised_req_string in ancestral_req_strings:
51 # cyclical dependency, already checked.
52 return
53
54 if req.marker:
55 extras = frozenset(('',)).union(parent_extras)
56 # a requirement can have multiple extras but ``evaluate`` can
57 # only check one at a time.
58 if all(not req.marker.evaluate(environment={'extra': e}) for e in extras):
59 # if the marker conditions are not met, we pretend that the
60 # dependency is satisfied.
61 return
62
63 try:
64 dist = importlib.metadata.distribution(req.name)
65 except importlib.metadata.PackageNotFoundError:
66 # dependency is not installed in the environment.
67 yield (*ancestral_req_strings, normalised_req_string)
68 return
69
70 if req.specifier and not req.specifier.contains(dist.version, prereleases=True):
71 # the installed version is incompatible.
72 yield (*ancestral_req_strings, normalised_req_string)
73 return
74
75 satisfied = True
76 for other_req_string in dist.requires or ():
77 # yields transitive dependencies that are not satisfied.
78 for unmet in _check_dependency(other_req_string, (*ancestral_req_strings, normalised_req_string), req.extras, seen):
79 satisfied = False
80 yield unmet
81
82 # unmet requirements are not memoised: they must be reported once per
83 # dependency chain that reaches them.
84 if satisfied:
85 seen.add(normalised_req_string)
86
87
88 def format_unmet_dependencies(unmet: AbstractSet[tuple[str, ...]]) -> str:
89 body = ''.join(_format_missing_dependency(chain) for chain in sorted(unmet))
90 return f'Unmet dependencies (checked against {sys.executable}):{body}'
91
92
93 def _format_missing_dependency(dep_chain: tuple[str, ...]) -> str:
94 requirement = packaging.requirements.Requirement(dep_chain[-1])
95 try:
96 found = importlib.metadata.distribution(requirement.name).version
97 except importlib.metadata.PackageNotFoundError:
98 found = 'not installed'
99 wanted = str(requirement.specifier) if requirement.specifier else 'any'
100 return f'\n\t{_format_dep_chain(dep_chain)}\n\t\twanted: {wanted}\n\t\tfound: {found}'
101
102
103 def _format_dep_chain(dep_chain: tuple[str, ...]) -> str:
104 return ' -> '.join(dep.partition(';')[0].strip() for dep in dep_chain)