|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Managed by nwarila/python-template — do not edit manually. |
| 3 | +# Source: https://github.com/nwarila/python-template |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import argparse |
| 8 | +import glob |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import tomllib |
| 14 | +from pathlib import Path |
| 15 | +from typing import Any |
| 16 | + |
| 17 | + |
| 18 | +def _load_pyproject() -> dict[str, Any]: |
| 19 | + path = Path("pyproject.toml") |
| 20 | + if not path.exists(): |
| 21 | + return {} |
| 22 | + with open(path, "rb") as f: |
| 23 | + return tomllib.load(f) |
| 24 | + |
| 25 | + |
| 26 | +def _tool(name: str) -> str: |
| 27 | + exe_dir = Path(sys.executable).resolve().parent |
| 28 | + candidates = [exe_dir / name, exe_dir / f"{name}.exe"] |
| 29 | + for candidate in candidates: |
| 30 | + if candidate.exists(): |
| 31 | + return str(candidate) |
| 32 | + return name |
| 33 | + |
| 34 | + |
| 35 | +def _run(cmd: list[str], label: str) -> int: |
| 36 | + print(f"\n--- {label} ---") |
| 37 | + result = subprocess.run(cmd) |
| 38 | + if result.returncode != 0 and os.environ.get("GITHUB_ACTIONS") == "true": |
| 39 | + print(f"::error::{label} failed with exit code {result.returncode}") |
| 40 | + return result.returncode |
| 41 | + |
| 42 | + |
| 43 | +def _cleanup() -> None: |
| 44 | + shutil.rmtree("dist", ignore_errors=True) |
| 45 | + for egg_dir in glob.glob("*.egg-info"): |
| 46 | + shutil.rmtree(egg_dir, ignore_errors=True) |
| 47 | + for egg_dir in glob.glob("src/*.egg-info"): |
| 48 | + shutil.rmtree(egg_dir, ignore_errors=True) |
| 49 | + |
| 50 | + |
| 51 | +def main() -> int: |
| 52 | + argparse.ArgumentParser(description="Validate package build, metadata, and entry points.").parse_args() |
| 53 | + |
| 54 | + pyproject = _load_pyproject() |
| 55 | + |
| 56 | + if "build-system" not in pyproject: |
| 57 | + print("No [build-system] found, skipping package check") |
| 58 | + return 0 |
| 59 | + |
| 60 | + entry_points = pyproject.get("project", {}).get("scripts", {}) |
| 61 | + |
| 62 | + try: |
| 63 | + rc = _run([_tool("validate-pyproject"), "pyproject.toml"], "Validate pyproject.toml") |
| 64 | + if rc != 0: |
| 65 | + return rc |
| 66 | + |
| 67 | + rc = _run([sys.executable, "-m", "build"], "Build sdist+wheel") |
| 68 | + if rc != 0: |
| 69 | + return rc |
| 70 | + |
| 71 | + dist_files = glob.glob("dist/*") |
| 72 | + if not dist_files: |
| 73 | + is_ci = os.environ.get("GITHUB_ACTIONS") == "true" |
| 74 | + print("::error::No dist files produced" if is_ci else "ERROR: No dist files produced") |
| 75 | + return 1 |
| 76 | + |
| 77 | + rc = _run([_tool("twine"), "check", "--strict", *dist_files], "Twine Check") |
| 78 | + if rc != 0: |
| 79 | + return rc |
| 80 | + |
| 81 | + for name in entry_points: |
| 82 | + tool_path = shutil.which(name) or _tool(name) |
| 83 | + if shutil.which(name) is None and tool_path == name: |
| 84 | + print(f" Entry point '{name}' not found on PATH, skipping smoke test") |
| 85 | + continue |
| 86 | + rc = _run([tool_path, "--help"], f"Entry point: {name} --help") |
| 87 | + if rc != 0: |
| 88 | + return rc |
| 89 | + |
| 90 | + finally: |
| 91 | + _cleanup() |
| 92 | + |
| 93 | + return 0 |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + sys.exit(main()) |
0 commit comments