beffroi-reporting/scripts/check-legacy-parity.py

118 lines
7.6 KiB
Python

#!/usr/bin/env python3
"""Compare a publication with the original Typst renderer's 17 PDFs."""
import hashlib
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
from pypdf import PdfReader
ROOT = Path(__file__).resolve().parents[1]
REFERENCE = ROOT / "legacy/typst_reporter/build"
EXPECTED = {
"reports": {
"2026-09-04-clevo-handoff": (3, "a75ef856603ee60c232ee2297c1c6c7a804a5cbd70a39df6c3e17a2766da3db4"),
"2026-09-04-clevo-initial": (4, "e3f85a7ddaacd66b59c0e145b0986fbcca0e507670b03ef9f99dbe45aded6c83"),
"2026-09-04-report": (2, "077c992c3b224c93433d15a8acf2b1c170b78579a6803eca4bf29d3901bcc3d0"),
"2026-09-14-system-stability-diagnostic-notes": (9, "4d0c08493a152989b74c7632690f1c3483814f6232bc49e12afd4ccc50a55689"),
"2026-09-15-clevo-diagnostic-synthesis": (3, "48003a50448105c225c946dd89740b208d59fc2b8cc58b1b5c379d1b00173c96"),
"2026-09-15-clevo-manufacturer-report": (4, "d5747b6f8ad98f17327cda38c22127dfb045adafa4b31cc68760e767fd643c46"),
"2026-09-15-clevo-system-stability-diagnostic-notes": (9, "649c034fc99af8e0e42a9b81e52508313936bc09683283c5bcec0287a6f9b3af"),
},
"diagnostics": {
"hardware-17-tA6kwqAg-windows-system-report-desktop-2osf051-20260904-134702": (8, "2cd2b1c73554ac8a58831f68f97bd7e83c66f05c9e0af5a1c6ee893169b8deab"),
"hardware-18-WHA8oUN2-windows-system-report-pcvid008-20260904-134729": (8, "7d74b28a2a7f615ff301691ea75dcb3cee3c4457ee6bf3210ef91c174b500a89"),
"hardware-19-BOOso6wz-windows-system-report-desktop-dh4hot6-20260904-173202": (8, "b5020fb8102114a26991d93cf2b19e413b08b078ca4f3a5a26be36b4dd18d7d6"),
"hardware-20-dlBVl76W-windows-system-report-desktop-pr71lgi-20260904-162618": (8, "4bd0c60bc165a0b062e5684a3032d871201e79d52d38eab8d7ad917e5f7bb60a"),
"hardware-35-NSmkLXTb-windows-system-report-blin2-20260904-161637": (8, "f128fed238a09496379a9802eeb3bcb1608aa239084218034ecbfe32b234531a"),
"hardware-51-6M9gNOPR-windows-system-report-desktop-sau019h-20260904-164514": (8, "def902addf516a26e6db086b3a89092329ab97f380434074954201bd6da00393"),
"hardware-53-AbCX84Q8-windows-system-report-desktop-3pptg49-20260904-135829": (8, "a0f4eb6d422511aa196886693dd5bc7a34cfa4a84ffef420ffc8dcbb0cee43ce"),
"windows_system_report_DESKTOP-SAU019H_20260914_163753": (8, "ddfec97f68fe5e8498be384a7b1a6291c9804735e9b52b1c99e6116592f99aff"),
"windows_system_report_DESKTOP-SAU019H_20260915_113124": (8, "c3bbaf37cefc8683d1cbb20797decf793ba98d7b0d70df8651f81b4182cb5169"),
"windows_system_report_DESKTOP-SAU019H_20260915_121057": (12, "38f8eca734f8891a5fae608c6ca27090fad1b5a50be3490402b43b4955b541e3"),
},
}
# Extracted from the original PDFs using pypdf 6.7.5, layout mode. This is the
# portable CI oracle; Poppler checks below remain available when installed.
EXPECTED_PYPDF = {
"reports": {
"2026-09-04-clevo-handoff": "e479a341bb3282ae85e0c18bc839c5f679704f92c9a332f9793a47407eac2e8d",
"2026-09-04-clevo-initial": "5286ec509e8a77bf77221e19dc92adfd6e85056b1e8db79896a92947cc2a58b9",
"2026-09-04-report": "637fa32246969b6410a687b255ac83ab2fc93e577f88a8bed2f489aa2d1a2a41",
"2026-09-14-system-stability-diagnostic-notes": "566e811d71dcfbe46161d767d92d0a3b7bfd909bec041259724a932bc5733300",
"2026-09-15-clevo-diagnostic-synthesis": "f005a0179181c5d986be478bae4fe640a4f363f46bb157977fb6038cc5390c4d",
"2026-09-15-clevo-manufacturer-report": "614f9f2a2cb77075d65549185298d101610b90a314098ff34b43813b09c60863",
"2026-09-15-clevo-system-stability-diagnostic-notes": "49ed67cfb4c6186b0b6cc6a56d8182e437553a6c878ed8b90e3299f2fb67560e",
},
"diagnostics": {
"hardware-17-tA6kwqAg-windows-system-report-desktop-2osf051-20260904-134702": "b8c8d9f9118b27fcd9c55825c7d36e3d06f9d4487a8960cfdd058705781c5a0a",
"hardware-18-WHA8oUN2-windows-system-report-pcvid008-20260904-134729": "8220f7515481afb9e387e9941448659762a488e139c4dd612156ad63ee7d7e64",
"hardware-19-BOOso6wz-windows-system-report-desktop-dh4hot6-20260904-173202": "848ff93eb3d7750e18c2665d22b3655e63989b6386cdad0c958e83afd7c0f2c7",
"hardware-20-dlBVl76W-windows-system-report-desktop-pr71lgi-20260904-162618": "e9c40150192191a248d936b6af4b18070e4cc7d03655599280f7f9597f737f95",
"hardware-35-NSmkLXTb-windows-system-report-blin2-20260904-161637": "610875982bdd33cdcb4434b6ad01a7df9ade7fb783134dbf0cf98741d8d6c38f",
"hardware-51-6M9gNOPR-windows-system-report-desktop-sau019h-20260904-164514": "31a22a61c6740af88507035cfeef6479a4526fe05e724aa346f8309a8de89e94",
"hardware-53-AbCX84Q8-windows-system-report-desktop-3pptg49-20260904-135829": "9e2c596d85c222bdd31ba2a4d73a7aecaec7abb090eaa935d53ed66ebc907490",
"windows_system_report_DESKTOP-SAU019H_20260914_163753": "70019c92e00c0fb2c0aad31338744670ec5c873140b1927fe94df7edae69dd3a",
"windows_system_report_DESKTOP-SAU019H_20260915_113124": "7e86da7c5532fb2a91b2dd93a96f58468ab881a7cd890b0606fa58f3c8073c74",
"windows_system_report_DESKTOP-SAU019H_20260915_121057": "5dc7281fa5f2be08e968cae9df408d50a26d2f0ee6236d31d0ea2b708d91b3a4",
},
}
def portable_pdf_info(pdf):
reader = PdfReader(pdf)
text = "\n".join(page.extract_text(extraction_mode="layout") for page in reader.pages)
return len(reader.pages), hashlib.sha256(text.encode()).hexdigest()
def text_hash(pdf):
return hashlib.sha256(subprocess.check_output(["pdftotext", "-layout", str(pdf), "-"])).hexdigest()
def raster_equal(reference, current):
with tempfile.TemporaryDirectory(prefix="beffroi-raster-parity.") as directory:
subprocess.run(["pdftoppm", "-scale-to", "900", str(reference), f"{directory}/old"], check=True, stdout=subprocess.DEVNULL)
subprocess.run(["pdftoppm", "-scale-to", "900", str(current), f"{directory}/new"], check=True, stdout=subprocess.DEVNULL)
old = sorted(Path(directory).glob("old-*.ppm"))
new = sorted(Path(directory).glob("new-*.ppm"))
return len(old) == len(new) and all(a.read_bytes() == b.read_bytes() for a, b in zip(old, new))
def main():
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} BUILD_ROOT", file=sys.stderr)
return 2
build = Path(sys.argv[1])
check_poppler_text = shutil.which("pdftotext") is not None
check_raster = REFERENCE.is_dir() and shutil.which("pdftoppm") is not None
for category, documents in EXPECTED.items():
folder = build / category / "pdfs" if category == "reports" else build / category
actual = {path.stem: path for path in folder.glob("*.pdf")}
missing = set(documents) - set(actual)
if missing:
print(f"{category}: missing original PDFs: {', '.join(sorted(missing))}", file=sys.stderr)
return 1
for name, (wanted_pages, wanted_text) in documents.items():
pdf = actual[name]
if portable_pdf_info(pdf) != (wanted_pages, EXPECTED_PYPDF[category][name]):
print(f"{category}/{name}: page count or text differs", file=sys.stderr)
return 1
if check_poppler_text and text_hash(pdf) != wanted_text:
print(f"{category}/{name}: Poppler text differs", file=sys.stderr)
return 1
if check_raster:
reference = REFERENCE / ("diagnostics" if category == "diagnostics" else "") / f"{name}.pdf"
if not raster_equal(reference, pdf):
print(f"{category}/{name}: rendered pages differ", file=sys.stderr)
return 1
print("All 17 PDFs match original text and pagination" + (" and rendered pages" if check_raster else ""))
return 0
if __name__ == "__main__":
raise SystemExit(main())