-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat_code.py
executable file
·53 lines (45 loc) · 1.45 KB
/
format_code.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This script runs "clang-format" recursively on all C/C++ files
import os
import fnmatch
import subprocess
def all_files(root,
patterns='*',
skips='*.git*;*build*',
single_level=False,
yield_folders=False):
# self.checkPath(root)
patterns = patterns.split(';')
skips = skips.split(';')
for path, subdirs, files in os.walk(root):
# print('processing folder', path)
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
fullname = os.path.join(path, name)
ok = True
for skip in skips:
if fnmatch.fnmatch(fullname, skip):
ok = False
if ok:
yield fullname
break
if single_level:
break
def main():
# loop over all files and format them
for f in all_files(os.getcwd(), patterns='*.cpp;*.c;*.h;*.hpp',
skips='*.git*;*build*;*externals*'):
print(f)
cmd = ['clang-format', "-style=file", "-i", f]
retcode = subprocess.call(cmd)
if retcode != 0:
print(f'ERROR: retcode = {retcode}')
break
if __name__ == "__main__":
main()