diff --git a/tools/pip_install_requirements.py b/tools/pip_install_requirements.py index 89c9deb..07024b9 100644 --- a/tools/pip_install_requirements.py +++ b/tools/pip_install_requirements.py @@ -1,8 +1,10 @@ import subprocess import sys -import pkg_resources -from pkg_resources import DistributionNotFound, Requirement, VersionConflict +from packaging.requirements import Requirement +import importlib +import importlib.metadata + from status import status if len(sys.argv) < 2: @@ -38,12 +40,25 @@ def pip(req_files): sys.exit(retcode) +class IncompatibleVersionError(Exception): + pass + try: with status("Verifying Python package dependencies"): - pkg_resources.working_set.resolve( - [Requirement.parse(r.split("#egg=")[-1]) for r in requirements] - ) + for rspec in requirements: + req = Requirement(rspec.strip().split("#egg=")[-1].replace('==', '~=')) + name = req.name + version_specifier = req.specifier + version_installed = importlib.metadata.version(name) + + if not version_specifier.contains(version_installed): + raise IncompatibleVersionError(f'Need {name} {version_specifier} but found {version_installed}') + +except importlib.metadata.PackageNotFoundError: # + print(f'[!] Package `{name}` not found; refreshing dependencies') +except IncompatibleVersionError as e: + print(f'[!] {e}') +else: + sys.exit(0) -except (DistributionNotFound, VersionConflict) as e: - print(e.report()) - pip(all_req_files) +pip(all_req_files)