#!/usr/bin/env python3 """Generate deterministic P5-01 Pyodide/allowlist/clock evidence.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any OUTPUTS = (Path("python-runtime/manifest.json"), Path("web/worker/python-bridge.mjs"), Path("web/worker/test_python_bridge.mjs"), Path("python-runtime/test_runtime.mjs")) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run(root: Path, command: list[str]) -> dict[str, Any]: result = subprocess.run(command, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) return {"status": "verified" if result.returncode == 0 else "blocked", "command": " ".join(command), "returncode": result.returncode, "stdout": result.stdout.decode("utf-8", errors="replace")[-3000:], "stderr": result.stderr.decode("utf-8", errors="replace")[-2000:]} def generate(root: Path, report_path: Path) -> dict[str, Any]: manifest = json.loads((root / "python-runtime/manifest.json").read_text(encoding="utf-8")) runtime = run(root, ["node", "python-runtime/test_runtime.mjs"]) bridge = run(root, ["node", "web/worker/test_python_bridge.mjs"]) session = run(root, ["node", "web/worker/test_python_session.mjs"]) report: dict[str, Any] = { "schema": "python-runtime-closure-report.v1", "task_id": "P5-01", "context_id": "linuxcnc-2.10.0-pre0-wasm-m1-py-c1", "status": "verified" if all(item["status"] == "verified" for item in (runtime, bridge, session)) else "blocked", "runtime": {"name": manifest.get("runtime"), "version": manifest.get("version"), "python_version": manifest.get("python_version"), "abi_version": manifest.get("abi_version"), "worker_only": manifest.get("worker_only"), "offline_required": manifest.get("offline_required")}, "allowlist": {"virtual_modules": sorted(manifest.get("virtual_modules", {}).keys()), "denied_capabilities": manifest.get("denied_capabilities", []), "allowed_packages": manifest.get("package_policy", {}).get("allowed_packages", [])}, "limits": {"max_source_bytes": 65536, "max_calls": 256, "clock": "logical_tick * 0.001 seconds"}, "runtime_test": runtime, "bridge_test": bridge, "session_test": session, "outputs": [{"path": path.as_posix(), "bytes": (root / path).stat().st_size, "sha256": sha256(root / path)} for path in OUTPUTS], "known_boundaries": ["P5-02 implements full linuxcnc/hal/INI/tool/parameter compatibility API", "P5-03 implements remap/filter/VCP lifecycle"], } report["report_sha256"] = hashlib.sha256(canonical(report).encode("utf-8")).hexdigest() report_path.parent.mkdir(parents=True, exist_ok=True) report_path.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8") return report def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--report", type=Path, default=Path("coverage/python-runtime-closure.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"python_runtime_closure={report['status']} runtime={report['runtime_test']['status']} bridge={report['bridge_test']['status']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())