|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Smoke-import every installed distribution's top-level modules. |
| 16 | +
|
| 17 | +This guards against shipping an environment whose installed packages cannot |
| 18 | +actually be imported -- for example an ABI or version skew between a vendored |
| 19 | +shared library and its Python bindings. ``pip check`` only validates declared |
| 20 | +version ranges; it never exercises an import, so a nominally-satisfied |
| 21 | +dependency that crashes the moment it is imported slips through. This script |
| 22 | +closes that gap: it enumerates installed distributions, resolves each one's |
| 23 | +top-level importable modules, imports every module in an isolated subprocess, |
| 24 | +and exits non-zero if any import raises (or hangs). |
| 25 | +
|
| 26 | +Run with no arguments to check the active interpreter's environment:: |
| 27 | +
|
| 28 | + python import_check.py |
| 29 | +
|
| 30 | +Because an image is built without a GPU, packages that require a driver or a |
| 31 | +host library at import time are not importable at build time. List those in a |
| 32 | +skip file (one module per line, ``#`` comments allowed) and pass it via |
| 33 | +``--skip-file``; every other import error then fails the build. |
| 34 | +""" |
| 35 | + |
| 36 | +from __future__ import annotations |
| 37 | + |
| 38 | +import argparse |
| 39 | +import importlib.metadata as md |
| 40 | +import subprocess |
| 41 | +import sys |
| 42 | +from concurrent.futures import ThreadPoolExecutor |
| 43 | +from dataclasses import dataclass |
| 44 | +from pathlib import Path |
| 45 | + |
| 46 | + |
| 47 | +@dataclass(frozen=True) |
| 48 | +class ImportResult: |
| 49 | + """Outcome of importing a single top-level module. |
| 50 | +
|
| 51 | + Attributes: |
| 52 | + module: The top-level module name that was imported. |
| 53 | + ok: True when the import subprocess exited cleanly. |
| 54 | + detail: Trailing diagnostic output when the import failed, else "". |
| 55 | + """ |
| 56 | + |
| 57 | + module: str |
| 58 | + ok: bool |
| 59 | + detail: str |
| 60 | + |
| 61 | + |
| 62 | +def _clean_module_name(filename: str) -> str | None: |
| 63 | + """Reduce a top-level filename to the module name Python would import. |
| 64 | +
|
| 65 | + Extension modules carry an ABI tag (``foo.cpython-312-x86_64-linux-gnu.so``) |
| 66 | + that is not part of the import name, so everything from the first dot is |
| 67 | + dropped. Pure-Python files lose their ``.py`` suffix. |
| 68 | +
|
| 69 | + Args: |
| 70 | + filename: The basename of a top-level file. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + The import name, or None when the file is not an importable module. |
| 74 | + """ |
| 75 | + if filename.endswith((".so", ".pyd")): |
| 76 | + return filename.split(".", 1)[0] |
| 77 | + if filename.endswith(".py"): |
| 78 | + return filename[:-3] |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def _modules_from_files(dist: md.Distribution) -> set[str]: |
| 83 | + """Derive importable top-level modules from a distribution's file list. |
| 84 | +
|
| 85 | + Deriving from the recorded files (rather than ``top_level.txt``) reflects |
| 86 | + what is actually on disk, which avoids both stale metadata entries that name |
| 87 | + non-importable helpers and ABI-tagged extension filenames. |
| 88 | +
|
| 89 | + Args: |
| 90 | + dist: The installed distribution to inspect. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + The set of importable top-level module names. |
| 94 | + """ |
| 95 | + modules: set[str] = set() |
| 96 | + for entry in dist.files or []: |
| 97 | + parts = entry.parts |
| 98 | + if not parts: |
| 99 | + continue |
| 100 | + head = parts[0] |
| 101 | + if head.endswith((".dist-info", ".data", ".egg-info")) or head == "__pycache__": |
| 102 | + continue |
| 103 | + if len(parts) == 1: |
| 104 | + name = _clean_module_name(head) |
| 105 | + candidate = name if name and name != "__init__" else None |
| 106 | + else: |
| 107 | + candidate = head if entry.suffix in {".py", ".so", ".pyd"} else None |
| 108 | + if candidate and candidate.isidentifier(): |
| 109 | + modules.add(candidate) |
| 110 | + return modules |
| 111 | + |
| 112 | + |
| 113 | +def _modules_from_top_level_txt(dist: md.Distribution) -> set[str]: |
| 114 | + """Return importable module names declared in ``top_level.txt``. |
| 115 | +
|
| 116 | + Used only when the file list is unavailable. |
| 117 | +
|
| 118 | + Args: |
| 119 | + dist: The installed distribution to inspect. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + The set of top-level module names that are valid identifiers. |
| 123 | + """ |
| 124 | + text = dist.read_text("top_level.txt") |
| 125 | + if not text: |
| 126 | + return set() |
| 127 | + return {line.strip() for line in text.splitlines() if line.strip().isidentifier()} |
| 128 | + |
| 129 | + |
| 130 | +def discover_modules(skip: set[str]) -> dict[str, list[str]]: |
| 131 | + """Map each importable top-level module to the distributions providing it. |
| 132 | +
|
| 133 | + Args: |
| 134 | + skip: Module names to exclude from the result. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + A mapping of module name to the sorted list of distribution names that |
| 138 | + provide it, excluding any module in ``skip``. |
| 139 | + """ |
| 140 | + providers: dict[str, set[str]] = {} |
| 141 | + for dist in md.distributions(): |
| 142 | + name = dist.metadata["Name"] or "<unknown>" |
| 143 | + modules = _modules_from_files(dist) or _modules_from_top_level_txt(dist) |
| 144 | + for module in modules: |
| 145 | + if module in skip: |
| 146 | + continue |
| 147 | + providers.setdefault(module, set()).add(name) |
| 148 | + return {module: sorted(names) for module, names in providers.items()} |
| 149 | + |
| 150 | + |
| 151 | +def import_one(module: str, timeout: float) -> ImportResult: |
| 152 | + """Import a single module in a fresh subprocess. |
| 153 | +
|
| 154 | + Isolation in a subprocess keeps a hard crash (segfault, ``os._exit``) or a |
| 155 | + hang in one module from aborting the whole sweep. |
| 156 | +
|
| 157 | + Args: |
| 158 | + module: The top-level module name to import. |
| 159 | + timeout: Seconds to allow before treating the import as hung. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + The :class:`ImportResult` for this module. |
| 163 | + """ |
| 164 | + try: |
| 165 | + proc = subprocess.run( |
| 166 | + [sys.executable, "-c", f"import {module}"], |
| 167 | + capture_output=True, |
| 168 | + text=True, |
| 169 | + timeout=timeout, |
| 170 | + ) |
| 171 | + except subprocess.TimeoutExpired: |
| 172 | + return ImportResult(module, False, f"timed out after {timeout:.0f}s") |
| 173 | + if proc.returncode == 0: |
| 174 | + return ImportResult(module, True, "") |
| 175 | + tail = "\n".join((proc.stderr or proc.stdout).strip().splitlines()[-3:]) |
| 176 | + return ImportResult(module, False, tail or f"exit code {proc.returncode}") |
| 177 | + |
| 178 | + |
| 179 | +def load_skip(skip_file: Path | None) -> set[str]: |
| 180 | + """Read a newline-delimited skip list, ignoring blanks and comments. |
| 181 | +
|
| 182 | + Args: |
| 183 | + skip_file: Path to the skip file, or None. |
| 184 | +
|
| 185 | + Returns: |
| 186 | + The set of module names to skip. |
| 187 | + """ |
| 188 | + if skip_file is None or not skip_file.exists(): |
| 189 | + return set() |
| 190 | + skip: set[str] = set() |
| 191 | + for line in skip_file.read_text().splitlines(): |
| 192 | + token = line.split("#", 1)[0].strip() |
| 193 | + if token: |
| 194 | + skip.add(token) |
| 195 | + return skip |
| 196 | + |
| 197 | + |
| 198 | +def run(skip: set[str], jobs: int, timeout: float) -> list[ImportResult]: |
| 199 | + """Import every discovered module and collect results. |
| 200 | +
|
| 201 | + Args: |
| 202 | + skip: Module names to exclude. |
| 203 | + jobs: Maximum number of concurrent import subprocesses. |
| 204 | + timeout: Per-import timeout in seconds. |
| 205 | +
|
| 206 | + Returns: |
| 207 | + The import results sorted by module name. |
| 208 | + """ |
| 209 | + modules = discover_modules(skip) |
| 210 | + with ThreadPoolExecutor(max_workers=jobs) as pool: |
| 211 | + results = pool.map(lambda m: import_one(m, timeout), sorted(modules)) |
| 212 | + return list(results) |
| 213 | + |
| 214 | + |
| 215 | +def report(results: list[ImportResult], skipped: set[str]) -> int: |
| 216 | + """Print a summary and return the process exit code. |
| 217 | +
|
| 218 | + Args: |
| 219 | + results: The import results to summarize. |
| 220 | + skipped: Module names that were skipped. |
| 221 | +
|
| 222 | + Returns: |
| 223 | + 1 if any import failed, otherwise 0. |
| 224 | + """ |
| 225 | + failures = [r for r in results if not r.ok] |
| 226 | + for failure in failures: |
| 227 | + print(f"FAIL {failure.module}") |
| 228 | + for line in failure.detail.splitlines(): |
| 229 | + print(f" {line}") |
| 230 | + passed = len(results) - len(failures) |
| 231 | + print(f"\nimport check: {passed} ok, {len(failures)} failed, {len(skipped)} skipped, {len(results)} total") |
| 232 | + return 1 if failures else 0 |
| 233 | + |
| 234 | + |
| 235 | +def main(argv: list[str] | None = None) -> int: |
| 236 | + """Entry point for the import-check CLI. |
| 237 | +
|
| 238 | + Args: |
| 239 | + argv: Argument vector, defaulting to ``sys.argv``. |
| 240 | +
|
| 241 | + Returns: |
| 242 | + The process exit code. |
| 243 | + """ |
| 244 | + parser = argparse.ArgumentParser(description="Smoke-import installed packages.") |
| 245 | + parser.add_argument("--skip-file", type=Path, default=None) |
| 246 | + parser.add_argument("--jobs", type=int, default=8) |
| 247 | + parser.add_argument("--timeout", type=float, default=120.0) |
| 248 | + args = parser.parse_args(argv) |
| 249 | + |
| 250 | + skip = load_skip(args.skip_file) |
| 251 | + results = run(skip, args.jobs, args.timeout) |
| 252 | + return report(results, skip) |
| 253 | + |
| 254 | + |
| 255 | +if __name__ == "__main__": |
| 256 | + raise SystemExit(main()) |
0 commit comments