-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathclang-format-all.py
More file actions
145 lines (124 loc) · 3.44 KB
/
Copy pathclang-format-all.py
File metadata and controls
145 lines (124 loc) · 3.44 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
import os
import subprocess
from pathlib import Path
import argparse
import logging
from typing import List
# modified from https://github.com/thebigG/clang_format_all/blob/main/src/clang_format_all/clang_format_all.py
logging.basicConfig()
logger = logging.getLogger("check-all")
logger.setLevel(logging.INFO)
default_extensions = [
".cpp",
".cc",
".C",
"CPP",
".c++",
"cp",
".cxx",
".h",
".hh",
".hpp",
".hxx",
".H",
".tpp",
".ino",
]
default_exclusions = [
".history",
".git",
".github",
".pio",
".vscode",
"build",
"bin",
"lib",
"include",
"external",
"third_party",
]
def parse_args():
parser = argparse.ArgumentParser(
description="clang_format_all starts at this directory and drills down recursively"
)
parser.add_argument(
"--root_dir",
type=str,
required=False,
help="Root Directory",
default=os.getcwd(),
)
parser.add_argument(
"--file_extensions",
type=str,
required=False,
nargs="+",
help="File extensions to check or format",
default=default_extensions,
)
parser.add_argument(
"--exclude_dirs",
type=str,
required=False,
nargs="+",
help="Files/Folders to Exclude",
default=default_exclusions,
)
args = parser.parse_args()
return args
def format_all_walk_recursive(root_dir: str, exclude_files=None, file_extensions=None):
if exclude_files is None:
exclude_files = set()
if file_extensions is None:
file_extensions = []
for root, dirs, files in os.walk(root_dir):
if dirs:
for d in dirs:
format_all_walk_recursive((os.path.join(root, d)), file_extensions)
for file in files:
path = Path(os.path.join(root, file))
if str(path) in exclude_files:
continue
if path.suffix in file_extensions:
if (
subprocess.run(
[
"C:\\Program Files\\LLVM\\bin\\clang-format.exe",
"-style=file",
"-i",
path,
],
capture_output=True,
).returncode
!= 0
):
logger.info(
'"%s": An error occurred while parsing this file.', path
)
exit(-1)
else:
logger.info('"%s": parsed successfully.', path)
def get_all_files(root_dir: str) -> List:
files_array = []
for root, dirs, files in os.walk(root_dir):
if dirs:
for d in dirs:
files_array += get_all_files((os.path.join(root, d)))
for file in files:
files_array.append(os.path.join(root, file))
return files_array
def get_resolved_paths(unresolved_paths: List[str]) -> List[str]:
resolved_paths = []
for p in unresolved_paths:
resolved_paths += get_all_files(str(Path(p).resolve()))
return list(set(resolved_paths))
def main():
args = parse_args()
excluded_dirs = get_resolved_paths(args.exclude_dirs)
format_all_walk_recursive(
str(Path(args.root_dir).resolve()),
excluded_dirs,
args.file_extensions,
)
if __name__ == "__main__":
main()