@@ -56,22 +56,25 @@ recursively and check whether all are installed in the current environment
56
56
57
57
.. code-block :: python
58
58
59
- # TODO Unless we get special permission, this snippet is Apache-2-licensed :
59
+ # Adapted from (see there for copyright & license) :
60
60
# https://github.com/HansBug/hbutils/blob/927b0757449a781ce8e30132f26b06089a24cd71/LICENSE
61
+ # SPDX-License-Identifier: Apache-2.0
61
62
62
63
from collections.abc import Iterable
63
64
from importlib.metadata import PackageNotFoundError, distribution, metadata
64
65
65
- from packaging.metadata import Metadata
66
+ from packaging.metadata import Metadata, RawMetadata
66
67
from packaging.requirements import Requirement
67
68
69
+
68
70
def check_reqs (req_strs : Iterable[str ]) -> bool :
69
71
return all (
70
72
_check_req_recursive(req)
71
73
for req_str in req_strs
72
74
if not (req := Requirement(req_str)).marker or req.marker.evaluate()
73
75
)
74
76
77
+
75
78
def _check_req_recursive (req : Requirement) -> bool :
76
79
try :
77
80
version = distribution(req.name).version
@@ -83,21 +86,23 @@ recursively and check whether all are installed in the current environment
83
86
84
87
req_metadata = Metadata.from_raw(metadata(req.name).json, validate = False )
85
88
for child_req in req_metadata.requires_dist or []:
86
- for extra in req.extras:
87
- if child_req.marker and child_req.marker.evaluate({" extra" : extra}):
88
- if not _check_req_recursive(child_req):
89
- return False
90
- break
89
+ # A dependency is only required to be present if ...
90
+ if (
91
+ not child_req.marker # ... it doesn't have a marker
92
+ or child_req.marker.evaluate() # ... its marker matches our env
93
+ or any ( # ... its marker matches our env given one of our extras
94
+ child_req.marker.evaluate({" extra" : extra}) for extra in req.extras
95
+ )
96
+ ):
97
+ if not _check_req_recursive(child_req):
98
+ return False
91
99
92
100
return True
93
101
94
102
95
103
# Perform check, e.g.:
96
104
extra_installed = check_reqs([" your-package[your-extra]" ])
97
105
98
- TODO Either point out that this snippet doesn't actually check everything
99
- (https://github.com/HansBug/hbutils/issues/109) or fix it.
100
-
101
106
The possibility of offering a helper function similar to ``check_reqs `` in
102
107
``importlib.metadata `` or ``packaging `` themselves is still being discussed
103
108
(`packaging-problems #317 <packaging-problems-317 _>`_).
0 commit comments