Files
VerseVDI-Protocol/tools/fixture_digest.py
T

55 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Check the content-addressed Phase 3A conformance corpus."""
from __future__ import annotations
import hashlib
import json
import pathlib
import sys
ROOT = pathlib.Path(__file__).resolve().parents[1]
MANIFEST = ROOT / "fixtures/manifest.json"
def digest(paths: list[str]) -> str:
value = hashlib.sha256()
for relative in paths:
path = ROOT / relative
value.update(relative.encode("utf-8"))
value.update(b"\0")
value.update(path.read_bytes())
value.update(b"\0")
return value.hexdigest()
def main() -> int:
manifest = json.loads(MANIFEST.read_text(encoding="utf-8"))
paths = sorted(path.relative_to(ROOT).as_posix() for path in (ROOT / "fixtures/conformance").glob("*.tsv"))
if paths != manifest.get("files"):
raise ValueError("fixture manifest file list is stale")
actual = digest(paths)
expected = manifest.get("corpus_sha256")
if not expected or actual != expected:
raise ValueError(f"fixture corpus hash mismatch: {actual}")
json_paths = sorted(
path.relative_to(ROOT).as_posix()
for directory in (ROOT / "fixtures/valid", ROOT / "fixtures/invalid")
for path in directory.glob("*.json")
)
if json_paths != manifest.get("json_files"):
raise ValueError("JSON fixture manifest file list is stale")
json_actual = digest(json_paths)
if json_actual != manifest.get("json_corpus_sha256"):
raise ValueError(f"JSON fixture corpus hash mismatch: {json_actual}")
print(f"Fixture corpus SHA256 {actual}; JSON SHA256 {json_actual}")
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except (OSError, ValueError, json.JSONDecodeError) as exc:
print(f"fixture_digest: {exc}", file=sys.stderr)
raise SystemExit(1)