44
55from pathlib import Path
66from 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-
3727def 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+
4151def 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
5261def 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
7185def 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
75114def 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
104150def 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