import pathlib,sys,tarfile,io,re
root=pathlib.Path(sys.argv[1]);prefix=sys.argv[2]
with tarfile.open(root/f'{prefix}-traces.tar.gz','w:gz') as tar:
 for side in ['base','head']:
  folder=root/f'{prefix}-{side}'
  for i,p in enumerate(sorted((folder/'trace').iterdir())):
   s=p.read_text(errors='replace')
   s=s.replace(str(folder/'sample'),'<fixture>').replace(str(folder),'<run>')
   s=re.sub(r'/Users/[^/\s|]+/[^\s|]*', '<private-path>',s)
   s=re.sub(r'/home/[^/\s|]+/[^\s|]*','<private-path>',s)
   s=re.sub(r'/tmp/[^\s|]+','<temporary-path>',s)
   s=re.sub(r'(?<=ancestry:)\[[^\]]+\]','[process ancestry redacted]',s)
   b=s.encode();info=tarfile.TarInfo(f'{side}/trace-{i:05}.txt');info.size=len(b);tar.addfile(info,io.BytesIO(b))
