Skip to content

Commit e95f3af

Browse files
committed
Update linter and format scripts
1 parent 505c923 commit e95f3af

File tree

4 files changed

+173
-44
lines changed

4 files changed

+173
-44
lines changed

tools/__init__.py

Whitespace-only changes.

tools/base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-License-Identifier: MPL-2.0
2+
# SPDX-FileCopyrightText: 2025 igo95862
3+
from __future__ import annotations
4+
5+
from pathlib import Path
6+
7+
PROJECT_ROOT_PATH = Path(__file__).parent.parent
8+
BUILD_DIR = PROJECT_ROOT_PATH / "build"
9+
PYTHON_SOURCES: list[Path] = [
10+
PROJECT_ROOT_PATH / "src",
11+
PROJECT_ROOT_PATH / "tools",
12+
PROJECT_ROOT_PATH / "test",
13+
PROJECT_ROOT_PATH / "docs/conf.py",
14+
]
15+
16+
__all__ = ("PROJECT_ROOT_PATH", "BUILD_DIR", "PYTHON_SOURCES")

tools/run_format.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-License-Identifier: MPL-2.0
2+
# SPDX-FileCopyrightText: 2025 igo95862
3+
from __future__ import annotations
4+
5+
from subprocess import run
6+
7+
from .base import PROJECT_ROOT_PATH, PYTHON_SOURCES
8+
9+
10+
def format_with_black(check: bool = False) -> None:
11+
black_args = ["black"]
12+
13+
if check:
14+
black_args.extend(("--check", "--diff"))
15+
16+
black_args.extend(map(str, PYTHON_SOURCES))
17+
18+
run(
19+
args=black_args,
20+
cwd=PROJECT_ROOT_PATH,
21+
check=check,
22+
)
23+
24+
25+
def format_with_isort(check: bool = False) -> None:
26+
isort_args = ["isort", "--profile", "black"]
27+
28+
if check:
29+
isort_args.extend(("--check", "--diff"))
30+
31+
isort_args.extend(map(str, PYTHON_SOURCES))
32+
33+
run(
34+
args=isort_args,
35+
cwd=PROJECT_ROOT_PATH,
36+
check=check,
37+
)
38+
39+
40+
def format_meson(check: bool = False) -> None:
41+
meson_args = [
42+
"meson",
43+
"format",
44+
"--configuration",
45+
str(PROJECT_ROOT_PATH / "meson.format"),
46+
"--recursive",
47+
]
48+
49+
if check:
50+
meson_args.append("--check")
51+
else:
52+
meson_args.append("--inplace")
53+
54+
run(
55+
args=meson_args,
56+
cwd=PROJECT_ROOT_PATH,
57+
check=check,
58+
)
59+
60+
61+
if __name__ == "__main__":
62+
format_with_isort()
63+
format_with_black()
64+
format_meson()

tools/run_linters.py

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@
44

55
from pathlib import Path
66
from subprocess import PIPE, CalledProcessError, Popen, run
7-
from typing import Union
7+
from sys import stderr
88

9-
PROJECT_ROOT_PATH = Path(__file__).parent.parent
10-
PYTHON_SOURCES: list[Path] = [
11-
PROJECT_ROOT_PATH / "src",
12-
PROJECT_ROOT_PATH / "tools",
13-
PROJECT_ROOT_PATH / "test",
14-
PROJECT_ROOT_PATH / "docs/conf.py",
15-
PROJECT_ROOT_PATH / "examples",
16-
]
9+
from .base import BUILD_DIR, PROJECT_ROOT_PATH, PYTHON_SOURCES
10+
from .run_format import format_meson, format_with_black, format_with_isort
1711

1812

