Files
DCCONSTRUCTIONS d0e5f90952 Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
2026-10-08 09:59:51 +03:00

64 lines
3.2 KiB
Python

"""Seal the qualified Python closure and upstream source into a model image.
Only copies the existing artifact-owned environment into bounded build staging.
No installation into Windows/WSL or mutation of the source environment.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import tarfile
def sha(path):
with path.open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--runtime', type=Path, default=Path('D:/NDC_MISSIONCORE/runtime'))
args = parser.parse_args()
assert not (args.runtime / 'simulation/state/active.json').exists()
base = 'ndc-sam3-litetext-qualification:0705589032d7'
expected = 'sha256:6a1a2f2e4ebe499b648b9f75bdacf47ef36a2128a0ec8385b6decdc770f21e6c'
actual = json.loads(subprocess.check_output(['docker', 'image', 'inspect', base], text=True))[0]['Id']
assert actual == expected, 'Build predecessor changed'
args.output.mkdir(parents=True, exist_ok=False)
context = args.output / 'context'; context.mkdir()
source_archive = args.runtime / 'simulation/staging/source.tar.gz'
assert sha(source_archive) == 'bdac58424bcb39e2533689b3256ba52e15f612472a7bbc05a0485de5017da576'
with tarfile.open(source_archive) as archive:
archive.extractall(context / 'upstream', filter='data')
upstream = context / 'upstream/efficientsam3-bd0936c788fed8d51fa799437f05abd97b401b06/sam3'
shutil.move(str(upstream), str(context / 'source'))
shutil.rmtree(context / 'upstream')
closure = args.runtime / 'derived/perception-p0-env-v1'
size = sum(p.stat().st_size for p in closure.rglob('*') if p.is_file())
assert shutil.disk_usage(args.output).free > size * 3 + 10 * 1024**3
shutil.copytree(closure, context / 'environment', ignore=shutil.ignore_patterns('__pycache__', '*.pyc'))
shutil.copyfile(Path(__file__).with_name('Dockerfile'), context / 'Dockerfile')
inputs = {p.relative_to(context).as_posix(): sha(p) for p in sorted(context.rglob('*')) if p.is_file()}
encoded = json.dumps(dict(base=expected, files=inputs), sort_keys=True).encode()
(context / 'build-inputs.json').write_bytes(encoded)
tag = 'ndc-sam3-surfaces:' + hashlib.sha256(encoded).hexdigest()[:16]
with (args.output / 'build.log').open('w') as log:
subprocess.run(['docker', 'build', '--pull=false', '-t', tag, str(context)],
env={**os.environ, 'DOCKER_BUILDKIT':'0'}, stdout=log, stderr=subprocess.STDOUT,
check=True, timeout=900)
identity = json.loads(subprocess.check_output(['docker', 'image', 'inspect', tag], text=True))[0]['Id']
receipt = dict(image=identity, tag=tag, input_sha256=hashlib.sha256(encoded).hexdigest(),
source_archive_sha256=sha(source_archive), runtime_changes='none; isolated image only')
(args.output / 'image.json').write_text(json.dumps(receipt, indent=2))
shutil.copyfile(context / 'build-inputs.json', args.output / 'build-inputs.json')
shutil.rmtree(context)
print(json.dumps(receipt))
if __name__ == '__main__':
main()