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