|
| 1 | +import importlib.metadata as importlib_metadata |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | + |
| 7 | +import toml |
| 8 | +from packaging.requirements import Requirement |
| 9 | + |
| 10 | + |
| 11 | +class PackageNotFound(Exception): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class PackageVersionConflict(Exception): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +# A simplified version inspired by: |
| 20 | +# https://github.com/HansBug/hbutils/blob/37879186c489bced2791309c43d131f1703b7bd4/hbutils/system/python/package.py#L171 |
| 21 | +def check_if_package_installed(package_name: str): |
| 22 | + requirement: Requirement = Requirement(package_name) |
| 23 | + try: |
| 24 | + version = importlib_metadata.distribution(requirement.name).version |
| 25 | + except importlib_metadata.PackageNotFoundError: |
| 26 | + raise PackageNotFound(requirement) from None |
| 27 | + if not requirement.specifier.contains(version): |
| 28 | + raise PackageVersionConflict(version) |
| 29 | + |
| 30 | + |
| 31 | +print( # noqa: T201 |
| 32 | + "pip_install_hpdf_deps.py: " |
| 33 | + "checking if the current Python environment has all packages installed" |
| 34 | + ".", |
| 35 | + flush=True, |
| 36 | +) |
| 37 | + |
| 38 | +pyproject_content = toml.load("pyproject.toml") |
| 39 | + |
| 40 | + |
| 41 | +# The development dependencies are ignored, because they are managed in tox.ini. |
| 42 | +dependencies = pyproject_content["project"]["dependencies"] |
| 43 | + |
| 44 | +needs_installation = False |
| 45 | + |
| 46 | +for dependency in dependencies: |
| 47 | + try: |
| 48 | + check_if_package_installed(dependency) |
| 49 | + except PackageNotFound: |
| 50 | + print( # noqa: T201 |
| 51 | + f"pip_install_hpdf_deps.py: " |
| 52 | + f"Package is not installed: '{dependency}'.", |
| 53 | + flush=True, |
| 54 | + ) |
| 55 | + needs_installation = True |
| 56 | + break |
| 57 | + except PackageVersionConflict as exception_: |
| 58 | + print( # noqa: T201 |
| 59 | + ( |
| 60 | + f"pip_install_hpdf_deps.py: version conflict between " |
| 61 | + f"hpdf's requirement '{dependency}' " |
| 62 | + f"and the already installed package: " |
| 63 | + f"{exception_.args[0]}." |
| 64 | + ), |
| 65 | + flush=True, |
| 66 | + ) |
| 67 | + needs_installation = True |
| 68 | + break |
| 69 | + |
| 70 | +if not needs_installation: |
| 71 | + print( # noqa: T201 |
| 72 | + "pip_install_hpdf_deps.py: all packages seem to be installed.", |
| 73 | + flush=True, |
| 74 | + ) |
| 75 | + sys.exit(0) |
| 76 | + |
| 77 | +print( # noqa: T201 |
| 78 | + "pip_install_hpdf_deps.py: will install packages.", flush=True |
| 79 | +) |
| 80 | + |
| 81 | +all_packages = "\n".join(dependencies) + "\n" |
| 82 | + |
| 83 | +with tempfile.TemporaryDirectory() as tmp_dir: |
| 84 | + with open( |
| 85 | + os.path.join(tmp_dir, "requirements.txt"), "w", encoding="utf8" |
| 86 | + ) as tmp_requirements_txt_file: |
| 87 | + tmp_requirements_txt_file.write(all_packages) |
| 88 | + |
| 89 | + command = [ |
| 90 | + sys.executable, |
| 91 | + "-m", |
| 92 | + "pip", |
| 93 | + "install", |
| 94 | + "-r", |
| 95 | + tmp_requirements_txt_file.name, |
| 96 | + ] |
| 97 | + |
| 98 | + result = subprocess.run(command, check=True, encoding="utf8") |
| 99 | + print( # noqa: T201 |
| 100 | + f"'pip install' command exited with: {result.returncode}", flush=True |
| 101 | + ) |
0 commit comments