Skip to content

Commit 349ddca

Browse files
authored
tasks: set up harness for installing packages, itest for caching ChromeDriver (#7)
1 parent 0d0025b commit 349ddca

File tree

8 files changed

+155
-7
lines changed

8 files changed

+155
-7
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "submodules/html2pdf"]
22
path = submodules/html2pdf
3-
url = git@github.com:mettta/html2pdf.git
3+
url = https://github.com/mettta/html2pdf.git

developer/pip_install_hpdf_deps.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
)

hpdf/hpdf.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def find_driver(self, driver: Driver):
7575
browser_version = self._os_system_manager.get_browser_version_from_os(
7676
browser_type
7777
)
78+
assert browser_version is not None, browser_version
7879

7980
path_to_cached_chrome_driver_dir = os.path.join(
8081
path_to_cached_chrome_driver_dir, browser_version, os_type
@@ -84,18 +85,18 @@ def find_driver(self, driver: Driver):
8485
)
8586
if os.path.isfile(path_to_cached_chrome_driver):
8687
print( # noqa: T201
87-
f"html2pdf: chromedriver exists in StrictDoc's local cache: "
88+
f"html2pdf: ChromeDriver exists in the local cache: "
8889
f"{path_to_cached_chrome_driver}"
8990
)
9091
return path_to_cached_chrome_driver
9192
print( # noqa: T201
92-
f"html2pdf: chromedriver does not exist in StrictDoc's local cache: "
93+
f"html2pdf: ChromeDriver does not exist in the local cache: "
9394
f"{path_to_cached_chrome_driver}"
9495
)
9596
path_to_downloaded_chrome_driver = super().find_driver(driver)
9697
if path_to_downloaded_chrome_driver is None:
9798
print( # noqa: T201
98-
f"html2pdf: could not get a downloaded Chrome driver: "
99+
f"html2pdf: could not get a downloaded ChromeDriver: "
99100
f"{path_to_cached_chrome_driver}"
100101
)
101102
return None
@@ -241,7 +242,7 @@ def main():
241242
parser.add_argument(
242243
"--cache-dir",
243244
type=str,
244-
help="Optional path to a cache directory whereto the Chrome driver is downloaded.",
245+
help="Optional path to a cache directory whereto the ChromeDriver is downloaded.",
245246
)
246247
parser.add_argument("paths", nargs='+', help="Paths to input HTML file.")
247248
args = parser.parse_args()
@@ -253,7 +254,7 @@ def main():
253254
if args.cache_dir is not None
254255
else (
255256
os.path.join(
256-
tempfile.gettempdir(), "strictdoc_cache", "chromedriver"
257+
Path.home(), ".hpdf", "chromedriver"
257258
)
258259
)
259260
)

requirements.txt renamed to requirements.development.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
invoke
2+
toml
3+
packaging
4+
setuptools
25

36
#
47
# Integration tests

tasks.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def list_tasks(context):
5454

5555
@task
5656
def bootstrap(context):
57-
run_invoke(context, "pip install -r requirements.txt")
57+
run_invoke(context, "git submodule update --init --recursive")
58+
run_invoke(context, "pip install -r requirements.development.txt")
5859

5960

6061
@task
@@ -71,6 +72,11 @@ def format_readme(context):
7172
""")
7273

7374

75+
@task
76+
def lint(context):
77+
pass
78+
79+
7480
@task
7581
def test_integration(
7682
context,
@@ -86,8 +92,12 @@ def test_integration(
8692
focus_or_none = f"--filter {focus}" if focus else ""
8793
debug_opts = "-vv --show-all" if debug else ""
8894

95+
# For now, the --threads are set to 1 because running tests parallelized
96+
# will result in race conditions between Web Driver Manager downloading
97+
# ChromeDriver.
8998
itest_command = f"""
9099
lit
100+
--threads 1
91101
--param HTML2PDF_EXEC="{html2pdf_exec}"
92102
-v
93103
{debug_opts}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Test page</title>
7+
<script src="../../../submodules/html2pdf/dist/bundle.js"></script>
8+
<link rel="stylesheet" href="../../../submodules/html2pdf/test/shared/css/main.css">
9+
</head>
10+
11+
<body>
12+
<p>Hello world!</p>
13+
</body>
14+
15+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RUN: %html2pdf --cache-dir %S/Output/cache %S/index.html:%S/Output/index.pdf | filecheck %s --dump-input=fail --check-prefix CHECK-RUN1
2+
RUN: %check_exists --file "%S/Output/index.pdf"
3+
RUN: python %S/test.py
4+
5+
CHECK-RUN1: html2pdf: ChromeDriver does not exist in the local cache:
6+
7+
RUN: %html2pdf --cache-dir %S/Output/cache %S/index.html:%S/Output/index.pdf | filecheck %s --dump-input=fail --check-prefix CHECK-RUN2
8+
RUN: %check_exists --file "%S/Output/index.pdf"
9+
RUN: python %S/test.py
10+
11+
CHECK-RUN2: html2pdf: ChromeDriver exists in the local cache:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pypdf import PdfReader
2+
3+
reader = PdfReader("Output/index.pdf")
4+
5+
assert len(reader.pages) == 1
6+
7+
assert reader.pages[0].extract_text() == "Hello world!"

0 commit comments

Comments
 (0)