-
Notifications
You must be signed in to change notification settings - Fork 6
/
format-files.py
43 lines (35 loc) · 1.53 KB
/
format-files.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
# A wrapper meant for automating the format process, using clang-format.
# This program iterates through the include and src folder, formatting
# each C(++) file.
# Axiot / Intns - 2021
import os
import subprocess
def launch_no_console(command, args):
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen([command] + args, startupinfo=si,
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
def format_folders(folder_to_iter):
for dirpath, dirname, files in os.walk(folder_to_iter):
for name in files:
if name.lower().endswith(".h") or name.lower().endswith(".cpp") or name.lower().endswith(".c"):
relative_path = os.path.join(dirpath, name)
print(relative_path, end="\t")
launch_no_console("clang-format.exe", [relative_path, "-style=file", "-i"])
if os.stat(relative_path).st_size < 1:
print("")
continue
with open(relative_path, "r+") as f:
f.seek(0, 2)
f.seek(f.tell() - 1, 0)
if f.read() != '\n':
f.write("\n");
print("added newline", end="")
print("")
if __name__ == "__main__":
if os.path.exists("clang-format.exe"):
format_folders("include/")
format_folders("src/")
print("Done formatting!")
else:
print("You need to download clang-format and place it in this directory!")