diff --git a/.github/workflows/cli-wheels.yml b/.github/workflows/cli-wheels.yml index 323cb831b..dcc866459 100644 --- a/.github/workflows/cli-wheels.yml +++ b/.github/workflows/cli-wheels.yml @@ -71,7 +71,9 @@ jobs: with: python-version: '3.13' - name: Test stable publishing checks - run: bash ci/cli_wheel/test_publish_checks.sh + run: | + bash ci/cli_wheel/test_publish_checks.sh + python -m unittest discover -s ci/cli_wheel -p test_verify_pypi.py - run: python -m pip install wheel==0.48.0 build - name: Build wheels from published release archives env: @@ -191,7 +193,13 @@ jobs: concurrency: pypi-lancedb-tools-${{ github.event.workflow_run.head_branch || inputs.tag }} permissions: id-token: write + contents: read steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + sparse-checkout: ci/cli_wheel/verify_pypi.py + sparse-checkout-cone-mode: false - uses: actions/download-artifact@v8 with: pattern: lancedb-tools-* @@ -203,11 +211,4 @@ jobs: # A retry of this job can finish a partially uploaded release. skip-existing: true - name: Verify the published distribution checksums - working-directory: dist - run: | - curl --fail --silent --show-error --retry 5 --retry-all-errors \ - "https://pypi.org/pypi/lancedb-tools/${CLI_TAG#v}/json" -o "$RUNNER_TEMP/pypi.json" - for file in *.whl *.tar.gz; do - digest=$(jq -er --arg file "$file" '.urls[] | select(.filename == $file) | .digests.sha256' "$RUNNER_TEMP/pypi.json") - echo "$digest $file" | sha256sum --check - done + run: python3 ci/cli_wheel/verify_pypi.py "${CLI_TAG#v}" dist diff --git a/ci/cli_wheel/test_verify_pypi.py b/ci/cli_wheel/test_verify_pypi.py new file mode 100644 index 000000000..46baaf2ee --- /dev/null +++ b/ci/cli_wheel/test_verify_pypi.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright The LanceDB Authors + +import subprocess +import unittest +from unittest.mock import patch + +import verify_pypi + +EXPECTED = {"cli.whl": "a" * 64, "cli.tar.gz": "b" * 64} + + +def metadata(files): + return { + "urls": [ + {"filename": name, "digests": {"sha256": digest}} + for name, digest in files.items() + ] + } + + +class VerifyPyPITest(unittest.TestCase): + def setUp(self): + self.fetch = self.enterContext(patch.object(verify_pypi, "fetch_metadata")) + self.sleep = self.enterContext(patch.object(verify_pypi.time, "sleep")) + + def test_complete_metadata(self): + self.fetch.return_value = metadata(EXPECTED) + verify_pypi.verify("0.42.1", EXPECTED) + self.fetch.assert_called_once_with("0.42.1") + self.sleep.assert_not_called() + + def test_partial_metadata_then_complete(self): + self.fetch.side_effect = [metadata({"cli.whl": "a" * 64}), metadata(EXPECTED)] + verify_pypi.verify("0.42.1", EXPECTED) + self.assertEqual(self.fetch.call_count, 2) + self.sleep.assert_called_once_with(verify_pypi.RETRY_DELAY) + + def test_mismatch_is_fatal_even_when_another_file_is_missing(self): + self.fetch.return_value = metadata({"cli.tar.gz": "c" * 64}) + with self.assertRaisesRegex(SystemExit, r"cli\.tar\.gz: SHA-256 mismatch"): + verify_pypi.verify("0.42.1", EXPECTED) + self.fetch.assert_called_once_with("0.42.1") + self.sleep.assert_not_called() + + def test_persistent_missing_file_exhausts_budget(self): + self.fetch.return_value = metadata({"cli.whl": "a" * 64}) + with self.assertRaisesRegex(SystemExit, r"missing from PyPI: cli\.tar\.gz"): + verify_pypi.verify("0.42.1", EXPECTED) + self.assertEqual(self.fetch.call_count, verify_pypi.MAX_ATTEMPTS) + self.assertEqual(self.sleep.call_count, verify_pypi.MAX_ATTEMPTS - 1) + + def test_transient_request_error(self): + self.fetch.side_effect = [ + subprocess.CalledProcessError(22, "curl"), + metadata(EXPECTED), + ] + verify_pypi.verify("0.42.1", EXPECTED) + self.sleep.assert_called_once_with(verify_pypi.RETRY_DELAY) diff --git a/ci/cli_wheel/verify_pypi.py b/ci/cli_wheel/verify_pypi.py new file mode 100644 index 000000000..29bfb4ced --- /dev/null +++ b/ci/cli_wheel/verify_pypi.py @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright The LanceDB Authors + +"""Verify uploaded CLI distributions against the version-specific PyPI metadata.""" + +import hashlib +import json +import subprocess +import sys +import time +from pathlib import Path + +MAX_ATTEMPTS = 8 +RETRY_DELAY = 15 + + +def fetch_metadata(version): + url = f"https://pypi.org/pypi/lancedb-tools/{version}/json" + return json.loads( + subprocess.check_output(["curl", "-fsS", "--max-time", "15", url]) + ) + + +def verify(version, expected): + for attempt in range(1, MAX_ATTEMPTS + 1): + try: + metadata = fetch_metadata(version) + except subprocess.CalledProcessError as exc: + reason = f"metadata request failed (curl exit {exc.returncode})" + else: + missing = set(expected) + for entry in metadata["urls"]: + name = entry["filename"] + if name in expected: + published = entry["digests"]["sha256"] + if published != expected[name]: + raise SystemExit( + f"{name}: SHA-256 mismatch " + f"(local {expected[name]}, PyPI {published})" + ) + missing.discard(name) + if not missing: + print(f"Verified all {len(expected)} distribution checksums on PyPI.") + return + reason = f"missing from PyPI: {', '.join(sorted(missing))}" + + print(f"Attempt {attempt}/{MAX_ATTEMPTS}: {reason}", flush=True) + if attempt == MAX_ATTEMPTS: + raise SystemExit(f"Could not verify lancedb-tools {version}: {reason}") + time.sleep(RETRY_DELAY) + + +if __name__ == "__main__": + version, directory = sys.argv[1:] + dist = Path(directory) + wheels = sorted(dist.glob("*.whl")) + sources = sorted(dist.glob("*.tar.gz")) + if not wheels or not sources: + raise SystemExit(f"Expected wheels and a source archive in {dist}") + expected = {} + for path in wheels + sources: + with path.open("rb") as stream: + expected[path.name] = hashlib.file_digest(stream, "sha256").hexdigest() + verify(version, expected)