"""Restore only the fixed manifest's hash-checked native cache into the private tree.""" import argparse,gzip,hashlib,json,os,subprocess from pathlib import Path MANIFEST_SHA='292caa386a620d935cfc736b56fcceaab9fe7073086693b62e899c1ee4725f04' SOURCE_TREE='24df20b62d9976ccd20da819bc20b16f808f0fdd' def sha(p): with p.open('rb') as f:return hashlib.file_digest(f,'sha256').hexdigest() def main(): p=argparse.ArgumentParser(description=__doc__) p.add_argument('--cache',type=Path,required=True);p.add_argument('--project',type=Path,required=True) p.add_argument('--leantar',type=Path,required=True);p.add_argument('--report',type=Path,required=True) p.add_argument('--sample',type=int) a=p.parse_args();root=a.project.resolve();raw=(a.cache/'modules.jsonl.gz').read_bytes() if hashlib.sha256(raw).hexdigest()!=MANIFEST_SHA:raise ValueError('manifest SHA-256 mismatch') lines=gzip.decompress(raw).splitlines();header=json.loads(lines[0]);rows=[json.loads(l) for l in lines[1:]] if len(rows)!=175187 or header['source_tree']!=SOURCE_TREE:raise ValueError('unexpected cache manifest') tree=subprocess.check_output(['git','-C',str(root),'rev-parse','HEAD:src'],text=True).strip() if tree!=SOURCE_TREE:raise ValueError('private source tree differs from the cache pin') if a.sample is not None: if not 1<=a.sample<=100:raise ValueError('invalid sample count') rows=rows[:a.sample] # Windows verbatim paths preserve deeply nested original module names. fsroot=root if os.name=='nt':fsroot=Path('\\\\?\\'+str(root)) jobs=[] for i,(name,dep,artifact,digest) in enumerate(rows,1): pieces=name.split('.') if not all(piece and all(c.isalnum() or c in "_'" for c in piece) for piece in pieces):raise ValueError('unsafe module name') file=a.cache/'artifacts'/artifact if sha(file)!=digest:raise ValueError('cached archive digest mismatch: '+artifact) source=fsroot/'src'/Path(*pieces).with_suffix('.lean') if not source.is_file():raise ValueError('missing private source module: '+name) bases=[] for directory in ['lib/lean','ir']: parent=fsroot/'.lake/build'/directory/Path(*pieces[:-1]);parent.mkdir(parents=True,exist_ok=True);bases.append(str(parent)) jobs.append({'file':str(file.resolve()),'hash':dep,'base':bases}) if i%10000==0:print(f'progress: {i}/{len(rows)} cache archives rechecked for restore',flush=True) version=subprocess.check_output([str(a.leantar),'--version'],text=True).strip() if version!='leantar 0.1.20':raise ValueError('unexpected archive reader version') subprocess.run([str(a.leantar),'--jobs','4','-x','-j','-'],input=json.dumps(jobs),text=True,cwd=root,check=True) for name,_,_,_ in rows: file=fsroot/'.lake/build/lib/lean'/Path(*name.split('.')).with_suffix('.olean') if not file.is_file():raise ValueError('missing restored module '+name) report={'source_tree':tree,'manifest_sha256':MANIFEST_SHA,'restored_modules':len(rows),'sample_only':a.sample is not None,'leantar':version, 'scope':'Restored third-party compiled modules after archive digest checks; not an independent rebuild or kernel verification.'} a.report.write_text(json.dumps(report,indent=2)+'\n',encoding='utf-8') print(('Sample only: ' if a.sample else '')+f'Restored {len(rows)} hash-checked native cache modules.',flush=True) if __name__=='__main__':main()