forked from varqox/sim-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.py
executable file
·163 lines (148 loc) · 5.53 KB
/
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import json
import os
import re
import subprocess
import sys
import time
def filter_subdirs(dir, subdirs, allowed_extensions, denied_suffixes):
dir = Path(os.path.abspath(dir))
allowed_extensions = ['.' + ext for ext in allowed_extensions]
def qualifies(path):
for ext in allowed_extensions:
if path.endswith(ext):
for dsuf in denied_suffixes:
if re.search(dsuf + '$', path):
return False
return True
return False
return filter(qualifies, [str(f) for subdir in subdirs for f in (dir / subdir).glob('**/*')])
class Source:
def __init__(self, path, format_file_path, dependency_files, pre_format_commands = []):
self.path = path
self.format_file_path = format_file_path
self.dependency_files = [format_file_path] + dependency_files
self.pre_format_commands = pre_format_commands
def format_sources(sources, cache_dir = Path(os.getenv('MESON_SOURCE_ROOT', '.')) / '.cache'):
clang_format_path = subprocess.check_output(['sh', '-c', 'command -v clang-format']).strip()
newest_dependency_files_mtime = max(map(os.path.getctime, [clang_format_path, __file__]))
# Make source paths absolute
for source in sources:
source.path = os.path.abspath(source.path)
# Cache contains pairs: path => last reformat time
cache = {}
try:
with open(Path(cache_dir) / 'format.json') as cache_file:
cache = json.load(cache_file)
except FileNotFoundError:
pass
def needs_reformatting(source):
last_reformat = cache.get(source.path)
if last_reformat is None:
return True
if last_reformat < os.path.getctime(source.path):
return True
if last_reformat < max(newest_dependency_files_mtime, *map(os.path.getctime, source.dependency_files)):
return True
return False
# Trim cache to only contain the current sources i.e. remove sources that disappeared
cache = {source.path: cache[source.path] for source in sources if source.path in cache}
sources_to_format = {source for source in sources if needs_reformatting(source)}
print(len(sources_to_format), 'files to format.')
with ThreadPoolExecutor() as executor:
def run_clang_format(source):
print('format ' + source.path)
for command in source.pre_format_commands:
subprocess.check_call(command)
subprocess.check_call(['clang-format', '-style=file:' + source.format_file_path, '-i', source.path])
cache[source.path] = time.time() # this is thread safe
futures = [executor.submit(run_clang_format, source) for source in sources_to_format]
[fut.result() for fut in futures]
# Save cache
try:
os.mkdir(cache_dir)
except FileExistsError:
pass
with open(Path(cache_dir) / 'format.json', 'w') as cache_file:
json.dump(cache, cache_file)
def simlib_sources():
src_dir = os.path.join(sys.path[0], 'subprojects/simlib')
return [
Source(
path,
os.path.join(os.path.dirname(__file__), '.clang-format'),
[__file__],
[
# Fix includes of form "simlib/*", "gtest/*" "gmock/*" (thanks clangd...) to <...>
["sed", r's@^#include "\(\(simlib/\|gtest\|gmock/\).*\)"$@#include <\1>@', "-i", path],
]
)
for path in filter_subdirs(
src_dir, [
'examples/',
'include/',
'src/',
'test/',
],
['c', 'cc', 'h', 'hh'],
[
'src/sim/default_checker_dump.c',
'test/conver_test_cases/.*',
'test/old_sandbox_test_cases/.*',
]
)
]
def sim_sources():
src_dir = os.path.join(sys.path[0], 'subprojects/sim')
return [
Source(path,
os.path.join(os.path.dirname(__file__), '.clang-format'),
[__file__],
[
# Fix includes of form "sim/*", "simlib/*", "gtest/*" "gmock/*" (thanks clangd...) to <...>
["sed", r's@^#include "\(\(sim/\|simlib/\|gtest/\|gmock/\).*\)"$@#include <\1>@', "-i", path]
]
)
for path in filter_subdirs(
src_dir,
[
'include/',
'src/',
'test/',
],
['c', 'cc', 'h', 'hh'],
[
'src/web_server/static/.*',
'test/sim/cpp_syntax_highlighter_test_cases/.*',
]
)
]
def sip_sources():
src_dir = os.path.join(sys.path[0], 'subprojects/sip')
return [
Source(path,
os.path.join(os.path.dirname(__file__), '.clang-format'),
[__file__],
[
# Fix includes of form "simlib/*", "gtest/*" "gmock/*" (thanks clangd...) to <...>
["sed", r's@^#include "\(\(simlib/\|gtest/\|gmock/\).*\)"$@#include <\1>@', "-i", path]
]
)
for path in filter_subdirs(
src_dir,
[
'src/',
'templates/',
'test/',
],
['c', 'cc', 'h', 'hh'],
[
'src/git_commit.hh',
'test/sip_test_cases/.*'
]
)
]
if __name__ == '__main__':
format_sources(simlib_sources() + sim_sources() + sip_sources())