Debian Codegraph @

check_dist

raw
1 #!/usr/bin/env bash
2 #
3 # Copyright 2009-2026 Joshua Bronson. All rights reserved.
4 #
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 #
9 # Inspect built distributions before they are published. Nothing else does: the tests import
10 # from a wheel but never look inside one, and the release workflows went straight from `uv build`
11 # to publishing. That is how 0.24.0 shipped a wheel whose RECORD listed hex-encoded hashes
12 # instead of the urlsafe-base64 the spec requires (#406) with every check still green.
13 #
14 # Usage: ./check_dist [dist_dir] (dist_dir defaults to ./dist, where `uv build` writes)
15
16 set -euo pipefail
17
18 log() {
19 >&2 printf "> %s\n" "$@"
20 }
21
22
23 main() {
24 local dist_dir=${1:-dist}
25 declare -r hint="Hint: Use 'nix develop' to bootstrap a development environment"
26
27 if ! command -v uv >/dev/null 2>&1; then
28 log "Error: No 'uv' on PATH. $hint"
29 exit 1
30 fi
31
32 shopt -s nullglob
33 local -a wheels=("$dist_dir"/*.whl)
34 local -a sdists=("$dist_dir"/*.tar.gz)
35 # Guard against a false pass: with no artifacts to check, everything below trivially succeeds.
36 if [[ ${#wheels[@]} -eq 0 || ${#sdists[@]} -eq 0 ]]; then
37 log "Error: expected at least one wheel and one sdist in '$dist_dir'," \
38 " but found ${#wheels[@]} and ${#sdists[@]}. Run 'uv build' first?"
39 exit 1
40 fi
41
42 # The check that #406 needed. installer recomputes the hash and size of every file in the
43 # wheel and compares them to what RECORD claims, so a RECORD that is malformed (not just
44 # stale) fails here on every entry.
45 log "Validating each wheel's RECORD..."
46 uv run --no-project --with installer python - "${wheels[@]}" <<'PY'
47 import sys
48
49 from installer.sources import WheelFile
50
51 for path in sys.argv[1:]:
52 try:
53 with WheelFile.open(path) as wheel:
54 wheel.validate_record()
55 except Exception as exc: # noqa: BLE001 (report the issues, not a traceback)
56 print(f'RECORD INVALID: {path}', file=sys.stderr)
57 for issue in getattr(exc, 'issues', None) or [exc]:
58 print(f' {issue}', file=sys.stderr)
59 sys.exit(1)
60 print(f'RECORD ok: {path}')
61 PY
62
63 # Orthogonal to the above: catches metadata that PyPI would reject or render wrong, e.g. a
64 # README that no longer parses as reStructuredText.
65 log "Checking metadata renders..."
66 uv run --no-project --with twine twine check "${wheels[@]}" "${sdists[@]}"
67
68 log "Distributions OK: ${#wheels[@]} wheel(s), ${#sdists[@]} sdist(s) in '$dist_dir'"
69 }
70
71 main "$@"