Skip to content

Commit 72ef995

Browse files
committed
added script to apply clang-format recursively
1 parent a2de964 commit 72ef995

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

apply-clang-format.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""Apply clang-format to a given set of files
2+
"""
3+
4+
from __future__ import print_function
5+
import traceback
6+
import os, sys, glob
7+
8+
def clangFormat(filemask, recursive):
9+
files = GetFiles(filemask, recursive)
10+
path = os.path.dirname(os.path.abspath(sys.argv[0]))
11+
os.chdir(path)
12+
for fullname in files:
13+
print("clang-format: " + fullname)
14+
os.system("clang-format -i " + fullname)
15+
16+
def listFiles(filemask, recursive):
17+
print("List: " + filemask + ", recursive:", recursive)
18+
for fullname in GetFiles(filemask, recursive):
19+
print(fullname)
20+
21+
def IsCppFile(fullname):
22+
filename, ext = os.path.splitext(fullname)
23+
if ext.lower().endswith(".cpp"):
24+
return True
25+
if ext.lower().endswith(".cc"):
26+
return True
27+
if ext.lower().endswith(".h"):
28+
return True
29+
if ext.lower().endswith(".hpp"):
30+
return True
31+
if ext.lower().endswith(".hh"):
32+
return True
33+
return False
34+
35+
def IsIgnoredDirectory(dirname):
36+
if dirname.lower().endswith("\gen"):
37+
return True
38+
if dirname.lower().endswith("\gen64"):
39+
return True
40+
return False
41+
42+
def GetFiles(filemask, recurse):
43+
if (recurse):
44+
result = []
45+
path = os.path.dirname(os.path.abspath(filemask))
46+
for dirName, subdirList, fileList in os.walk(path):
47+
if IsIgnoredDirectory(dirName):
48+
continue
49+
for fname in fileList:
50+
filename = dirName + "\\" + fname
51+
if not IsCppFile(filename):
52+
continue
53+
result += [filename]
54+
return result
55+
else:
56+
result = []
57+
for filename in glob.glob(filemask):
58+
if not IsCppFile(filename):
59+
continue
60+
result += [os.path.abspath(filename)]
61+
return result
62+
63+
def filter(keyword):
64+
for line in sys.stdin:
65+
if not keyword in line:
66+
sys.stdout.write(line)
67+
else:
68+
sys.stdout.write("// " + line)
69+
70+
def HasArgument(option):
71+
for arg in sys.argv:
72+
if (arg.strip() == ("/" + option)):
73+
return True
74+
return False
75+
76+
def HasInvalidArgs():
77+
args = len(sys.argv) - 1
78+
if args < 1 or args > 3:
79+
return True
80+
if sys.argv[1].startswith("/"):
81+
return True
82+
return False
83+
84+
def main():
85+
if HasInvalidArgs():
86+
print ("Usage: " + os.path.basename(sys.argv[0]) + " <filemask> [/s] [/f]")
87+
print (" note: gen\ and gen64\ directories are ignored")
88+
print (" /s = recursively process files in subdirecties");
89+
print (" /f = actually format the selected files instead of just listing them");
90+
print ("")
91+
print (" example: ")
92+
print (" "+ os.path.basename(sys.argv[0]) + " src\*.* /s /f")
93+
return
94+
95+
if HasArgument("f"):
96+
clangFormat(os.path.abspath(sys.argv[1]), HasArgument("s"))
97+
else:
98+
listFiles(os.path.abspath(sys.argv[1]), HasArgument("s"))
99+
100+
if __name__ == "__main__":
101+
try:
102+
main()
103+
except SystemExit :
104+
pass
105+
except KeyboardInterrupt:
106+
pass
107+
except Exception:
108+
traceback.print_exc(file=sys.stdout)
109+
sys.exit(0)
110+
111+

0 commit comments

Comments
 (0)