from pathlib import Path
import shutil,tarfile,io
HERE=Path(__file__).resolve().parent;RUN=Path('/tmp/cdx-ai-final-weight-read-20261011')
for p in (HERE/'tools').glob('*.php'):shutil.copyfile(p,RUN/p.name)
for lane in ['B','C']:
 entries={p.name:p for p in RUN.iterdir() if p.is_file()};entries.update({p.relative_to(RUN).as_posix():p for p in (RUN/'source').rglob('*') if p.is_file()});entries.update({'private/'+p.name:p for p in (RUN/f'seed-{lane}').iterdir() if p.is_file()});dirs={RUN.name,RUN.name+'/private'}
 for n in entries:dirs.update(str(q) for q in (Path(RUN.name)/n).parents if str(q)!='.')
 with tarfile.open('/tmp/cdx-ai-final-weight-'+lane+'.tar','w',format=tarfile.USTAR_FORMAT) as t:
  for n in sorted(dirs):i=tarfile.TarInfo(n);i.type=tarfile.DIRTYPE;i.mode=0o700;t.addfile(i)
  for n,p in sorted(entries.items()):raw=p.read_bytes();i=tarfile.TarInfo(RUN.name+'/'+n);i.size=len(raw);i.mode=0o600;t.addfile(i,io.BytesIO(raw))
