"""Stage the verified original source and fixed Mathlib checkout privately on E.""" import argparse import hashlib import io import json import os from pathlib import Path import subprocess import tarfile from inventory_source import Parts from verify_source import PARTS, verify MATHLIB='0df444a360eaa60ab8c11dca51a86af692955474' BASELINE='e50d09817ad81469f737e8f637561e6ccf86c670' CACHE_TREE='24df20b62d9976ccd20da819bc20b16f808f0fdd' def main(): p=argparse.ArgumentParser(description=__doc__) p.add_argument('--cache',required=True,type=Path) p.add_argument('--private-root',required=True,type=Path) p.add_argument('--report',required=True,type=Path) a=p.parse_args();verify(a.cache);root=a.private_root.resolve();root.mkdir(parents=True,exist_ok=True) count=0 with io.BufferedReader(Parts([a.cache/n for n in PARTS])) as stream: with tarfile.open(fileobj=stream,mode='r|gz') as archive: for member in archive: if not member.isfile():raise ValueError('unexpected non-regular source archive member') if not member.name.startswith('lean/'):raise ValueError('unexpected source archive root') relative=Path(member.name[len('lean/'):]) if relative.is_absolute() or '..' in relative.parts:raise ValueError('unsafe source archive path') target=root/relative;raw=archive.extractfile(member).read() target.parent.mkdir(parents=True,exist_ok=True) if not target.exists() or target.read_bytes()!=raw:target.write_bytes(raw) count+=1 if count%5000==0:print(f'progress: {count}/175190 original source files staged privately',flush=True) curl='curl.exe' if os.name=='nt' else 'curl' for name,relative in [('lakefile.toml','lakefile.toml'),('Challenge.lean','src/Challenge.lean')]: subprocess.run([curl,'-fsSL','--retry','4','--retry-all-errors', f'https://raw.githubusercontent.com/leanprover-community/riemann-hypothesis/{BASELINE}/{relative}', '-o',str(root/relative)],check=True) git=['git','-c','credential.helper=','-c','http.sslBackend=schannel'] if os.name=='nt' else ['git','-c','credential.helper='] if not (root/'.git').exists():subprocess.run(git+['init',str(root)],check=True) for key,value in [('core.autocrlf','false'),('user.name','cogito-curator'),('user.email','cogito-curator@users.noreply')]: subprocess.run(git+['-C',str(root),'config',key,value],check=True) subprocess.run(git+['-C',str(root),'add','src','lakefile.toml','lake-manifest.json','lean-toolchain'],check=True) tree=subprocess.check_output(git+['-C',str(root),'write-tree'],text=True).strip() src_tree=subprocess.check_output(git+['-C',str(root),'rev-parse',tree+':src'],text=True).strip() if src_tree!=CACHE_TREE:raise ValueError('staged source does not match the pinned cache source tree') diff=subprocess.run(git+['-C',str(root),'diff','--cached','--quiet']).returncode if diff:subprocess.run(git+['-C',str(root),'commit','-m','Stage private original source with challenge-only cache wrapper'],check=True) manifest=json.loads((root/'lake-manifest.json').read_text());packages=root/'.lake/packages';packages.mkdir(parents=True,exist_ok=True) for package in manifest['packages']: dest=packages/package['name'];revision=package['rev'] if not (dest/'.git').exists():subprocess.run(git+['init',str(dest)],check=True) present=subprocess.run(git+['-C',str(dest),'cat-file','-e',revision+'^{commit}'],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode==0 if not present:subprocess.run(git+['-C',str(dest),'fetch','--depth=1',package['url'],revision],check=True) subprocess.run(git+['-C',str(dest),'checkout','--detach',revision],check=True) actual=subprocess.check_output(git+['-C',str(dest),'rev-parse','HEAD'],text=True).strip() if actual!=revision:raise ValueError('dependency checkout mismatch') print('Pinned dependency ready: '+package['name']+' '+actual,flush=True) a.report.write_text(json.dumps({'source_files':count,'source_tree_with_challenge':src_tree,'mathlib_revision':MATHLIB, 'dependencies':{x['name']:x['rev'] for x in manifest['packages']}, 'scope':'Private source/dependency staging only. No independent build or atlas extraction.'},indent=2)+'\n',encoding='utf-8') print('Private original source and pinned dependencies ready; no project build claimed.',flush=True) if __name__=='__main__':main()