-
-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathrunner.py
More file actions
81 lines (64 loc) · 2.5 KB
/
runner.py
File metadata and controls
81 lines (64 loc) · 2.5 KB
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
import sys
import traceback
from importlib import import_module
from inspect import isfunction, signature
from pkgutil import walk_packages
from ultimatepython import __name__ as root_name
from ultimatepython import __path__ as root_path
# Module-level constants
_STYLE_SUCCESS = "\033[92m"
_STYLE_FAILURE = "\033[91m"
_STYLE_BOLD = "\033[1m"
_STYLE_END = "\033[0m"
_RUNNER_PROGRESS = "->"
_RUNNER_MAIN = "main"
def style_text(text: str, color: str = "") -> str:
"""Get styled text."""
return f"{color}{_STYLE_BOLD}{text}{_STYLE_END}"
def main() -> None:
# Get filter from command line arguments
filter_str = sys.argv[1] if len(sys.argv) > 1 else None
print(style_text(f"Start {root_name} runner"))
stats = {"passed": 0, "failed": 0, "skipped": 0}
for item in walk_packages(root_path, f"{root_name}."):
# Skip packages (folders), only run modules (files)
if item.ispkg:
continue
# Filter modules based on command line argument
if filter_str and filter_str not in item.name:
continue
try:
mod = import_module(item.name)
except (ImportError, SyntaxError):
print(f"{_RUNNER_PROGRESS} Load {item.name}", end="")
print(style_text(" [FAIL]", _STYLE_FAILURE))
for line in traceback.format_exc().splitlines():
print(f" {line}")
stats["failed"] += 1
continue
# Skip modules without a valid main object
mod_main = getattr(mod, _RUNNER_MAIN, None)
if not isfunction(mod_main) or len(signature(mod_main).parameters) != 0:
print(f"{_RUNNER_PROGRESS} Skip {item.name}: No valid {_RUNNER_MAIN}() function")
stats["skipped"] += 1
continue
# Execution phase
print(f"{_RUNNER_PROGRESS} Run {mod.__name__}:{_RUNNER_MAIN}", end="")
try:
mod_main()
print(style_text(" [PASS]", _STYLE_SUCCESS))
stats["passed"] += 1
except Exception:
print(style_text(" [FAIL]", _STYLE_FAILURE))
for line in traceback.format_exc().splitlines():
print(f" {line}")
stats["failed"] += 1
# Summary report
print("\n" + "=" * 30)
print(style_text(f"Finish {root_name} runner", _STYLE_SUCCESS))
print(f"Passed: {stats['passed']} | Failed: {stats['failed']} | Skipped: {stats['skipped']}")
print("=" * 30)
if stats["failed"] > 0:
sys.exit(1)
if __name__ == "__main__":
main()