92 lines
3.8 KiB
Python
92 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Orchestrate verified profile selection through durable checkpoint readiness only."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
|
|
|
|
def load_module(name: str, filename: str):
|
|
spec = importlib.util.spec_from_file_location(name, HERE / filename)
|
|
module = importlib.util.module_from_spec(spec)
|
|
assert spec.loader
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
planner = load_module("profile_planner", "plan-python-runtime-profile.py")
|
|
journal_api = load_module("profile_journal", "atomic-profile-journal.py")
|
|
checkpoint_api = load_module("profile_checkpoint", "python-environment-checkpoint.py")
|
|
profile_api = load_module("profile_validator", "validate-python-runtime-profile.py")
|
|
|
|
|
|
def prepare(probe: dict, receipt: dict, requested: str, operation_id: str, journal_path: Path,
|
|
target: Path, checkpoint: Path, contract: dict, wheel_dir: Path,
|
|
allow_test_receipt: bool = False) -> dict:
|
|
current = journal_api.read_verified(journal_path)
|
|
planned = planner.plan(probe, receipt, requested, operation_id, current, allow_test_receipt)
|
|
selected = planned["selected_profile"]
|
|
errors = profile_api.validate(contract, selected, wheel_dir)
|
|
if errors:
|
|
raise ValueError("PROFILE_PAYLOAD_INVALID:" + "|".join(errors))
|
|
|
|
if current is None:
|
|
current = journal_api.update(
|
|
journal_path, expected_generation=0, operation_id=operation_id, profile=selected,
|
|
manifest_sha256=receipt["manifest_sha256"], state="profile_selected", checkpoint_id=None,
|
|
)
|
|
if current["state"] == "profile_selected":
|
|
if checkpoint.exists():
|
|
checkpoint_manifest = checkpoint_api.verify(checkpoint, target)
|
|
else:
|
|
checkpoint_manifest = checkpoint_api.create(target, checkpoint, operation_id)
|
|
current = journal_api.update(
|
|
journal_path, expected_generation=current["generation"], operation_id=operation_id,
|
|
profile=selected, manifest_sha256=receipt["manifest_sha256"], state="checkpoint_ready",
|
|
checkpoint_id=checkpoint_manifest["inventory_sha256"],
|
|
)
|
|
if current["state"] != "checkpoint_ready":
|
|
raise ValueError(f"OPERATION_NOT_PREPARABLE:{current['state']}")
|
|
return {"ready": True, "selected_profile": selected, "generation": current["generation"],
|
|
"checkpoint_id": current["checkpoint_id"], "mutation_performed": False}
|
|
|
|
|
|
def read(path: Path) -> dict:
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--probe", type=Path, required=True)
|
|
parser.add_argument("--receipt", type=Path, required=True)
|
|
parser.add_argument("--requested", choices=("auto", "cpu-only", "nvidia-cu128"), default="auto")
|
|
parser.add_argument("--operation-id", required=True)
|
|
parser.add_argument("--journal", type=Path, required=True)
|
|
parser.add_argument("--target", type=Path, required=True)
|
|
parser.add_argument("--checkpoint", type=Path, required=True)
|
|
parser.add_argument("--profile-contract", type=Path, required=True)
|
|
parser.add_argument("--wheel-dir", type=Path, required=True)
|
|
parser.add_argument("--allow-test-receipt", action="store_true")
|
|
args = parser.parse_args()
|
|
try:
|
|
result = prepare(
|
|
read(args.probe), read(args.receipt), args.requested, args.operation_id, args.journal,
|
|
args.target, args.checkpoint, read(args.profile_contract), args.wheel_dir,
|
|
args.allow_test_receipt,
|
|
)
|
|
except Exception as error:
|
|
print(json.dumps({"ready": False, "code": str(error)}, sort_keys=True))
|
|
return 1
|
|
print(json.dumps(result, sort_keys=True))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|