-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_push.py
102 lines (73 loc) · 2.28 KB
/
pre_push.py
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
"""Run static analysis on the project."""
from __future__ import annotations
import argparse
import sys
from subprocess import CalledProcessError, check_call
def do_process(args: list[str], shell: bool = False, cwd: str = ".") -> bool:
"""Run program provided by args.
Return ``True`` on success.
Output failed message on non-zero exit and return False.
Exit if command is not found.
"""
print(f"Running: {' '.join(args)}")
try:
check_call(args, shell=shell, cwd=cwd)
except CalledProcessError:
print(f"\nFailed: {' '.join(args)}")
return False
except Exception as exc:
sys.stderr.write(f"{exc!s}\n")
sys.exit(1)
return True
def run_static() -> bool:
"""Run static analysis on the source code.
:returns: True if static analysis processes were successful, False otherwise.
:rtype: bool
"""
success = True
success &= do_process(["mypy", "src"])
success &= do_process(["pyright"])
return success
def run_linting() -> bool:
"""Run linting checks on the source code.
:returns: True if all linting processes were successful, False otherwise.
:rtype: bool
"""
success = True
success &= do_process(["black", "src"])
success &= do_process(["ruff", "check", "src", "--fix"])
success &= do_process(["docstrfmt", "."])
return success
def main() -> int:
"""Run the main function.
Run static and lint on code
"""
parser = argparse.ArgumentParser(description="Run static and/or unit-tests")
parser.add_argument(
"-s",
"--static",
action="store_true",
help="Do not run static tests (black/flake8/pydocstyle/sphinx-build)",
default=False,
)
parser.add_argument(
"-l",
"--linting",
action="store_true",
default=True,
help="Run the linting",
)
args = parser.parse_args()
success = True
try:
if args.static:
success &= run_static()
if args.linting:
success &= run_linting()
except KeyboardInterrupt:
return int(not False)
return int(not success)
if __name__ == "__main__":
exit_code = main()
print("\npre_push.py: Success!" if not exit_code else "\npre_push.py: Fail")
sys.exit(exit_code)