-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathrestyle.py
executable file
·290 lines (216 loc) · 7.69 KB
/
restyle.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
import argparse
import os
import sys
import pathlib
import subprocess
import contextlib
from dataclasses import dataclass
GIT_ROOT = pathlib.Path(
subprocess.check_output(
["git", "rev-parse", "--show-toplevel"], universal_newlines=True
).strip()
)
def clang_format(clang_format, config, files):
if not files:
raise ValueError("Files list cannot be empty")
cmd = [clang_format, "--verbose", f"--style=file:{config.as_posix()}", "-i"]
cmd.extend(files)
subprocess.run(cmd, check=True)
def ls_files(patterns):
"""Git-only search, but rather poor at matching complex patterns (at least w/ <=py3.12)"""
proc = subprocess.run(
["git", "--no-pager", "ls-files"],
capture_output=True,
check=True,
universal_newlines=True,
)
out = []
for line in proc.stdout.split("\n"):
path = pathlib.Path(line.strip())
if any(path.match(pattern) for pattern in patterns):
out.append(path)
return out
def diff_lines():
proc = subprocess.run(
["git", "--no-pager", "diff", "--ignore-submodules"],
capture_output=True,
check=True,
universal_newlines=True,
)
return proc.stdout.split("\n")
def find_files(patterns):
"""Filesystem search, matches both git and non-git files"""
return [
file
for pattern in patterns
for file in [found for found in GIT_ROOT.rglob(pattern)]
]
def find_core_files():
"""Returns a subset of Core files that should be formatted"""
return [
file
for file in find_files(
(
"cores/esp8266/Lwip*",
"libraries/ESP8266mDNS/**/*",
"libraries/Wire/**/*",
"libraries/lwIP*/**/*",
"cores/esp8266/debug*",
"cores/esp8266/core_esp8266_si2c*",
"cores/esp8266/StreamString*",
"cores/esp8266/StreamSend*",
"libraries/Netdump/**/*",
"tests/**/*",
)
)
if file.is_file()
and file.suffix in (".c", ".cpp", ".h", ".hpp")
and not GIT_ROOT / "tests/host/bin" in file.parents
and not GIT_ROOT / "tests/host/common/catch.hpp" == file
]
def find_arduino_files():
"""Returns every .ino file available in the repository, excluding submodule ones"""
return [
ino
for library in find_files(("libraries/*",))
if library.is_dir() and not (library / ".git").exists()
for ino in library.rglob("**/*.ino")
]
FILES_PRESETS = {
"core": find_core_files,
"arduino": find_arduino_files,
}
@dataclass
class Changed:
file: str
hunk: str
lines: list[int]
class Context:
def __init__(self):
self.append_hunk = False
self.deleted = False
self.file = ""
self.hunk = []
self.markers = []
def reset(self):
self.__init__()
def reset_with_line(self, line):
self.reset()
self.hunk.append(line)
def pop(self, out, line):
if self.file and self.hunk and self.markers:
out.append(
Changed(file=self.file, hunk="\n".join(self.hunk), lines=self.markers)
)
self.reset_with_line(line)
def changed_files_for_diff(lines: list[str] | str) -> list[Changed]:
"""
Naive git-diff output parser. Generates list of objects for every file changed after clang-format.
"""
match lines:
case str():
lines = lines.split("\n")
case list():
pass
case _:
raise ValueError("Unknown 'lines' type, can be either list[str] or str")
ctx = Context()
out = []
# TODO: pygit2?
# ref. https://github.com/cpp-linter/cpp-linter/blob/main/cpp_linter/git/__init__.py ::parse_diff
# ref. https://github.com/libgit2/pygit2/blob/master/src/diff.c ::parse_diff
for line in lines:
# '--- a/path/to/changed/file' most likely
# '--- /dev/null' aka created file. should be ignored, same as removed ones
if line.startswith("---"):
ctx.pop(out, line)
_, file = line.split(" ")
ctx.deleted = "/dev/null" in file
# '+++ b/path/to/changed/file' most likely
# '+++ /dev/null' aka removed file
elif not ctx.deleted and line.startswith("+++"):
ctx.hunk.append(line)
_, file = line.split(" ")
ctx.deleted = "/dev/null" in file
if not ctx.deleted:
ctx.file = file[2:]
# @@ from-file-line-numbers to-file-line-numbers @@
elif not ctx.deleted and line.startswith("@@"):
ctx.hunk.append(line)
_, _, numbers, _ = line.split(" ", 3)
if "," in numbers:
numbers, _ = numbers.split(",") # drop count
numbers = numbers.replace("+", "")
numbers = numbers.replace("-", "")
ctx.markers.append(int(numbers))
ctx.append_hunk = True
# capture diff for the summary
elif ctx.append_hunk and line.startswith(("+", "-", " ")):
ctx.hunk.append(line)
ctx.pop(out, line)
return out
def changed_files() -> list[Changed]:
return changed_files_for_diff(diff_lines())
def errors_changed(changed: Changed):
all_lines = ", ".join(str(x) for x in changed.lines)
for line in changed.lines:
print(
f"::error file={changed.file},title=Run tests/restyle.sh and re-commit {changed.file},line={line}::File {changed.file} failed clang-format style check. (lines {all_lines})"
)
SUMMARY_PATH = pathlib.Path(os.environ.get("GITHUB_STEP_SUMMARY", os.devnull))
SUMMARY_OUTPUT = SUMMARY_PATH.open("a")
def summary_diff(changed: Changed):
with contextlib.redirect_stdout(SUMMARY_OUTPUT):
print(f"# {changed.file} (suggested change)")
print("```diff")
print(changed.hunk)
print("```")
def stdout_diff():
subprocess.run(["git", "--no-pager", "diff", "--ignore-submodules"])
def assert_unchanged():
subprocess.run(
["git", "diff", "--ignore-submodules", "--exit-code"],
check=True,
stdout=subprocess.DEVNULL,
)
def run_format(args):
targets = []
for include in args.include:
targets.append(
(GIT_ROOT / f"tests/clang-format-{include}.yaml", FILES_PRESETS[include]())
)
if not targets:
targets.append((args.config, args.files))
for target in targets:
clang_format(args.clang_format, *target)
def run_assert(args):
for changed in changed_files():
if args.with_errors:
errors_changed(changed)
if args.with_summary:
summary_diff(changed)
if args.with_diff:
stdout_diff()
assert_unchanged()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
cmd = parser.add_subparsers(required=True)
format_ = cmd.add_parser("format")
format_.set_defaults(func=run_format)
format_.add_argument("--clang-format", default="clang-format")
fmt = format_.add_subparsers(required=True)
preset = fmt.add_parser("preset")
preset.add_argument(
"--include", action="append", required=True, choices=tuple(FILES_PRESETS.keys())
)
files = fmt.add_parser("files")
files.add_argument("--config", type=pathlib.Path, required=True)
files.add_argument("files", type=pathlib.Path, nargs="+")
assert_ = cmd.add_parser("assert")
assert_.set_defaults(func=run_assert)
assert_.add_argument("--with-diff", action="store_true")
assert_.add_argument("--with-errors", action="store_true")
assert_.add_argument("--with-summary", action="store_true")
args = parser.parse_args()
args.func(args)