Spaces:
Paused
Paused
Add executable artifact manifest validator
Browse files- validate_manifest.py +69 -0
validate_manifest.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Validate a Simam3D run manifest and its exported artifact fingerprints."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import hashlib
|
| 6 |
+
import json
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def file_sha256(path: Path) -> str:
|
| 12 |
+
digest = hashlib.sha256()
|
| 13 |
+
with path.open("rb") as handle:
|
| 14 |
+
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
|
| 15 |
+
digest.update(chunk)
|
| 16 |
+
return digest.hexdigest()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def validate_manifest(manifest_path: str | Path) -> tuple[bool, list[str]]:
|
| 20 |
+
manifest_path = Path(manifest_path)
|
| 21 |
+
errors: list[str] = []
|
| 22 |
+
try:
|
| 23 |
+
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
| 24 |
+
except (OSError, json.JSONDecodeError) as exc:
|
| 25 |
+
return False, [f"manifest unreadable: {exc}"]
|
| 26 |
+
if manifest.get("project") != "Simam3D":
|
| 27 |
+
errors.append("project is not Simam3D")
|
| 28 |
+
if int(manifest.get("manifest_schema_version", 0)) < 2:
|
| 29 |
+
errors.append("manifest schema is older than fingerprinted schema 2")
|
| 30 |
+
outputs = manifest.get("outputs")
|
| 31 |
+
hashes = manifest.get("output_sha256")
|
| 32 |
+
if not isinstance(outputs, list) or not isinstance(hashes, dict):
|
| 33 |
+
return False, errors + ["outputs/output_sha256 fields are missing or invalid"]
|
| 34 |
+
for raw_output in outputs:
|
| 35 |
+
output = Path(str(raw_output))
|
| 36 |
+
if not output.is_absolute() and not output.is_file():
|
| 37 |
+
sibling = manifest_path.parent / output.name
|
| 38 |
+
if sibling.is_file():
|
| 39 |
+
output = sibling
|
| 40 |
+
if not output.is_file():
|
| 41 |
+
errors.append(f"missing artifact: {raw_output}")
|
| 42 |
+
continue
|
| 43 |
+
expected = hashes.get(output.name)
|
| 44 |
+
if not expected:
|
| 45 |
+
errors.append(f"missing hash: {output.name}")
|
| 46 |
+
continue
|
| 47 |
+
actual = file_sha256(output)
|
| 48 |
+
if actual != expected:
|
| 49 |
+
errors.append(f"hash mismatch: {output.name}")
|
| 50 |
+
return not errors, errors
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def main(argv: list[str] | None = None) -> int:
|
| 54 |
+
argv = argv or sys.argv[1:]
|
| 55 |
+
if len(argv) != 1:
|
| 56 |
+
print("usage: python validate_manifest.py PATH_TO_RUN_JSON", file=sys.stderr)
|
| 57 |
+
return 2
|
| 58 |
+
valid, errors = validate_manifest(argv[0])
|
| 59 |
+
if valid:
|
| 60 |
+
print("VALID: all Simam3D artifacts match manifest fingerprints")
|
| 61 |
+
return 0
|
| 62 |
+
print("INVALID")
|
| 63 |
+
for error in errors:
|
| 64 |
+
print(f"- {error}")
|
| 65 |
+
return 1
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
raise SystemExit(main())
|