MateuszBocko's picture
Initial | Project Setup
6237959 verified
Raw History Blame Contribute Delete
3.31 kB
"""Create the complete deliverable ZIP only after successful exchange checks."""
from pathlib import Path
import hashlib,json,os,time,zipfile
def main():
start=time.time();root=Path(__file__).resolve().parent.parent
report=json.loads((root/'validation_report.json').read_text())
manifest=json.loads((root/'EXPORT/manifest.json').read_text())
fit=json.loads((root/'fit_checks.json').read_text())
assert report['version']==manifest['version']==fit['version']=='2.1'
assert not fit['failures']
assert report['GLB']['all_files_validated']==len(manifest['files'])
assert len(list((root/'EXPORT').rglob('*.glb')))==len(manifest['files'])
for state in ['Stowed','Deployed','Internal','Exploded']:
r=next(x for x in report['STEP'] if x['path']=='ORCASat_'+state+'.step')
assert r['valid'] and r['volume_relative_error']<1e-6
def eligible(p):
return p.is_file() and '__pycache__' not in p.parts and p.suffix not in ['.pyc','.tmp'] and p.name!='package_manifest.json'
paths=sorted(p for p in root.rglob('*') if eligible(p))
rows=[]
for p in paths:
rows.append({'path':p.relative_to(root).as_posix(),'bytes':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()})
current={r['path']:r['sha256'] for r in rows}
for r in report['STEP']:assert current[r['path']]==r['sha256'],r['path']
for r in manifest['files']:assert current['EXPORT/'+r['path']]==r['sha256'],r['path']
(root/'package_manifest.json').write_text(json.dumps({'version':'2.1','root':'ORCASat_CAD','files_excluding_this_manifest':rows,'GLB_files':len(manifest['files']),'STEP_paths':len(report['STEP']),'configurations':['Stowed','Deployed','Internal','Exploded'],'contents':'Updated CAD and integrated EXPORT in one archive'},indent=2))
paths.append(root/'package_manifest.json');paths.sort()
target=root.parent/'ORCASat_CAD_Package.zip';temp=target.with_suffix('.zip.tmp')
with zipfile.ZipFile(temp,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=6,allowZip64=True) as archive:
for i,p in enumerate(paths):
archive.write(p,'ORCASat_CAD/'+p.relative_to(root).as_posix())
if i%500==0:print('ZIP files',i+1,'/',len(paths),flush=True)
with zipfile.ZipFile(temp) as archive:
assert archive.testzip() is None,'ZIP CRC failure'
names=archive.namelist();assert len(names)==len(set(names))==len(paths)
assert sum(n.endswith('.glb') and n.startswith('ORCASat_CAD/EXPORT/') for n in names)==len(manifest['files'])
for state in ['Stowed','Deployed','Internal','Exploded']:
assert 'ORCASat_CAD/ORCASat_'+state+'.step' in names
assert 'ORCASat_CAD/EXPORT/assemblies/ORCASat_'+state+'.glb' in names
os.replace(temp,target)
result={'archive':str(target),'bytes':target.stat().st_size,'sha256':hashlib.sha256(target.read_bytes()).hexdigest(),'files':len(paths),'GLBs':len(manifest['files']),'STEP_paths':len(report['STEP']),'CRC_test':'passed','elapsed_seconds':round(time.time()-start,1)}
target.with_suffix('.zip.sha256').write_text(result['sha256']+' '+target.name+'\n')
(root.parent/'package_result.json').write_text(json.dumps(result,indent=2));print(json.dumps(result,indent=2),flush=True)
if __name__=='__main__':main()