"""Download verified frozen RIR cache archives from the public demo URL.""" from __future__ import annotations import argparse import json from pathlib import Path import tempfile import urllib.request from zipfile import ZipFile from .common import sha def main(): p=argparse.ArgumentParser();p.add_argument('--base-url',required=True);p.add_argument('--work',type=Path,required=True) a=p.parse_args();base=a.base_url.rstrip('/')+'/' with urllib.request.urlopen(base+'cache/manifest.json',timeout=60) as response:manifest=json.load(response) out=a.work/'rirs';out.mkdir(parents=True,exist_ok=True) with tempfile.TemporaryDirectory() as temp: for record in manifest['archives']: archive=Path(temp)/Path(record['path']).name urllib.request.urlretrieve(base+record['path'],archive) if sha(archive)!=record['sha256']:raise ValueError('Cache download checksum mismatch') with ZipFile(archive) as z: for name in z.namelist(): path=Path(name) if path.is_absolute() or len(path.parts)!=1 or path.suffix not in ('.json','.npy'): raise ValueError('Unexpected cache archive entry') z.extractall(out) print('Verified',record['path']) if __name__=='__main__':main()