19-
def run_linter(args: list[Union[str, Path]]) -> bool:
20-
print("Running:", args[0])
13+
def run_linter(args: list[str | Path]) -> bool:
14+
print("Running:", args[0], file=stderr)
2115
try:
2216
run(
2317
args=args,
@@ -30,66 +24,118 @@ def run_linter(args: list[Union[str, Path]]) -> bool:
3024
return False
3125

3226

33-
def run_reuse() -> bool:
34-
return run_linter(["reuse", "lint"])
35-
36-
3727
def run_pyflakes() -> bool:
3828
return run_linter(["pyflakes", *PYTHON_SOURCES])
3929

4030

31+
def run_mypy() -> bool:
32+
cache_dir = BUILD_DIR / "mypy_cache"
33+
mypy_args: list[str | Path] = [
34+
"mypy",
35+
"--pretty",
36+
"--strict",
37+
"--python-version",
38+
"3.9",
39+
"--cache-dir",
40+
cache_dir,
41+
*PYTHON_SOURCES,
42+
]
43+
44+
return run_linter(mypy_args)
45+
46+
47+
def run_reuse() -> bool:
48+
return run_linter(["reuse", "lint"])
49+
50+
4151
def run_black() -> bool:
42-
return run_linter(
43-
[
44-
"black",
45-
"--check",
46-
"--diff",
47-
*PYTHON_SOURCES,
48-
]
49-
)
52+
print("Running: black", file=stderr)
53+
try:
54+
format_with_black(check=True)
55+
except CalledProcessError:
56+
return True
57+
58+
return False
5059

5160

5261
def run_isort() -> bool:
53-
return run_linter(
54-
[
55-
"isort",
56-
"--check",
57-
"--diff",
58-
"--profile",
59-
"black",
60-
*PYTHON_SOURCES,
61-
]
62-
)
62+
print("Running: isort", file=stderr)
63+
try:
64+
format_with_isort(check=True)
65+
except CalledProcessError:
66+
return True
67+
68+
return False
6369

6470

65-
def run_mypy() -> bool:
66-
return run_linter(
67-
["mypy", "--pretty", "--strict", "--python-version", "3.9", *PYTHON_SOURCES]
68-
)
71+
def run_meson_format() -> bool:
72+
print("Running: meson format", file=stderr)
73+
try:
74+
format_meson(check=True)
75+
except CalledProcessError:
76+
print("Meson format failed!", file=stderr)
77+
return True
78+
79+
return False
80+
81+
82+
IGNORE_CODESPELL_WORDS = ("assertIn",)
6983

7084

7185
def run_codespell() -> bool:
72-
return run_linter(["codespell"])
86+
print("Running: codespell", file=stderr)
87+
try:
88+
list_of_files = run(
89+
args=("git", "ls-files", "-z"),
90+
cwd=PROJECT_ROOT_PATH,
91+
stdout=PIPE,
92+
text=True,
93+
check=True,
94+
).stdout.split("\0")
95+
run(
96+
args=[
97+
"codespell",
98+
"--check-filenames",
99+
"--enable-colors",
100+
"--context",
101+
"3",
102+
"--ignore-words-list",
103+
",".join(IGNORE_CODESPELL_WORDS),
104+
*list_of_files,
105+
],
106+
check=True,
107+
)
108+
except CalledProcessError:
109+
return True
110+
111+
return False
73112

74113

75114
def run_codespell_on_commits() -> bool:
76-
print("Running: git log to codespell")
115+
print("Running: git log to codespell", file=stderr)
77116
try:
78117
git_log = Popen(
79118
args=(
80119
"git",
81120
"log",
82121
"--max-count=50",
83122
"--no-merges",
84-
"--since=2025-01-01",
85-
r"--format=%s%n%n%b",
123+
r"--format='%H%n%n%s%n%n%b'",
86124
),
87125
cwd=PROJECT_ROOT_PATH,
88126
stdout=PIPE,
89127
)
90128

91129
run(
92-
["codespell", "--context", "3", "-"],
130+
args=(
131+
"codespell",
132+
"--enable-colors",
133+
"--context",
134+
"3",
135+
"--ignore-words-list",
136+
",".join(IGNORE_CODESPELL_WORDS),
137+
"-",
138+
),
93139
cwd=PROJECT_ROOT_PATH,
94140
check=True,
95141
stdin=git_log.stdout,
@@ -102,13 +148,16 @@ def run_codespell_on_commits() -> bool:
102148

103149

104150
def main() -> None:
151+
BUILD_DIR.mkdir(exist_ok=True)
152+
105153
has_failed = False
106154

107-
has_failed |= run_reuse()
108155
has_failed |= run_pyflakes()
156+
has_failed |= run_mypy()
157+
has_failed |= run_reuse()
109158
has_failed |= run_black()
110159
has_failed |= run_isort()
111-
has_failed |= run_mypy()
160+
has_failed |= run_meson_format()
112161
has_failed |= run_codespell()
113162
has_failed |= run_codespell_on_commits()
114163

0 commit comments

Comments
 (0)