| @@ -0,0 +1,83 @@ |
| 1 | +"""Download original published compiled artifacts privately; do not execute them.""" |
| 2 | +import argparse |
| 3 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 4 | +import hashlib |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +import subprocess |
| 8 | + |
| 9 | +REVISION = '9036e64326acf4acfae159b96664e9708445c1d1' |
| 10 | +BASE = f'https://huggingface.co/datasets/JackMcCarthy/riemann-hypothesis-build/resolve/{REVISION}' |
| 11 | +FILES = { |
| 12 | + 'olean-manifest.sha256': (64388496, 'fb3609ad73a17102e6e3e2346c5358a9c0957bcf5945442a7e5ffaa00c441ff5'), |
| 13 | + 'oleans.tar.zst': (28638044751, '953bab5386a7849527a11960f80a2fab427c49c637d6e94cc75788dd8f40196b'), |
| 14 | +} |
| 15 | +MAIN_SHA256 = '9146dfb4148e24d2252e04c50742f3e49dd126dfcc5006d10bf5f873ddf796ad' |
| 16 | + |
| 17 | +def digest(path): |
| 18 | + with path.open('rb') as stream: return hashlib.file_digest(stream,'sha256').hexdigest() |
| 19 | + |
| 20 | +def fetch(root, name, workers): |
| 21 | + size, expected = FILES[name] |
| 22 | + target = root / name |
| 23 | + if target.exists() and target.stat().st_size == size and digest(target) == expected: |
| 24 | + return target |
| 25 | + chunk_size = 2 * 1024 * 1024 if name.endswith('.sha256') else 64 * 1024 * 1024 |
| 26 | + pairs = [(start,min(start + chunk_size,size)-1) for start in range(0,size,chunk_size)] |
| 27 | + directory = root / (name + '.chunks'); directory.mkdir(parents=True,exist_ok=True) |
| 28 | + def one(pair): |
| 29 | + start,end = pair |
| 30 | + p = directory / f'{start:012d}-{end:012d}.bin' |
| 31 | + if p.exists() and p.stat().st_size == end-start+1: return |
| 32 | + partial = p.with_suffix('.partial'); headers = p.with_suffix('.headers') |
| 33 | + subprocess.run(['curl','-fsSL','--max-time','600','--retry','4','--retry-all-errors', |
| 34 | + '--retry-delay','3','-r',f'{start}-{end}','-D',str(headers),'-o',str(partial), |
| 35 | + BASE + '/' + name + f'?download=true&source-range={start}-{end}'],check=True) |
| 36 | + if (f'content-range: bytes {start}-{end}/{size}' not in headers.read_text().lower() |
| 37 | + or partial.stat().st_size != end-start+1): |
| 38 | + raise ValueError('incorrect range response for original published cache') |
| 39 | + partial.replace(p) |
| 40 | + errors=[] |
| 41 | + with ThreadPoolExecutor(max_workers=workers) as pool: |
| 42 | + jobs=[pool.submit(one,pair) for pair in pairs] |
| 43 | + for count,job in enumerate(as_completed(jobs),1): |
| 44 | + try: job.result() |
| 45 | + except Exception as error: errors.append(str(error)); print('Range failed; completed chunks retained for resume.',flush=True) |
| 46 | + print(f'progress: {count}/{len(jobs)} {name} range tasks finished',flush=True) |
| 47 | + if errors: raise RuntimeError(f'{len(errors)} ranges failed; rerun to reuse completed chunks') |
| 48 | + partial = target.with_suffix(target.suffix+'.verified-partial') |
| 49 | + h=hashlib.sha256() |
| 50 | + with partial.open('wb') as output: |
| 51 | + for start,end in pairs: |
| 52 | + with (directory/f'{start:012d}-{end:012d}.bin').open('rb') as stream: |
| 53 | + while data:=stream.read(4*1024*1024): output.write(data); h.update(data) |
| 54 | + if partial.stat().st_size != size or h.hexdigest()!=expected: |
| 55 | + raise ValueError('original cache archive digest mismatch; no extraction permitted') |
| 56 | + partial.replace(target) |
| 57 | + print(f'{name}: published size and SHA-256 matched',flush=True) |
| 58 | + return target |
| 59 | + |
| 60 | +def main(): |
| 61 | + parser=argparse.ArgumentParser(description=__doc__) |
| 62 | + parser.add_argument('--private-root',required=True,type=Path) |
| 63 | + parser.add_argument('--report',required=True,type=Path) |
| 64 | + parser.add_argument('--workers',type=int,default=32) |
| 65 | + args=parser.parse_args() |
| 66 | + if not 1<=args.workers<=32: parser.error('workers must be in 1..32') |
| 67 | + args.private_root.mkdir(parents=True,exist_ok=True) |
| 68 | + manifest=fetch(args.private_root,'olean-manifest.sha256',args.workers) |
| 69 | + entries={} |
| 70 | + for line in manifest.read_text().splitlines(): |
| 71 | + sha,path=line.split(maxsplit=1); path=path.lstrip('*').removeprefix('./') |
| 72 | + if path in entries: raise ValueError('duplicate compiled-file manifest path') |
| 73 | + entries[path]=sha |
| 74 | + if entries.get('.lake/build/lib/lean/Main.olean')!=MAIN_SHA256: |
| 75 | + raise ValueError('published Main.olean hash differs from the independent verification page') |
| 76 | + archive=fetch(args.private_root,'oleans.tar.zst',args.workers) |
| 77 | + report={'producer_revision':REVISION,'base_url':BASE,'files':{n:{'bytes':s,'sha256':h} for n,(s,h) in FILES.items()}, |
| 78 | + 'manifest_entries':len(entries),'main_olean_sha256':MAIN_SHA256, |
| 79 | + 'scope':'Published compiled-cache bytes only; not extracted, executed, independently rebuilt or used to produce an atlas.'} |
| 80 | + args.report.write_text(json.dumps(report,indent=2)+'\n') |
| 81 | + print('Original cache download verified. No independent rebuild or declaration extraction performed.',flush=True) |
| 82 | + |
| 83 | +if __name__=='__main__':main() |