|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +"""Validate downloaded release wheels against the requested release tag.""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import re |
| 13 | +import sys |
| 14 | +from collections import defaultdict |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +COMPONENT_TO_DISTRIBUTIONS: dict[str, set[str]] = { |
| 18 | + "cuda-core": {"cuda_core"}, |
| 19 | + "cuda-bindings": {"cuda_bindings"}, |
| 20 | + "cuda-pathfinder": {"cuda_pathfinder"}, |
| 21 | + "cuda-python": {"cuda_python"}, |
| 22 | + "all": {"cuda_core", "cuda_bindings", "cuda_pathfinder", "cuda_python"}, |
| 23 | +} |
| 24 | + |
| 25 | +TAG_PATTERNS = ( |
| 26 | + re.compile(r"^v(?P<version>\d+\.\d+\.\d+)"), |
| 27 | + re.compile(r"^cuda-core-v(?P<version>\d+\.\d+\.\d+)"), |
| 28 | + re.compile(r"^cuda-pathfinder-v(?P<version>\d+\.\d+\.\d+)"), |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def parse_args() -> argparse.Namespace: |
| 33 | + parser = argparse.ArgumentParser( |
| 34 | + description=( |
| 35 | + "Validate that wheel versions match the release tag. " |
| 36 | + "This rejects dev/local wheel versions for release uploads." |
| 37 | + ) |
| 38 | + ) |
| 39 | + parser.add_argument("git_tag", help="Release git tag (for example: v13.0.0)") |
| 40 | + parser.add_argument("component", choices=sorted(COMPONENT_TO_DISTRIBUTIONS.keys())) |
| 41 | + parser.add_argument("wheel_dir", help="Directory containing wheel files") |
| 42 | + return parser.parse_args() |
| 43 | + |
| 44 | + |
| 45 | +def version_from_tag(tag: str) -> str: |
| 46 | + for pattern in TAG_PATTERNS: |
| 47 | + match = pattern.match(tag) |
| 48 | + if match: |
| 49 | + return match.group("version") |
| 50 | + raise ValueError( |
| 51 | + "Unsupported git tag format " |
| 52 | + f"{tag!r}; expected tags beginning with vX.Y.Z, cuda-core-vX.Y.Z, " |
| 53 | + "or cuda-pathfinder-vX.Y.Z." |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def parse_wheel_dist_and_version(path: Path) -> tuple[str, str]: |
| 58 | + # Wheel name format starts with: {distribution}-{version}-... |
| 59 | + parts = path.stem.split("-") |
| 60 | + if len(parts) < 5: |
| 61 | + raise ValueError(f"Invalid wheel filename format: {path.name}") |
| 62 | + return parts[0], parts[1] |
| 63 | + |
| 64 | + |
| 65 | +def main() -> int: |
| 66 | + args = parse_args() |
| 67 | + expected_version = version_from_tag(args.git_tag) |
| 68 | + expected_distributions = COMPONENT_TO_DISTRIBUTIONS[args.component] |
| 69 | + wheel_dir = Path(args.wheel_dir) |
| 70 | + |
| 71 | + wheels = sorted(wheel_dir.glob("*.whl")) |
| 72 | + if not wheels: |
| 73 | + print(f"Error: No wheel files found in {wheel_dir}", file=sys.stderr) |
| 74 | + return 1 |
| 75 | + |
| 76 | + seen_versions: dict[str, set[str]] = defaultdict(set) |
| 77 | + errors: list[str] = [] |
| 78 | + |
| 79 | + for wheel in wheels: |
| 80 | + try: |
| 81 | + distribution, version = parse_wheel_dist_and_version(wheel) |
| 82 | + except ValueError as exc: |
| 83 | + errors.append(str(exc)) |
| 84 | + continue |
| 85 | + |
| 86 | + if distribution not in expected_distributions: |
| 87 | + continue |
| 88 | + |
| 89 | + seen_versions[distribution].add(version) |
| 90 | + |
| 91 | + if ".dev" in version or "+" in version: |
| 92 | + errors.append( |
| 93 | + f"{wheel.name}: wheel version {version!r} contains dev/local markers " |
| 94 | + "(.dev or +), which is not allowed for release uploads." |
| 95 | + ) |
| 96 | + |
| 97 | + if version != expected_version: |
| 98 | + errors.append( |
| 99 | + f"{wheel.name}: wheel version {version!r} does not match expected " |
| 100 | + f"release version {expected_version!r} from git tag {args.git_tag!r}." |
| 101 | + ) |
| 102 | + |
| 103 | + missing_distributions = sorted(expected_distributions - set(seen_versions)) |
| 104 | + if missing_distributions: |
| 105 | + errors.append("Missing expected component wheels in download set: " + ", ".join(missing_distributions)) |
| 106 | + |
| 107 | + for distribution, versions in sorted(seen_versions.items()): |
| 108 | + if len(versions) > 1: |
| 109 | + errors.append( |
| 110 | + f"Expected one release version for {distribution}, found multiple: " + ", ".join(sorted(versions)) |
| 111 | + ) |
| 112 | + |
| 113 | + if errors: |
| 114 | + print("Wheel validation failed:", file=sys.stderr) |
| 115 | + for error in errors: |
| 116 | + print(f" - {error}", file=sys.stderr) |
| 117 | + return 1 |
| 118 | + |
| 119 | + print( |
| 120 | + "Validated release wheels for component " |
| 121 | + f"{args.component} at version {expected_version} from tag {args.git_tag}." |
| 122 | + ) |
| 123 | + return 0 |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + raise SystemExit(main()) |
0 commit comments