#!/usr/bin/env python3 """Compare a publication with the original Typst renderer's 17 PDFs.""" import hashlib from pathlib import Path import subprocess import sys import tempfile 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"), }, } def pages(pdf): info = subprocess.check_output(["pdfinfo", str(pdf)], text=True) return int(next(line.split(":", 1)[1] for line in info.splitlines() if line.startswith("Pages:"))) 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_raster = REFERENCE.is_dir() 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")} if set(actual) != set(documents): print(f"{category}: PDF set differs from the original", file=sys.stderr) return 1 for name, (wanted_pages, wanted_text) in documents.items(): pdf = actual[name] if (pages(pdf), text_hash(pdf)) != (wanted_pages, wanted_text): print(f"{category}/{name}: page count or 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())