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