-
Notifications
You must be signed in to change notification settings - Fork 0
/
clang_format_all.py
114 lines (93 loc) · 4.17 KB
/
clang_format_all.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
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
import os
import subprocess
from pathlib import Path
import argparse
import logging
import yaml
logging.basicConfig()
logger = logging.getLogger('check-all')
logger.setLevel(logging.INFO)
def parse_args():
parser = argparse.ArgumentParser(
description='clang_format_all starts at this directory and drills down recursively')
parser.add_argument('--config', type=str, required=True,
help='Yaml file path with configuration.')
args = parser.parse_args()
return args
def check_all_walk_recursive(clang_format_command: str, 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:
check_all_walk_recursive(clang_format_command, (os.path.join(root, d)), exclude_files, 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([clang_format_command, "--dry-run", "--Werror", "-style=file",
path]).returncode != 0:
logger.info("\"%s\": does not comply to format according to clang-format", path)
exit(-1)
else:
logger.info("\"%s\": looks fine according to clang-format", path)
def format_all_walk_recursive(clang_format_command: str, 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(clang_format_command, (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([clang_format_command, "-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) -> []:
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: [str], root_dir: str) -> [str]:
resolved_paths = []
for p in unresolved_paths:
resolved_paths += get_all_files(str(Path(root_dir, p).resolve()))
return set(resolved_paths)
def main():
args = parse_args()
excluded_dirs = None
with open(args.config) as yaml_stream:
config = yaml.load(yaml_stream, Loader=yaml.CSafeLoader)
if 'exclude_dirs' not in config:
logger.error(f"No key 'exclude_dirs' found in {args.config}")
return -1
if 'root_dir' not in config:
logger.error(f"No key 'root_dir' found in {args.config}")
return -1
if 'check_all' not in config:
logger.error(f"No key 'check_all' found in {args.config}")
return -1
if 'file_extensions' not in config:
logger.error(f"No key 'file_extensions' found in {args.config}")
return -1
excluded_dirs = get_resolved_paths(config['exclude_dirs'], config["root_dir"])
if config['check_all'] is True:
check_all_walk_recursive(config["clang_format_command"], str(Path(config["root_dir"]).resolve()), excluded_dirs, config['file_extensions'])
else:
format_all_walk_recursive(config["clang_format_command"], str(Path(config["root_dir"]).resolve()), excluded_dirs, config['file_extensions'])
main()