#!/usr/bin/env python3 """Rebuild the exact, test-only contact package and its derived review fixtures.""" from __future__ import annotations import hashlib import io import json import shutil import tarfile from pathlib import Path HERE = Path(__file__).resolve().parent SPEC = HERE.parent POS = HERE / "fixtures/positive" def digest(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def main() -> None: shutil.copyfile(HERE / "sidecat-author-0.1.schema.json", POS / "schemas/sidecat-author-0.1.schema.json") members = [ Path("contact-descriptor.json"), Path("handler-build.test-bytes"), Path("docs/help.md"), *sorted(Path("schemas") / path.name for path in (POS / "schemas").glob("*.json")), ] archive = POS / "contact-package.test.tar" with tarfile.open(archive, "w", format=tarfile.USTAR_FORMAT) as target: for relative in members: content = (POS / relative).read_bytes() info = tarfile.TarInfo(str(relative)) info.size = len(content) info.mode = 0o644 info.mtime = 0 info.uid = info.gid = 0 info.uname = info.gname = "" target.addfile(info, io.BytesIO(content)) activation_path = POS / "activation.json" activation = json.loads(activation_path.read_text()) for field, path in ( ("packageDigest", archive), ("descriptorDigest", POS / "contact-descriptor.json"), ("handlerBuildDigest", POS / "handler-build.test-bytes"), ): activation[field]["digest"] = digest(path) activation_path.write_text(json.dumps(activation, indent=2) + "\n") profile_path = SPEC / "base-author-profile-0.1.md" profile = profile_path.read_text() start = profile.index("~~~json\n") + len("~~~json\n") end = profile.index("\n~~~", start) descriptor = json.loads((POS / "contact-descriptor.json").read_text()) profile_path.write_text(profile[:start] + json.dumps(descriptor, indent=2) + profile[end:]) print(f"rebuilt test-only package: {digest(archive)}") if __name__ == "__main__": main()