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

48 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Guard the seven authored PDFs against the pre-plugin release's extracted text."""
import hashlib
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parents[1]
TEXT_SHA256 = {
"2026-09-04-clevo-handoff": "6bb91e92caf442eaabacca61fbafca6b967bd4692877eee9efa5d3ff54686e5c",
"2026-09-04-clevo-initial": "2a05b6f93f8e5a5e0adf48fa2d83d3072fc404755654e8feb27eb5c592ec2ddf",
"2026-09-04-report": "e7dbaf7fa722fa18e333387f463622f7e082bd9a39dd785d5700965456a743ab",
"2026-09-14-system-stability-diagnostic-notes": "0fbe2dc9bd6044663c0bc026dc132eb69a7f8e4f8c4a9640914da9860aa9c11e",
"2026-09-15-clevo-diagnostic-synthesis": "c729ceca5c6e7481f16593af1113c94d03cee33d51af2556bd4c74a26155215a",
"2026-09-15-clevo-manufacturer-report": "01c522b83a14524bee1646c3f55c45bc0c96eec28696416054224958027d4d3d",
"2026-09-15-clevo-system-stability-diagnostic-notes": "32c88e56144aa15392e929a5d07a7f737e6397e37e65dae43d648429f8da1a7f",
}
def main():
if not shutil.which("pdftotext"):
print("pdftotext is required for regular report parity checks", file=sys.stderr)
return 2
inputs = {path.stem: path for path in (ROOT / "reports/reports").glob("*.yaml")}
if set(inputs) != set(TEXT_SHA256):
print("Authored report set changed; update the parity baseline deliberately", file=sys.stderr)
return 1
with tempfile.TemporaryDirectory(prefix="beffroi-regular-parity.") as directory:
for name, expected in TEXT_SHA256.items():
pdf = Path(directory) / f"{name}.pdf"
subprocess.run([str(ROOT / "reporter/render.sh"), str(inputs[name]), "--output", str(pdf)],
check=True, stdout=subprocess.DEVNULL)
extracted = subprocess.check_output(["pdftotext", "-layout", str(pdf), "-"])
actual = hashlib.sha256(extracted).hexdigest()
if actual != expected:
print(f"{name}: extracted text differs from pre-plugin PDF", file=sys.stderr)
return 1
print(f"All {len(TEXT_SHA256)} authored reports retain pre-plugin extracted text")
return 0
if __name__ == "__main__":
raise SystemExit(main())