"""Retrieve the two published source artifacts, checking pinned SHA-256 hashes.""" from __future__ import annotations import ftplib import hashlib import json import urllib.request import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] / "data/raw" def digest(path): with path.open("rb") as stream: return hashlib.file_digest(stream, "sha256").hexdigest() def main(): manifest = json.loads((ROOT / "SOURCES.json").read_text()) for item in manifest["downloads"]: destination = ROOT / item["file"] if destination.exists(): if digest(destination) != item["sha256"]: raise ValueError(f"Existing source has a different hash: {destination}") print(f"Verified existing source: {destination.name}") continue temporary = destination.with_suffix(destination.suffix + ".download") if item["protocol"] == "ftp": connection = ftplib.FTP(item["host"], timeout=45) connection.login() with temporary.open("wb") as out: connection.retrbinary("RETR " + item["remote_path"], out.write) connection.quit() else: with urllib.request.urlopen(item["url"], timeout=45) as response, temporary.open("wb") as out: while chunk := response.read(1024 * 1024): out.write(chunk) if digest(temporary) != item["sha256"]: raise ValueError(f"Downloaded source hash mismatch: {temporary}") temporary.replace(destination) print(f"Downloaded and verified: {destination.name}") archive = ROOT / "WO2025-137555-TAB-20250119-001.zip" with zipfile.ZipFile(archive) as handle: member = "OMNI-002WO_SEQLIST.xml" data = handle.read(member) if hashlib.sha256(data).hexdigest() != manifest["sequence_xml"]["sha256"]: raise ValueError("Filed XML hash mismatch") (ROOT / member).write_bytes(data) print("Filed ST.26 XML verified") if __name__ == "__main__": main()