|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify built distributions include LICENSE and NOTICE files.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import sys |
| 7 | +import tarfile |
| 8 | +import zipfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +REQUIRED_BASENAMES = {"LICENSE", "NOTICE"} |
| 13 | + |
| 14 | + |
| 15 | +def list_archive_names(archive_path: Path) -> list[str]: |
| 16 | + if archive_path.suffix == ".whl": |
| 17 | + with zipfile.ZipFile(archive_path) as archive: |
| 18 | + return archive.namelist() |
| 19 | + |
| 20 | + if archive_path.suffixes[-2:] == [".tar", ".gz"]: |
| 21 | + with tarfile.open(archive_path) as archive: |
| 22 | + return archive.getnames() |
| 23 | + |
| 24 | + raise ValueError(f"Unsupported distribution type: {archive_path}") |
| 25 | + |
| 26 | + |
| 27 | +def verify_package(package_dir: Path) -> list[str]: |
| 28 | + dist_dir = package_dir / "dist" |
| 29 | + errors: list[str] = [] |
| 30 | + |
| 31 | + if not dist_dir.is_dir(): |
| 32 | + return [f"{package_dir}: missing dist directory"] |
| 33 | + |
| 34 | + archives = sorted( |
| 35 | + path |
| 36 | + for path in dist_dir.iterdir() |
| 37 | + if path.is_file() |
| 38 | + and (path.suffix == ".whl" or path.suffixes[-2:] == [".tar", ".gz"]) |
| 39 | + ) |
| 40 | + if not archives: |
| 41 | + return [f"{package_dir}: no built distributions found"] |
| 42 | + |
| 43 | + for archive_path in archives: |
| 44 | + names = list_archive_names(archive_path) |
| 45 | + basenames = {Path(name).name for name in names} |
| 46 | + missing = sorted(REQUIRED_BASENAMES - basenames) |
| 47 | + if missing: |
| 48 | + errors.append(f"{archive_path}: missing {', '.join(missing)}") |
| 49 | + |
| 50 | + return errors |
| 51 | + |
| 52 | + |
| 53 | +def main(argv: list[str]) -> int: |
| 54 | + if len(argv) < 2: |
| 55 | + print( |
| 56 | + "usage: check_dist_legal_files.py <package-dir> [<package-dir> ...]", |
| 57 | + file=sys.stderr, |
| 58 | + ) |
| 59 | + return 2 |
| 60 | + |
| 61 | + errors: list[str] = [] |
| 62 | + for arg in argv[1:]: |
| 63 | + errors.extend(verify_package(Path(arg))) |
| 64 | + |
| 65 | + if errors: |
| 66 | + for error in errors: |
| 67 | + print(error, file=sys.stderr) |
| 68 | + return 1 |
| 69 | + |
| 70 | + for arg in argv[1:]: |
| 71 | + print(f"{arg}: LICENSE and NOTICE found in all distributions") |
| 72 | + return 0 |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + raise SystemExit(main(sys.argv)) |
0 commit comments