|
| 1 | +from __future__ import annotations |
| 2 | +import argparse |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | +from typing import List |
| 8 | + |
| 9 | +PYTHON = sys.executable |
| 10 | +ROOT = Path(__file__).resolve().parent |
| 11 | + |
| 12 | + |
| 13 | +def run(cmd: List[str], dry: bool = False): |
| 14 | + print("+", " ".join(cmd)) |
| 15 | + if dry: |
| 16 | + return 0 |
| 17 | + return subprocess.call(cmd) |
| 18 | + |
| 19 | + |
| 20 | +def ensure(tool: str): |
| 21 | + return shutil.which(tool) is not None |
| 22 | + |
| 23 | + |
| 24 | +def cmd_test(dry): |
| 25 | + # pytest preferred |
| 26 | + if ensure("pytest"): |
| 27 | + return run([PYTHON, "-m", "pytest"], dry) |
| 28 | + return run([PYTHON, "-m", "unittest", "discover", "-v"], dry) |
| 29 | + |
| 30 | + |
| 31 | +def cmd_lint(dry): |
| 32 | + if ensure("ruff"): |
| 33 | + return run(["ruff", "check", "."], dry) |
| 34 | + if ensure("flake8"): |
| 35 | + return run(["flake8", "."], dry) |
| 36 | + if ensure("pylint"): |
| 37 | + # auto-detect python packages in repo |
| 38 | + packages = [p.name for p in ROOT.iterdir() if (p / "__init__.py").exists()] |
| 39 | + if packages: |
| 40 | + return run(["pylint"] + packages, dry) |
| 41 | + return run(["pylint", "."], dry) |
| 42 | + print("No linter installed.") |
| 43 | + return 1 |
| 44 | + |
| 45 | + |
| 46 | +def cmd_format(dry): |
| 47 | + if ensure("black"): |
| 48 | + return run(["black", "."], dry) |
| 49 | + print("Black not installed.") |
| 50 | + return 1 |
| 51 | + |
| 52 | + |
| 53 | +def cmd_mypy(dry): |
| 54 | + if ensure("mypy"): |
| 55 | + return run(["mypy", "."], dry) |
| 56 | + print("mypy not installed.") |
| 57 | + return 1 |
| 58 | + |
| 59 | + |
| 60 | +def cmd_docs(dry): |
| 61 | + if ensure("sphinx-build"): |
| 62 | + return run(["sphinx-build", "docs", "docs/_build"], dry) |
| 63 | + print("Sphinx not installed.") |
| 64 | + return 1 |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + parser = argparse.ArgumentParser() |
| 69 | + parser.add_argument( |
| 70 | + "command", |
| 71 | + nargs="?", |
| 72 | + default="help", |
| 73 | + choices=["help", "test", "lint", "format", "mypy", "docs", "all"], |
| 74 | + ) |
| 75 | + parser.add_argument("--dry-run", action="store_true") |
| 76 | + args = parser.parse_args() |
| 77 | + |
| 78 | + cmds = { |
| 79 | + "test": [cmd_test], |
| 80 | + "lint": [cmd_lint], |
| 81 | + "format": [cmd_format], |
| 82 | + "mypy": [cmd_mypy], |
| 83 | + "docs": [cmd_docs], |
| 84 | + "all": [cmd_format, cmd_lint, cmd_mypy, cmd_test, cmd_docs], |
| 85 | + } |
| 86 | + |
| 87 | + if args.command == "help": |
| 88 | + parser.print_help() |
| 89 | + return |
| 90 | + |
| 91 | + for task in cmds[args.command]: |
| 92 | + rc = task(args.dry_run) |
| 93 | + if rc: |
| 94 | + sys.exit(rc) |
| 95 | + |
| 96 | + sys.exit(0) |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments