|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +import os.path |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import tempfile |
| 11 | +import urllib.parse |
| 12 | +import urllib.request |
| 13 | +import zipfile |
| 14 | +from collections.abc import Sequence |
| 15 | + |
| 16 | + |
| 17 | +def _get_metadata_bytes(filename: str) -> bytes: |
| 18 | + with zipfile.ZipFile(filename) as zipf: |
| 19 | + (metadata,) = ( |
| 20 | + name |
| 21 | + for name in zipf.namelist() |
| 22 | + if name.endswith(".dist-info/METADATA") and name.count("/") == 1 |
| 23 | + ) |
| 24 | + with zipf.open(metadata) as f: |
| 25 | + return f.read() |
| 26 | + |
| 27 | + |
| 28 | +def main(argv: Sequence[str] | None = None) -> int: |
| 29 | + parser = argparse.ArgumentParser() |
| 30 | + parser.add_argument("--pypi-url", required=True) |
| 31 | + args = parser.parse_args(argv) |
| 32 | + |
| 33 | + url = urllib.parse.urljoin(args.pypi_url, "packages.json") |
| 34 | + packages = [json.loads(line) for line in urllib.request.urlopen(url)] |
| 35 | + |
| 36 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 37 | + os.makedirs(f"{tmpdir}/metadata") |
| 38 | + for package in packages: |
| 39 | + basename = os.path.basename(package["filename"]) |
| 40 | + |
| 41 | + if package.get("core_metadata"): |
| 42 | + print(f"skipping: core metadata already present for {basename}") |
| 43 | + continue |
| 44 | + |
| 45 | + url = f"{args.pypi_url}/wheels/{basename}" |
| 46 | + fn = f"{tmpdir}/{basename}" |
| 47 | + |
| 48 | + with urllib.request.urlopen(url) as resp, open(fn, "wb") as f: |
| 49 | + shutil.copyfileobj(resp, f) |
| 50 | + |
| 51 | + metadata_bytes = _get_metadata_bytes(fn) |
| 52 | + metadata_sha256 = hashlib.sha256(metadata_bytes).hexdigest() |
| 53 | + |
| 54 | + with open(f"{tmpdir}/metadata/{basename}.metadata", "wb") as f: |
| 55 | + f.write(metadata_bytes) |
| 56 | + |
| 57 | + package["core_metadata"] = f"sha256={metadata_sha256}" |
| 58 | + print(f"core metadata fetched for {basename}") |
| 59 | + |
| 60 | + packages_json = os.path.join(tmpdir, "packages.json") |
| 61 | + with open(packages_json, "w") as f: |
| 62 | + for package in packages: |
| 63 | + f.write(f"{json.dumps(package)}\n") |
| 64 | + |
| 65 | + subprocess.check_call( |
| 66 | + ( |
| 67 | + "gcloud", |
| 68 | + "storage", |
| 69 | + "cp", |
| 70 | + "-n", # no-clobber |
| 71 | + "--cache-control", |
| 72 | + "public, max-age=3600", |
| 73 | + f"{tmpdir}/metadata/*", |
| 74 | + "gs://pypi.devinfra.sentry.io/wheels/", |
| 75 | + ) |
| 76 | + ) |
| 77 | + subprocess.check_call( |
| 78 | + ( |
| 79 | + "gcloud", |
| 80 | + "storage", |
| 81 | + "cp", |
| 82 | + # the packages.json file must be consistently read so no caching |
| 83 | + "--cache-control", |
| 84 | + "no-store", |
| 85 | + packages_json, |
| 86 | + "gs://pypi.devinfra.sentry.io", |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + return 0 |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + raise SystemExit(main()) |
0 commit comments