"""Package current source plus simulation adapters into a digest-addressed archive.""" import argparse import hashlib import json import tarfile from pathlib import Path root = Path(__file__).resolve().parents[2] parser = argparse.ArgumentParser() parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() args.output.mkdir(parents=True, exist_ok=True) files = sorted( p for folder in (root / "src", root / "simulation/ai-polygon") for p in folder.rglob("*") if p.is_file() and not p.is_symlink() and "__pycache__" not in p.parts ) manifest = { p.relative_to(root).as_posix(): hashlib.sha256(p.read_bytes()).hexdigest() for p in files } encoded = json.dumps(manifest, sort_keys=True, indent=2).encode() identity = hashlib.sha256(encoded).hexdigest() path = args.output / ("ai-polygon-" + identity[:16] + ".tgz") with tarfile.open(path, "w:gz") as archive: for p in files: archive.add(p, arcname=p.relative_to(root).as_posix(), recursive=False) manifest_path = path.with_suffix(".manifest.json") manifest_path.write_bytes(encoded) print( json.dumps( { "bundle": str(path), "identity": identity, "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), "files": len(files), } ) )