-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathformat.py
More file actions
299 lines (270 loc) · 9.94 KB
/
Copy pathformat.py
File metadata and controls
299 lines (270 loc) · 9.94 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
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
291
292
293
294
295
296
297
298
299
import argparse
import os
import subprocess
from pathlib import Path
from colorama import Fore, Style
# Supported file types and their corresponding formatter categories
SUPPORTED_FILES = {
".h": "c",
".hh": "c",
".hpp": "c",
".c": "c",
".cc": "c",
".cpp": "c",
".cxx": "c",
".cu": "c",
".cuh": "c",
".mlu": "c",
".cl": "c",
".py": "py",
}
def format_file(file: Path, check: bool, formatter) -> bool:
formatter = formatter.get(SUPPORTED_FILES.get(file.suffix, None), None)
if not formatter:
return True # Unsupported file type, skip
formatted = True
try:
cmd = []
if formatter.startswith("clang-format"):
cmd = [formatter, "-style=file", "-i", file]
if check:
cmd.insert(2, "-dry-run")
process = subprocess.run(
cmd,
capture_output=True,
text=True,
check=True,
)
if process.stderr:
print(f"{Fore.YELLOW}{file} is not formatted.{Style.RESET_ALL}")
print(
f"Use {Fore.CYAN}{formatter} -style=file -i {file}{Style.RESET_ALL} to format it."
)
formatted = False
else:
subprocess.run(
cmd,
capture_output=True,
text=True,
check=True,
)
print(f"{Fore.CYAN}Formatted: {file}{Style.RESET_ALL}")
elif formatter == "black":
cmd = [formatter, file]
if check:
cmd.insert(1, "--check")
process = subprocess.run(
cmd,
capture_output=True,
text=True,
check=False,
)
if process.returncode != 0:
print(f"{Fore.YELLOW}{file} is not formatted.{Style.RESET_ALL}")
print(
f"Use {Fore.CYAN}{formatter} {file}{Style.RESET_ALL} to format it."
)
formatted = False
else:
subprocess.run(
cmd,
capture_output=True,
text=True,
check=True,
)
print(f"{Fore.CYAN}Formatted: {file}{Style.RESET_ALL}")
elif formatter == "ruff":
ruff_cmd = ["ruff", "check", file]
ruff_format_cmd = ["ruff", "format", file]
try:
if check:
process = subprocess.run(
ruff_cmd,
capture_output=True,
text=True,
check=False,
)
if process.returncode != 0:
print(f"{Fore.YELLOW}{file} has ruff issues.{Style.RESET_ALL}")
print(
f"Use {Fore.CYAN}ruff check --fix {file}{Style.RESET_ALL} to fix it."
)
formatted = False
ruff_format_cmd.insert(2, "--check")
process = subprocess.run(
ruff_format_cmd,
capture_output=True,
text=True,
check=False,
)
if process.returncode != 0:
print(
f"{Fore.YELLOW}{file} is not ruff-formatted.{Style.RESET_ALL}"
)
print(
f"Use {Fore.CYAN}ruff format {file}{Style.RESET_ALL} to format it."
)
formatted = False
else:
ruff_cmd.insert(2, "--fix")
process = subprocess.run(
ruff_cmd,
capture_output=True,
text=True,
check=False,
)
if process.returncode != 0:
print(f"{Fore.RED}Ruff failed for {file}.{Style.RESET_ALL}")
if process.stdout:
print(process.stdout, end="")
if process.stderr:
print(process.stderr, end="")
formatted = False
else:
print(f"{Fore.CYAN}Ruff fixed: {file}{Style.RESET_ALL}")
process = subprocess.run(
ruff_format_cmd,
capture_output=True,
text=True,
check=False,
)
if process.returncode != 0:
print(
f"{Fore.RED}Ruff format failed for {file}.{Style.RESET_ALL}"
)
if process.stdout:
print(process.stdout, end="")
if process.stderr:
print(process.stderr, end="")
formatted = False
else:
print(f"{Fore.CYAN}Ruff formatted: {file}{Style.RESET_ALL}")
except FileNotFoundError:
print(
f"{Fore.RED}Formatter ruff not found, {file} skipped.{Style.RESET_ALL}"
)
formatted = False
except FileNotFoundError:
print(
f"{Fore.RED}Formatter {formatter} not found, {file} skipped.{Style.RESET_ALL}"
)
formatted = False
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Formatter {formatter} failed: {e}{Style.RESET_ALL}")
formatted = False
if not ensure_single_trailing_newline(file, check):
formatted = False
return formatted
def ensure_single_trailing_newline(file: Path, check: bool) -> bool:
"""Ensure the file ends with exactly one LF byte."""
content = file.read_bytes()
fixed = content.rstrip(b"\r\n") + b"\n"
if content == fixed:
return True
if check:
print(
f"{Fore.YELLOW}{file} does not end with exactly one newline.{Style.RESET_ALL}"
)
return False
file.write_bytes(fixed)
print(f"{Fore.CYAN}Fixed trailing newline: {file}{Style.RESET_ALL}")
return True
def git_added_files():
"""Get all staged files"""
try:
# Use git diff --cached --name-only to get all files added to staging area
result = subprocess.run(
["git", "diff", "--cached", "--diff-filter=AMR", "--name-only"],
capture_output=True,
text=True,
check=True,
)
for file in result.stdout.splitlines():
yield Path(file.strip())
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Git diff failed: {e}{Style.RESET_ALL}")
def git_modified_since_ref(ref):
"""Get list of files modified from the specified Git reference to the current state"""
try:
result = subprocess.run(
["git", "diff", f"{ref}..", "--diff-filter=AMR", "--name-only"],
capture_output=True,
text=True,
check=True,
)
for file in result.stdout.splitlines():
yield Path(file.strip())
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Git diff failed: {e}{Style.RESET_ALL}")
def list_files(paths):
"""Recursively get all files under the specified paths"""
for path in paths:
if path.is_file():
yield path
elif path.is_dir():
for dirpath, _, filenames in os.walk(path):
for name in filenames:
yield Path(dirpath) / name
else:
print(
f"{Fore.RED}Error: {path} is not a file or directory.{Style.RESET_ALL}"
)
def filter_in_path(file: Path, path) -> bool:
"""Check if file is within the specified paths"""
for p in path:
if file.is_relative_to(p):
return True
return False
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--ref", type=str, help="Git reference (commit hash) to compare against."
)
parser.add_argument(
"--path", nargs="*", type=Path, help="Files to format or check."
)
parser.add_argument(
"--check", action="store_true", help="Check files without modifying them."
)
parser.add_argument(
"--c", default="clang-format-16", help="C formatter (default: clang-format-16)"
)
parser.add_argument("--py", default="ruff", help="Python formatter (default: ruff)")
args = parser.parse_args()
if args.ref is None and args.path is None:
# Last commit.
print(f"{Fore.GREEN}Formatting git staged files.{Style.RESET_ALL}")
files = git_added_files()
else:
if args.ref is None:
print(f"{Fore.GREEN}Formatting files in {args.path}.{Style.RESET_ALL}")
files = list_files(args.path)
elif args.path is None:
print(
f"{Fore.GREEN}Formatting git modified files from {args.ref}.{Style.RESET_ALL}"
)
files = git_modified_since_ref(args.ref)
else:
print(
f"{Fore.GREEN}Formatting git modified files from {args.ref} in {args.path}.{Style.RESET_ALL}"
)
files = (
file
for file in git_modified_since_ref(args.ref)
if filter_in_path(file, args.path)
)
formatted = True
for file in files:
if not format_file(
file,
args.check,
{
"c": args.c,
"py": args.py,
},
):
formatted = False
if not formatted:
exit(1)
if __name__ == "__main__":
main()