-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_code_quality_check.py
executable file
·99 lines (76 loc) · 2.34 KB
/
run_code_quality_check.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
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import argparse
import subprocess
import sys
from pathlib import Path
def get_script_dir() -> Path:
"""Return path to the script directory."""
return Path(__file__).resolve().parent
def parse_args() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Run code quality checking tools")
parser.add_argument(
"-e",
"--edit",
action="store_true",
help="Edit the files automatically (make changes and overwrite files)",
)
return parser.parse_args()
def run_tool(cmd: list[str]) -> None:
print()
print("Running tool: " + repr(cmd))
subprocess.check_call(cmd)
print("Tool finished OK.")
def run_tool_isort(make_edits: bool, targets: list[Path]) -> None:
cmd = [sys.executable, "-m", "isort", "--profile", "black"]
if not make_edits:
cmd += ["--check-only", "--diff"]
cmd += map(str, targets)
run_tool(cmd)
def run_tool_black(make_edits: bool, targets: list[Path]) -> None:
cmd = [sys.executable, "-m", "black", "-q"]
if not make_edits:
cmd += ["--check", "--diff"]
cmd += map(str, targets)
run_tool(cmd)
def run_tool_flake8(targets: list[Path]) -> None:
cmd = [
sys.executable,
"-m",
"flake8",
"--max-line-length",
"88",
"--extend-ignore",
"E203",
]
cmd += map(str, targets)
run_tool(cmd)
def run_tool_mypy(targets: list[Path]) -> None:
for t in targets:
cmd = [sys.executable, "-m", "mypy", "--strict", str(t)]
run_tool(cmd)
def main() -> int:
args = parse_args()
srcs = [
get_script_dir() / "src" / "py_openocd_client",
]
tests = [
get_script_dir() / "tests_unit",
get_script_dir() / "tests_integration",
]
utils = [
get_script_dir() / "build_doc.py",
get_script_dir() / "run_code_quality_check.py",
get_script_dir() / "run_tests.py",
get_script_dir() / "make_release.py",
]
run_tool_isort(args.edit, srcs + tests + utils)
run_tool_black(args.edit, srcs + tests + utils)
run_tool_flake8(srcs + tests + utils)
run_tool_mypy(srcs + utils)
print()
print("Code quality check successful.")
return 0
if __name__ == "__main__":
sys.exit(main())