"""Prepare the official, digest-pinned Linux Lean release in a private directory.""" import argparse from concurrent.futures import ThreadPoolExecutor, as_completed import hashlib import json from pathlib import Path import subprocess URL = 'https://github.com/leanprover/lean4/releases/download/v4.33.1/lean-4.33.1-linux.tar.zst' SIZE = 570405234 SHA256 = '890afd185370f85666025b883914ab4f4b339136f8c96167b69cfb62aecaf235' CHUNK = 8 * 1024 * 1024 def windows_path(path): return subprocess.check_output(['wslpath', '-w', str(path.resolve())], text=True).strip() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--private-root', required=True, type=Path) parser.add_argument('--report', required=True, type=Path) args = parser.parse_args() root = args.private_root.resolve() chunks = root / 'lean-release-chunks'; chunks.mkdir(parents=True, exist_ok=True) ranges = [(start, min(start + CHUNK, SIZE) - 1) for start in range(0, SIZE, CHUNK)] def download(pair): start, end = pair target = chunks / f'{start:012d}-{end:012d}.bin' if target.exists() and target.stat().st_size == end - start + 1: return target partial = target.with_suffix('.partial'); headers = target.with_suffix('.headers') subprocess.run(['curl','-fsSL','--max-time','180','--retry','2', '-r',f'{start}-{end}','-D',str(headers),'-o',str(partial), URL + f'?shoal-range={start}-{end}'],check=True) text = headers.read_text().lower().replace('\r','') expected = f'content-range: bytes {start}-{end}/{SIZE}' if expected not in text or partial.stat().st_size != end - start + 1: raise ValueError('server did not honor the requested release range') partial.replace(target) return target print('progress: 0/100 downloading digest-pinned official Lean release',flush=True) with ThreadPoolExecutor(max_workers=16) as pool: jobs = [pool.submit(download, pair) for pair in ranges] for count, job in enumerate(as_completed(jobs), 1): job.result() print(f'progress: {int(75 * count / len(ranges))}/100 downloaded {count}/{len(ranges)} release chunks',flush=True) archive = root / 'lean-4.33.1-linux.tar.zst' h = hashlib.sha256() with archive.open('wb') as output: for start,end in ranges: with (chunks / f'{start:012d}-{end:012d}.bin').open('rb') as stream: while block := stream.read(1024 * 1024): h.update(block); output.write(block) if archive.stat().st_size != SIZE or h.hexdigest() != SHA256: raise ValueError('official release digest/size mismatch; nothing extracted') print('progress: 80/100 official release digest matched; extracting private toolchain',flush=True) # Windows libarchive can unpack zstd without installing system packages in WSL. subprocess.run(['tar.exe','-xf',windows_path(archive),'-C',windows_path(root)],check=True) binary = root / 'lean-4.33.1-linux/bin/lean' version = subprocess.check_output([str(binary),'--version'],text=True).strip() if '4.33.1' not in version or '819816b2' not in version: raise ValueError('installed Lean does not match the project pin: '+version) report = {'release_url':URL,'bytes':SIZE,'sha256':SHA256,'lean_version':version, 'scope':'Official toolchain only; project and Mathlib have not been built.'} args.report.write_text(json.dumps(report,indent=2)+'\n') print('progress: 100/100 pinned Lean toolchain ready; project build not yet performed',flush=True) if __name__ == '__main__': main()