forked from numpy/numpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinter.py
More file actions
91 lines (72 loc) · 2.55 KB
/
linter.py
File metadata and controls
91 lines (72 loc) · 2.55 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
82
83
84
85
86
87
88
89
90
91
import os
import subprocess
import sys
from argparse import ArgumentParser
CWD = os.path.abspath(os.path.dirname(__file__))
class DiffLinter:
def __init__(self) -> None:
self.repository_root = os.path.realpath(os.path.join(CWD, ".."))
def run_ruff(self, fix: bool) -> tuple[int, str]:
"""
Original Author: Josh Wilson (@person142)
Source:
https://github.com/scipy/scipy/blob/main/tools/lint_diff.py
Unlike pycodestyle, ruff by itself is not capable of limiting
its output to the given diff.
"""
print("Running Ruff Check...")
command = ["ruff", "check"]
if fix:
command.append("--fix")
res = subprocess.run(
command,
stdout=subprocess.PIPE,
cwd=self.repository_root,
encoding="utf-8",
)
return res.returncode, res.stdout
def run_cython_lint(self) -> tuple[int, str]:
print("Running cython-lint...")
command = ["cython-lint", "--no-pycodestyle", "numpy"]
res = subprocess.run(
command,
stdout=subprocess.PIPE,
cwd=self.repository_root,
encoding="utf-8",
)
return res.returncode, res.stdout
def run_lint(self, fix: bool) -> None:
# Ruff Linter
retcode, ruff_errors = self.run_ruff(fix)
ruff_errors and print(ruff_errors)
if retcode:
sys.exit(retcode)
# C API Borrowed-ref Linter
retcode, c_API_errors = self.run_check_c_api()
c_API_errors and print(c_API_errors)
if retcode:
sys.exit(retcode)
# Cython Linter
retcode, cython_errors = self.run_cython_lint()
cython_errors and print(cython_errors)
sys.exit(retcode)
def run_check_c_api(self) -> tuple[int, str]:
"""Run C-API borrowed-ref checker"""
print("Running C API borrow-reference linter...")
borrowed_ref_script = os.path.join(
self.repository_root, "tools", "ci", "check_c_api_usage.py"
)
borrowed_res = subprocess.run(
[sys.executable, borrowed_ref_script],
cwd=self.repository_root,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
check=False,
)
# Exit with non-zero if C API Check fails
return borrowed_res.returncode, borrowed_res.stdout
if __name__ == "__main__":
parser = ArgumentParser()
args = parser.parse_args()
DiffLinter().run_lint(fix=False)