Local preservation snapshot, not a new deployment or simulation acceptance.
50 lines
2.0 KiB
Python
50 lines
2.0 KiB
Python
"""Prepare only the pinned optional research model; no host packages or services."""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import urllib.request
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
|
|
|
|
def sha(path):
|
|
with path.open("rb") as stream:
|
|
return hashlib.file_digest(stream, "sha256").hexdigest()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--root", type=Path, default=Path("D:/NDC_MISSIONCORE/runtime/simulation"))
|
|
args = parser.parse_args()
|
|
descriptor = json.loads(Path(__file__).with_name("model.json").read_text())
|
|
output = args.root / "assets" / descriptor["asset_directory"] / descriptor["revision"]
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
for name, expected in descriptor["files"].items():
|
|
path = output / name
|
|
if not path.exists():
|
|
part = path.with_name(name + f".{os.getpid()}.part")
|
|
url = f'https://huggingface.co/{descriptor["repository"]}/resolve/{descriptor["revision"]}/{name}'
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=60) as response, part.open("xb") as stream:
|
|
while chunk := response.read(1024 * 1024):
|
|
stream.write(chunk)
|
|
if sha(part) != expected:
|
|
raise ValueError("Downloaded model checksum mismatch: " + name)
|
|
part.replace(path)
|
|
finally:
|
|
part.unlink(missing_ok=True)
|
|
if sha(path) != expected:
|
|
raise ValueError("Existing model checksum mismatch: " + name)
|
|
receipt = dict(schema_version="missioncore.optional-model-preparation/v1",
|
|
descriptor=descriptor, utc=datetime.now(UTC).isoformat(),
|
|
assets=str(output), scope="offline simulation qualification",
|
|
rollback="Remove this optional qualification asset; no active model was changed")
|
|
(output / "installation.json").write_text(json.dumps(receipt, indent=2))
|
|
print(json.dumps(receipt))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|