EVAx10_Video_analitics/tools/check-production-assets.py

47 lines
2.3 KiB
Python

"""Fail closed for unreviewed model manifests, model assets and runtime binaries."""
import hashlib
import json
from pathlib import Path
import sys
root = Path(__file__).resolve().parents[1]
assets = root / "assets"
models = assets / "models"
lock = json.loads((root / "license-lock.json").read_text())
required_rights = ("commercialUse", "modificationAllowed", "redistributionAllowed", "productionAllowed")
allowed_licenses = {"MIT", "Apache-2.0", "CC0-1.0", "BSD-2-Clause", "BSD-3-Clause"}
def check(condition, message):
if not condition:
sys.exit("Production license gate: " + message)
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
inventory = {p.name for p in models.glob("*.json")}
check(inventory == set(lock["models"]), "manifest inventory differs from reviewed lock")
expected_assets = set(inventory)
for name, approved_hash in lock["models"].items():
model = json.loads((models / name).read_text())
filename = Path(model["file"]).name
artifact = models / filename
expected_assets.add(filename)
check(model["file"] == f"models/{filename}" and artifact.suffix == ".onnx", f"{name} invalid artifact path")
check(model["license"] in allowed_licenses and model["licenseVerification"] == "verified" and
all(model.get(right) is True for right in required_rights), f"{name} does not grant all required rights")
notice = assets / model["licenseNotice"]
check(model["licenseEvidence"].startswith("https://") and notice.parent == assets / "notices" and
notice.is_file(), f"{name} license evidence and local notice are required")
check(artifact.is_file() and artifact.stat().st_size == model["size"] and
model["sha256"] == approved_hash == digest(artifact), f"{name} model bytes differ from reviewed lock")
check({p.name for p in models.iterdir()} == expected_assets, "unexpected content in models/")
packaged = {p.relative_to(assets).as_posix(): p for p in assets.rglob("*") if p.is_file()}
check(set(packaged) == set(lock["assetInventory"]), "APK asset inventory differs from reviewed lock")
for name, path in packaged.items():
check(digest(path) == lock["assetInventory"][name], f"{name} differs from reviewed asset lock")
check(digest(root / "build/ort.aar") == lock["runtimeAarSha256"], "unreviewed runtime AAR")
print("Production license gate passed")