"""Package current source plus simulation adapters into a digest-addressed archive.""" import argparse import ast 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 ) files.extend((root / "apps/control-station/public/vendor/playcanvas/draco/1.5.7").glob("*")) # The canonical Rover 006 asset is shared with PlayCanvas, not a second model. for profile in (root / "simulation/ai-polygon/robots").glob("*.json"): asset = root / json.loads(profile.read_text())["visual"]["source"] if asset not in files: files.append(asset) # Reject a broken Python artifact before staging or touching the running worker. for source in files: if source.suffix == ".py": ast.parse(source.read_bytes(), filename=str(source)) 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), } ) )