-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.py
38 lines (30 loc) · 860 Bytes
/
format.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
import errno
import os
import pathlib
import subprocess
import sys
def collect_files(extensions):
result = []
for (directory, _, files) in os.walk("."):
for f in files:
path = os.path.join(directory, f)
if pathlib.Path(path).suffix in extensions:
result.append(path)
return result
def format(file):
format_base = ["clang-format", "-i"]
command = format_base + [f]
print("Formatting", f)
subprocess.run(command)
if __name__ == "__main__":
extensions = [".c", ".h"]
try:
if len(sys.argv) > 1:
for f in sys.argv[1:]:
format(f)
else:
for f in collect_files(extensions):
format(f)
except OSError as e:
if e.errno == errno.ENOENT:
print("Error: clang-format is not available!")