| 1 |
import inspect |
| 2 |
import os |
| 3 |
import platform |
| 4 |
import shlex |
| 5 |
import sys |
| 6 |
from collections.abc import Callable |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
here = Path(__file__).parent |
| 10 |
|
| 11 |
if platform.system() == "Windows": |
| 12 |
import mslex # type:ignore[import] |
| 13 |
|
| 14 |
shlex = mslex # noqa |
| 15 |
|
| 16 |
|
| 17 |
class Command: |
| 18 |
def __call__(self, bin_dir, args) -> None: ... |
| 19 |
|
| 20 |
|
| 21 |
def command(func) -> Callable: |
| 22 |
func.__is_command__ = True |
| 23 |
return func |
| 24 |
|
| 25 |
|
| 26 |
def run(*args) -> None: |
| 27 |
cmd = " ".join(shlex.quote(str(part)) for part in args) |
| 28 |
print(f"Running '{cmd}'") |
| 29 |
ret = os.system(cmd) |
| 30 |
if ret != 0: |
| 31 |
sys.exit(1) |
| 32 |
|
| 33 |
|
| 34 |
class App: |
| 35 |
def __init__(self): |
| 36 |
self.commands = {} |
| 37 |
|
| 38 |
compare = inspect.signature(type("C", (Command,), {})().__call__) |
| 39 |
|
| 40 |
for name in dir(self): |
| 41 |
val = getattr(self, name) |
| 42 |
if getattr(val, "__is_command__", False): |
| 43 |
assert ( |
| 44 |
inspect.signature(val) == compare |
| 45 |
), f"Expected '{name}' to have correct signature, have {inspect.signature(val)} instead of {compare}" |
| 46 |
self.commands[name] = val |
| 47 |
|
| 48 |
def __call__(self, args) -> None: |
| 49 |
bin_dir = Path(sys.executable).parent |
| 50 |
|
| 51 |
if args and args[0] in self.commands: |
| 52 |
os.chdir(here.parent) |
| 53 |
self.commands[args[0]](bin_dir, args[1:]) |
| 54 |
return |
| 55 |
|
| 56 |
sys.exit(f"Unknown command:\nAvailable: {sorted(self.commands)}\nWanted: {args}") |
| 57 |
|
| 58 |
@command |
| 59 |
def format(self, bin_dir, args) -> None: |
| 60 |
if not args: |
| 61 |
args = [".", *args] |
| 62 |
run(bin_dir / "black", *args) |
| 63 |
run(bin_dir / "isort", *args) |
| 64 |
|
| 65 |
@command |
| 66 |
def lint(self, bin_dir, args) -> None: |
| 67 |
run(bin_dir / "pylama", *args) |
| 68 |
|
| 69 |
@command |
| 70 |
def tests(self, bin_dir, args) -> None: |
| 71 |
if "-q" not in args: |
| 72 |
args = ["-q", *args] |
| 73 |
|
| 74 |
env = os.environ |
| 75 |
env["NOSE_OF_YETI_BLACK_COMPAT"] = "false" |
| 76 |
|
| 77 |
files = [] |
| 78 |
if "TESTS_CHDIR" in env: |
| 79 |
ags = [] |
| 80 |
test_dir = Path(env["TESTS_CHDIR"]).absolute() |
| 81 |
for a in args: |
| 82 |
test_name = "" |
| 83 |
if "::" in a: |
| 84 |
filename, test_name = a.split("::", 1) |
| 85 |
else: |
| 86 |
filename = a |
| 87 |
try: |
| 88 |
p = Path(filename).absolute() |
| 89 |
except: |
| 90 |
ags.append(a) |
| 91 |
else: |
| 92 |
if p.exists(): |
| 93 |
rel = p.relative_to(test_dir) |
| 94 |
if test_name: |
| 95 |
files.append(f"{rel}::{test_name}") |
| 96 |
else: |
| 97 |
files.append(str(rel)) |
| 98 |
else: |
| 99 |
ags.append(a) |
| 100 |
args = ags |
| 101 |
os.chdir(test_dir) |
| 102 |
|
| 103 |
run(bin_dir / "pytest", *files, *args) |
| 104 |
|
| 105 |
|
| 106 |
app = App() |
| 107 |
|
| 108 |
if __name__ == "__main__": |
| 109 |
app(sys.argv[1:]) |