|
| 1 | +"""Package mp-300k artifact-gate inputs for CI handoff.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import hashlib |
| 7 | +import json |
| 8 | +import shutil |
| 9 | +import tarfile |
| 10 | +from datetime import UTC, datetime |
| 11 | +from pathlib import Path |
| 12 | +from typing import Any |
| 13 | + |
| 14 | + |
| 15 | +def package_mp300k_gate_inputs( |
| 16 | + artifact_dir: str | Path, |
| 17 | + output_dir: str | Path, |
| 18 | + *, |
| 19 | + candidate_dataset_path: str | Path | None = None, |
| 20 | + ecps_comparison_path: str | Path | None = None, |
| 21 | + runtime_smoke_path: str | Path | None = None, |
| 22 | + benchmark_manifest_path: str | Path | None = None, |
| 23 | + archive_name: str = "artifact.tar.gz", |
| 24 | +) -> dict[str, Any]: |
| 25 | + """Package an artifact archive plus gate evidence for GitHub Actions. |
| 26 | +
|
| 27 | + The output directory is intended to be uploaded as a single Actions artifact |
| 28 | + and consumed by ``mp300k-artifact-gates.yml`` through ``gate_inputs_artifact``. |
| 29 | + """ |
| 30 | + |
| 31 | + artifact_root = Path(artifact_dir).expanduser() |
| 32 | + output_root = Path(output_dir).expanduser() |
| 33 | + manifest_path = artifact_root / "manifest.json" |
| 34 | + manifest = _load_manifest(manifest_path) |
| 35 | + candidate_dataset = _resolve_candidate_dataset_path( |
| 36 | + artifact_root, |
| 37 | + manifest, |
| 38 | + candidate_dataset_path, |
| 39 | + ) |
| 40 | + if not candidate_dataset.exists(): |
| 41 | + raise FileNotFoundError(f"candidate dataset not found: {candidate_dataset}") |
| 42 | + |
| 43 | + output_root.mkdir(parents=True, exist_ok=True) |
| 44 | + archive_path = output_root / archive_name |
| 45 | + stage_parent = output_root / ".staging" |
| 46 | + if stage_parent.exists(): |
| 47 | + shutil.rmtree(stage_parent) |
| 48 | + stage_root = stage_parent / artifact_root.name |
| 49 | + stage_root.mkdir(parents=True) |
| 50 | + |
| 51 | + candidate_relpath = _candidate_archive_relpath( |
| 52 | + manifest, |
| 53 | + candidate_dataset=candidate_dataset, |
| 54 | + explicit_candidate_path=candidate_dataset_path, |
| 55 | + ) |
| 56 | + staged_candidate = stage_root / candidate_relpath |
| 57 | + staged_candidate.parent.mkdir(parents=True, exist_ok=True) |
| 58 | + shutil.copy2(candidate_dataset, staged_candidate) |
| 59 | + |
| 60 | + staged_manifest = _manifest_for_archive( |
| 61 | + manifest, |
| 62 | + source_artifact_dir=artifact_root, |
| 63 | + source_candidate_dataset=candidate_dataset, |
| 64 | + candidate_relpath=candidate_relpath, |
| 65 | + ) |
| 66 | + _write_json(stage_root / "manifest.json", staged_manifest) |
| 67 | + _write_archive(archive_path, stage_root) |
| 68 | + |
| 69 | + evidence = { |
| 70 | + "ecps_comparison": _copy_optional_evidence( |
| 71 | + ecps_comparison_path, |
| 72 | + output_root / "ecps_comparison.json", |
| 73 | + ), |
| 74 | + "runtime_smoke": _copy_optional_evidence( |
| 75 | + runtime_smoke_path, |
| 76 | + output_root / "runtime_smoke.json", |
| 77 | + ), |
| 78 | + "benchmark_manifest": _copy_optional_evidence( |
| 79 | + benchmark_manifest_path, |
| 80 | + output_root / "benchmark_manifest.json", |
| 81 | + ), |
| 82 | + } |
| 83 | + metadata = { |
| 84 | + "schema_version": 1, |
| 85 | + "generated_at": datetime.now(UTC).isoformat(), |
| 86 | + "source_artifact_dir": str(artifact_root.resolve()), |
| 87 | + "source_manifest": _file_descriptor(manifest_path), |
| 88 | + "source_candidate_dataset": _file_descriptor(candidate_dataset), |
| 89 | + "artifact_archive": _file_descriptor(archive_path), |
| 90 | + "evidence": evidence, |
| 91 | + "workflow_call": { |
| 92 | + "uses": "./.github/workflows/mp300k-artifact-gates.yml", |
| 93 | + "with": {"gate_inputs_artifact": output_root.name}, |
| 94 | + }, |
| 95 | + } |
| 96 | + _write_json(output_root / "gate_inputs.json", metadata) |
| 97 | + shutil.rmtree(stage_parent) |
| 98 | + return metadata |
| 99 | + |
| 100 | + |
| 101 | +def _load_manifest(path: Path) -> dict[str, Any]: |
| 102 | + if not path.exists(): |
| 103 | + raise FileNotFoundError(f"manifest not found: {path}") |
| 104 | + return json.loads(path.read_text()) |
| 105 | + |
| 106 | + |
| 107 | +def _resolve_candidate_dataset_path( |
| 108 | + artifact_root: Path, |
| 109 | + manifest: dict[str, Any], |
| 110 | + explicit_path: str | Path | None, |
| 111 | +) -> Path: |
| 112 | + if explicit_path is not None: |
| 113 | + return Path(explicit_path).expanduser() |
| 114 | + artifacts = dict(manifest.get("artifacts", {})) |
| 115 | + dataset_name = artifacts.get("policyengine_dataset") |
| 116 | + if not isinstance(dataset_name, str) or not dataset_name: |
| 117 | + raise ValueError( |
| 118 | + "manifest.artifacts.policyengine_dataset is required when " |
| 119 | + "candidate_dataset_path is not supplied" |
| 120 | + ) |
| 121 | + dataset_path = Path(dataset_name).expanduser() |
| 122 | + if not dataset_path.is_absolute(): |
| 123 | + dataset_path = artifact_root / dataset_path |
| 124 | + return dataset_path |
| 125 | + |
| 126 | + |
| 127 | +def _candidate_archive_relpath( |
| 128 | + manifest: dict[str, Any], |
| 129 | + *, |
| 130 | + candidate_dataset: Path, |
| 131 | + explicit_candidate_path: str | Path | None, |
| 132 | +) -> Path: |
| 133 | + if explicit_candidate_path is not None: |
| 134 | + return Path(candidate_dataset.name) |
| 135 | + dataset_name = dict(manifest.get("artifacts", {})).get("policyengine_dataset") |
| 136 | + if isinstance(dataset_name, str) and dataset_name: |
| 137 | + relpath = Path(dataset_name) |
| 138 | + if not relpath.is_absolute(): |
| 139 | + return relpath |
| 140 | + return Path(candidate_dataset.name) |
| 141 | + |
| 142 | + |
| 143 | +def _manifest_for_archive( |
| 144 | + manifest: dict[str, Any], |
| 145 | + *, |
| 146 | + source_artifact_dir: Path, |
| 147 | + source_candidate_dataset: Path, |
| 148 | + candidate_relpath: Path, |
| 149 | +) -> dict[str, Any]: |
| 150 | + updated = dict(manifest) |
| 151 | + artifacts = dict(updated.get("artifacts", {})) |
| 152 | + artifacts["policyengine_dataset"] = str(candidate_relpath) |
| 153 | + updated["artifacts"] = artifacts |
| 154 | + updated["mp300k_gate_inputs"] = { |
| 155 | + "packaged_at": datetime.now(UTC).isoformat(), |
| 156 | + "source_artifact_dir": str(source_artifact_dir.resolve()), |
| 157 | + "source_candidate_dataset": str(source_candidate_dataset.resolve()), |
| 158 | + } |
| 159 | + return updated |
| 160 | + |
| 161 | + |
| 162 | +def _copy_optional_evidence( |
| 163 | + source_path: str | Path | None, |
| 164 | + destination_path: Path, |
| 165 | +) -> dict[str, Any] | None: |
| 166 | + if source_path is None: |
| 167 | + return None |
| 168 | + source = Path(source_path).expanduser() |
| 169 | + if not source.exists(): |
| 170 | + raise FileNotFoundError(f"evidence file not found: {source}") |
| 171 | + shutil.copy2(source, destination_path) |
| 172 | + return _file_descriptor(destination_path) |
| 173 | + |
| 174 | + |
| 175 | +def _write_archive(archive_path: Path, stage_root: Path) -> None: |
| 176 | + with tarfile.open(archive_path, "w:gz") as archive: |
| 177 | + archive.add(stage_root, arcname=stage_root.name, recursive=True) |
| 178 | + |
| 179 | + |
| 180 | +def _write_json(path: Path, payload: dict[str, Any]) -> None: |
| 181 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 182 | + path.write_text(json.dumps(payload, indent=2, sort_keys=True)) |
| 183 | + |
| 184 | + |
| 185 | +def _file_descriptor(path: Path) -> dict[str, Any]: |
| 186 | + return { |
| 187 | + "path": str(path.resolve()), |
| 188 | + "size_bytes": path.stat().st_size, |
| 189 | + "sha256": _sha256_file(path), |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | +def _sha256_file(path: Path) -> str: |
| 194 | + digest = hashlib.sha256() |
| 195 | + with path.open("rb") as handle: |
| 196 | + for chunk in iter(lambda: handle.read(1024 * 1024), b""): |
| 197 | + digest.update(chunk) |
| 198 | + return digest.hexdigest() |
| 199 | + |
| 200 | + |
| 201 | +def main(argv: list[str] | None = None) -> int: |
| 202 | + parser = argparse.ArgumentParser( |
| 203 | + description="Package mp-300k artifact-gate inputs for CI." |
| 204 | + ) |
| 205 | + parser.add_argument("--artifact-dir", required=True) |
| 206 | + parser.add_argument("--output-dir", required=True) |
| 207 | + parser.add_argument("--candidate-dataset") |
| 208 | + parser.add_argument("--ecps-comparison-json") |
| 209 | + parser.add_argument("--runtime-smoke-json") |
| 210 | + parser.add_argument("--benchmark-manifest") |
| 211 | + parser.add_argument("--archive-name", default="artifact.tar.gz") |
| 212 | + args = parser.parse_args(argv) |
| 213 | + |
| 214 | + package_mp300k_gate_inputs( |
| 215 | + args.artifact_dir, |
| 216 | + args.output_dir, |
| 217 | + candidate_dataset_path=args.candidate_dataset, |
| 218 | + ecps_comparison_path=args.ecps_comparison_json, |
| 219 | + runtime_smoke_path=args.runtime_smoke_json, |
| 220 | + benchmark_manifest_path=args.benchmark_manifest, |
| 221 | + archive_name=args.archive_name, |
| 222 | + ) |
| 223 | + print(Path(args.output_dir).expanduser() / "gate_inputs.json") |
| 224 | + return 0 |
| 225 | + |
| 226 | + |
| 227 | +if __name__ == "__main__": |
| 228 | + raise SystemExit(main()) |
0 commit comments