|
| 1 | +# Copyright (c) 2015-2026 Vector 35 Inc |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to |
| 5 | +# deal in the Software without restriction, including without limitation the |
| 6 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +# IN THE SOFTWARE. |
| 20 | + |
| 21 | + |
| 22 | +import json |
| 23 | +from typing import Dict, Iterable, List, Optional |
| 24 | + |
| 25 | + |
| 26 | +def pip_requirements_from_dependency_metadata(dependencies: bytes) -> List[str]: |
| 27 | + raw_text = dependencies.decode("utf-8") |
| 28 | + try: |
| 29 | + # Dependencies might be specified in JSON format, which we need to translate to text. |
| 30 | + dependencies_json = json.loads(raw_text) |
| 31 | + dependency_text = dependencies_json.get("pip", "") |
| 32 | + except json.JSONDecodeError: |
| 33 | + # If we can't parse input as JSON, it's probably already in text format. |
| 34 | + dependency_text = raw_text |
| 35 | + return [line.split('#', 1)[0].strip() for line in dependency_text.split('\n') if line.split('#', 1)[0].strip()] |
| 36 | + |
| 37 | + |
| 38 | +def pip_requirements_satisfied(requirements: Iterable[str], installed_versions: Optional[Dict[str, str]] = None) -> bool: |
| 39 | + try: |
| 40 | + from importlib import metadata as importlib_metadata |
| 41 | + from packaging.requirements import Requirement |
| 42 | + from packaging.utils import canonicalize_name |
| 43 | + except Exception: |
| 44 | + from importlib import metadata as importlib_metadata |
| 45 | + from pip._vendor.packaging.requirements import Requirement |
| 46 | + from pip._vendor.packaging.utils import canonicalize_name |
| 47 | + |
| 48 | + if installed_versions is None: |
| 49 | + installed_versions = {} |
| 50 | + for dist in importlib_metadata.distributions(): |
| 51 | + name = dist.metadata.get("Name") |
| 52 | + if name: |
| 53 | + installed_versions[canonicalize_name(name)] = dist.version |
| 54 | + else: |
| 55 | + installed_versions = {canonicalize_name(name): version for name, version in installed_versions.items()} |
| 56 | + |
| 57 | + for requirement_text in requirements: |
| 58 | + requirement = Requirement(requirement_text) |
| 59 | + if requirement.marker is not None and not requirement.marker.evaluate(): |
| 60 | + continue |
| 61 | + |
| 62 | + version = installed_versions.get(canonicalize_name(requirement.name)) |
| 63 | + if version is None: |
| 64 | + return False |
| 65 | + if requirement.specifier and not requirement.specifier.contains(version, prereleases=True): |
| 66 | + return False |
| 67 | + |
| 68 | + return True |
0 commit comments