|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Run pycycle using settings from ``[tool.pycycle]`` in the repository ``pyproject.toml``. |
| 4 | +
|
| 5 | +Upstream ``pycycle --source <path>`` is unusable with current Click (the option is typed as a |
| 6 | +boolean), so this script invokes ``pycycle --here`` with the working directory set to |
| 7 | +``resolve_dir`` (typically ``src`` for this layout). |
| 8 | +
|
| 9 | +Usage:: |
| 10 | +
|
| 11 | + ./.venv/Scripts/python.exe dev/scripts/run_pycycle.py |
| 12 | + ./.venv/Scripts/python.exe dev/scripts/run_pycycle.py --verbose |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import argparse |
| 18 | +import shutil |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from collections.abc import Mapping |
| 22 | +from pathlib import Path |
| 23 | +from typing import Any |
| 24 | + |
| 25 | +import tomli |
| 26 | + |
| 27 | + |
| 28 | +def _find_pyproject(start: Path) -> Path: |
| 29 | + for directory in (start, *start.parents): |
| 30 | + candidate = directory / "pyproject.toml" |
| 31 | + if candidate.is_file(): |
| 32 | + return candidate |
| 33 | + msg = "pyproject.toml not found (expected an ancestor of this script)." |
| 34 | + raise SystemExit(msg) |
| 35 | + |
| 36 | + |
| 37 | +def _load_pycycle_config(pyproject_path: Path) -> dict[str, Any]: |
| 38 | + with pyproject_path.open("rb") as handle: |
| 39 | + data = tomli.load(handle) |
| 40 | + tool = data.get("tool") or {} |
| 41 | + if not isinstance(tool, Mapping): |
| 42 | + return {} |
| 43 | + pycycle = tool.get("pycycle") or {} |
| 44 | + if not isinstance(pycycle, Mapping): |
| 45 | + return {} |
| 46 | + return dict(pycycle) |
| 47 | + |
| 48 | + |
| 49 | +def _pycycle_executable() -> str: |
| 50 | + scripts_dir = Path(sys.executable).resolve().parent |
| 51 | + for name in ("pycycle.exe", "pycycle"): |
| 52 | + candidate = scripts_dir / name |
| 53 | + if candidate.is_file(): |
| 54 | + return str(candidate) |
| 55 | + found = shutil.which("pycycle") |
| 56 | + if found: |
| 57 | + return found |
| 58 | + msg = ( |
| 59 | + "pycycle executable not found. Install dev dependencies, for example: " |
| 60 | + "pip install -e .[dev]" |
| 61 | + ) |
| 62 | + raise SystemExit(msg) |
| 63 | + |
| 64 | + |
| 65 | +def main() -> int: |
| 66 | + parser = argparse.ArgumentParser(description=__doc__) |
| 67 | + parser.add_argument( |
| 68 | + "--verbose", |
| 69 | + action="store_true", |
| 70 | + help="Verbose pycycle output (overrides [tool.pycycle].verbose when set).", |
| 71 | + ) |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + script_path = Path(__file__).resolve() |
| 75 | + pyproject_path = _find_pyproject(script_path.parent) |
| 76 | + repo_root = pyproject_path.parent |
| 77 | + cfg = _load_pycycle_config(pyproject_path) |
| 78 | + |
| 79 | + resolve_dir = str(cfg.get("resolve_dir") or "src") |
| 80 | + encoding = cfg.get("encoding") |
| 81 | + ignore = str(cfg.get("ignore") or "") |
| 82 | + verbose = bool(cfg.get("verbose")) or args.verbose |
| 83 | + |
| 84 | + target = (repo_root / resolve_dir).resolve() |
| 85 | + try: |
| 86 | + target.relative_to(repo_root.resolve()) |
| 87 | + except ValueError: |
| 88 | + msg = f"resolve_dir {resolve_dir!r} escapes repository root." |
| 89 | + raise SystemExit(msg) |
| 90 | + if not target.is_dir(): |
| 91 | + msg = f"Analysis directory does not exist: {target}" |
| 92 | + raise SystemExit(msg) |
| 93 | + |
| 94 | + cmd: list[str] = [_pycycle_executable(), "--here"] |
| 95 | + if encoding: |
| 96 | + cmd.extend(["--encoding", str(encoding)]) |
| 97 | + stripped_ignore = ignore.strip() |
| 98 | + if stripped_ignore: |
| 99 | + cmd.extend(["--ignore", stripped_ignore]) |
| 100 | + if verbose: |
| 101 | + cmd.append("--verbose") |
| 102 | + |
| 103 | + completed = subprocess.run(cmd, cwd=target, check=False) |
| 104 | + return int(completed.returncode) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + raise SystemExit(main()) |
0 commit comments