"""Build immutable task-data tarballs with original workspace-relative paths.""" from pathlib import Path import csv,json,tarfile,hashlib,concurrent.futures,collections,time P=Path(__file__).resolve().parents[1];R=P.parents[1] OUT=R/'deliveries/wu_structure_function_data_20261105';OUT.mkdir(exist_ok=True);(OUT/'archives').mkdir(exist_ok=True) rows=list(csv.DictReader((P/'metadata/release_inventory.csv').open()));groups=collections.defaultdict(list) for row in rows: if row['public_disposition']!='INCLUDE':continue parts=Path(row['path']).parts group=parts[1] if parts[0]=='projects' else 'structural_atlas' if parts[:2]==('artifacts','wu_20261030_all5177_structural_atlas') else 'dependencies' groups[group].append(row) def build(item): group,items=item;target=OUT/'archives'/(group+'.tar.gz');start=time.monotonic() with tarfile.open(target,'w:gz',compresslevel=1) as tar: for row in sorted(items,key=lambda x:x['path']): p=R/row['path'];assert p.stat().st_size==int(row['bytes']) info=tar.gettarinfo(str(p),arcname=row['path']);info.uid=info.gid=0;info.uname=info.gname='';info.mtime=0 with p.open('rb') as f:tar.addfile(info,f) h=hashlib.sha256() with target.open('rb') as f: while b:=f.read(8*1024*1024):h.update(b) record=dict(archive=str(target.relative_to(OUT)),sha256=h.hexdigest(),bytes=target.stat().st_size,n_files=len(items),uncompressed_bytes=sum(int(x['bytes']) for x in items),elapsed_s=round(time.monotonic()-start,2)) print(json.dumps(record),flush=True);return record if __name__=='__main__': with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:archives=list(pool.map(build,sorted(groups.items()))) (OUT/'archive_manifest.json').write_text(json.dumps(archives,indent=2));(P/'metadata/data_archive_manifest.json').write_text(json.dumps(archives,indent=2)) print('PACKAGING_COMPLETE',len(archives),sum(x['n_files'] for x in archives),sum(x['bytes'] for x in archives),flush=True)