|
| 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 | +"""Resolve mini-CTK components from NVIDIA redistrib metadata.""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import json |
| 13 | +import sys |
| 14 | +import urllib.error |
| 15 | +import urllib.parse |
| 16 | +import urllib.request |
| 17 | +from pathlib import Path |
| 18 | +from typing import Any |
| 19 | + |
| 20 | +HOST_PLATFORM_TO_SUBDIR: dict[str, str] = { |
| 21 | + "linux-64": "linux-x86_64", |
| 22 | + "linux-aarch64": "linux-sbsa", |
| 23 | + "win-64": "windows-x86_64", |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +def host_platform_to_subdir(host_platform: str) -> str: |
| 28 | + try: |
| 29 | + return HOST_PLATFORM_TO_SUBDIR[host_platform] |
| 30 | + except KeyError as exc: |
| 31 | + raise ValueError(f"unsupported host-platform: {host_platform!r}") from exc |
| 32 | + |
| 33 | + |
| 34 | +def split_components(components: str) -> list[str]: |
| 35 | + return [component for component in components.split(",") if component] |
| 36 | + |
| 37 | + |
| 38 | +def filter_static_components(components: list[str], host_platform: str, cuda_version: str) -> list[str]: |
| 39 | + try: |
| 40 | + cuda_major = int(cuda_version.split(".", 1)[0]) |
| 41 | + except ValueError as exc: |
| 42 | + raise ValueError(f"invalid cuda-version: {cuda_version!r}") from exc |
| 43 | + |
| 44 | + filtered = [] |
| 45 | + for component in components: |
| 46 | + if component == "libnvjitlink" and cuda_major < 12: |
| 47 | + continue |
| 48 | + if component in {"cuda_crt", "libnvvm"} and cuda_major < 13: |
| 49 | + continue |
| 50 | + if component == "libcufile" and host_platform.startswith("win-"): |
| 51 | + continue |
| 52 | + filtered.append(component) |
| 53 | + return filtered |
| 54 | + |
| 55 | + |
| 56 | +def validate_metadata_url(metadata_url: str) -> str: |
| 57 | + parsed = urllib.parse.urlsplit(metadata_url) |
| 58 | + if parsed.scheme != "https" or not parsed.netloc: |
| 59 | + raise ValueError(f"metadata URL must be an https URL: {metadata_url!r}") |
| 60 | + return metadata_url |
| 61 | + |
| 62 | + |
| 63 | +def load_metadata(*, metadata_path: str | None, metadata_url: str | None) -> dict[str, Any]: |
| 64 | + if (metadata_path is None) == (metadata_url is None): |
| 65 | + raise ValueError("exactly one of --metadata-path or --metadata-url is required") |
| 66 | + |
| 67 | + if metadata_path is not None: |
| 68 | + return json.loads(Path(metadata_path).read_text(encoding="utf-8")) |
| 69 | + |
| 70 | + assert metadata_url is not None |
| 71 | + metadata_url = validate_metadata_url(metadata_url) |
| 72 | + with urllib.request.urlopen(metadata_url) as response: # noqa: S310 - scheme is restricted to https above |
| 73 | + return json.load(response) |
| 74 | + |
| 75 | + |
| 76 | +def filter_components( |
| 77 | + metadata: dict[str, Any], |
| 78 | + *, |
| 79 | + host_platform: str, |
| 80 | + cuda_version: str, |
| 81 | + components: str, |
| 82 | +) -> tuple[list[str], list[str]]: |
| 83 | + ctk_subdir = host_platform_to_subdir(host_platform) |
| 84 | + filtered = [] |
| 85 | + skipped = [] |
| 86 | + for component in filter_static_components(split_components(components), host_platform, cuda_version): |
| 87 | + if ctk_subdir in metadata.get(component, {}): |
| 88 | + filtered.append(component) |
| 89 | + else: |
| 90 | + skipped.append(component) |
| 91 | + return filtered, skipped |
| 92 | + |
| 93 | + |
| 94 | +def get_component_relative_path(metadata: dict[str, Any], *, host_platform: str, component: str) -> str: |
| 95 | + ctk_subdir = host_platform_to_subdir(host_platform) |
| 96 | + component_info = metadata.get(component) |
| 97 | + if component_info is None: |
| 98 | + raise KeyError(f"unknown CTK component {component!r}") |
| 99 | + |
| 100 | + subdir_info = component_info.get(ctk_subdir) |
| 101 | + if subdir_info is None: |
| 102 | + raise KeyError(f"CTK component {component!r} is not available for redistrib subdir {ctk_subdir!r}") |
| 103 | + |
| 104 | + relative_path = subdir_info.get("relative_path") |
| 105 | + if relative_path is None: |
| 106 | + raise KeyError(f"CTK component {component!r} for redistrib subdir {ctk_subdir!r} is missing 'relative_path'") |
| 107 | + return relative_path |
| 108 | + |
| 109 | + |
| 110 | +def parse_args(argv: list[str] | None = None) -> argparse.Namespace: |
| 111 | + parser = argparse.ArgumentParser(description=__doc__) |
| 112 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 113 | + |
| 114 | + filter_parser = subparsers.add_parser("filter-components") |
| 115 | + filter_parser.add_argument("--host-platform", required=True) |
| 116 | + filter_parser.add_argument("--cuda-version", required=True) |
| 117 | + filter_parser.add_argument("--components", required=True) |
| 118 | + filter_parser.add_argument("--metadata-path") |
| 119 | + filter_parser.add_argument("--metadata-url") |
| 120 | + |
| 121 | + relpath_parser = subparsers.add_parser("component-relative-path") |
| 122 | + relpath_parser.add_argument("--host-platform", required=True) |
| 123 | + relpath_parser.add_argument("--component", required=True) |
| 124 | + relpath_parser.add_argument("--metadata-path") |
| 125 | + relpath_parser.add_argument("--metadata-url") |
| 126 | + |
| 127 | + return parser.parse_args(argv) |
| 128 | + |
| 129 | + |
| 130 | +def main(argv: list[str] | None = None) -> int: |
| 131 | + args = parse_args(argv) |
| 132 | + |
| 133 | + try: |
| 134 | + metadata = load_metadata(metadata_path=args.metadata_path, metadata_url=args.metadata_url) |
| 135 | + |
| 136 | + if args.command == "filter-components": |
| 137 | + filtered, skipped = filter_components( |
| 138 | + metadata, |
| 139 | + host_platform=args.host_platform, |
| 140 | + cuda_version=args.cuda_version, |
| 141 | + components=args.components, |
| 142 | + ) |
| 143 | + for component in skipped: |
| 144 | + print( |
| 145 | + f"Skipping unsupported CTK component {component!r} for host-platform {args.host_platform!r}", |
| 146 | + file=sys.stderr, |
| 147 | + ) |
| 148 | + print(",".join(filtered)) |
| 149 | + return 0 |
| 150 | + |
| 151 | + if args.command == "component-relative-path": |
| 152 | + print( |
| 153 | + get_component_relative_path( |
| 154 | + metadata, |
| 155 | + host_platform=args.host_platform, |
| 156 | + component=args.component, |
| 157 | + ) |
| 158 | + ) |
| 159 | + return 0 |
| 160 | + |
| 161 | + raise AssertionError(f"unexpected command: {args.command!r}") |
| 162 | + except (ValueError, KeyError, OSError, urllib.error.URLError, json.JSONDecodeError) as exc: |
| 163 | + print(f"ERROR: {exc}", file=sys.stderr) |
| 164 | + return 1 |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + raise SystemExit(main()) |
0 commit comments