-
Notifications
You must be signed in to change notification settings - Fork 2
/
reformat-source-code.py
58 lines (45 loc) · 1.4 KB
/
reformat-source-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
54
55
56
57
58
# vim: set et,sts=3,ts=3,ai
import json
import subprocess
import os
import sys
platform = sys.platform
if platform.startswith('linux'):
platform = 'linux'
with open('../Config/LocalConfig.json', 'r') as f:
config = json.load(f)
failed = 0
succeeded = 0
CLANGABLE = ('cpp', 'h')
DOS2UNIXABLE = CLANGABLE + ('json', 'sh', 'py')
ALL_PROCESSABLE = CLANGABLE + DOS2UNIXABLE
for root, dirs, files in os.walk('./'):
for candidate in files:
extension = candidate.rpartition('.')[2].lower()
if extension in ALL_PROCESSABLE:
path = os.path.join(root, candidate)
print('Formatting', path, '...')
cmds = []
if extension in CLANGABLE:
cmds.append([config['paths'][platform]['clang-format'], '-i', path])
if extension in DOS2UNIXABLE:
cmds.append([config['paths'][platform]['dos2unix'], path])
for cmd in cmds:
result = subprocess.check_call(cmd)
if result != 0:
print('Failed!')
failed += 1
else:
succeeded += 1
if failed > 0:
print('Several commands failed (%d).' % failed)
else:
print('Done, no failures.')
if len(sys.argv) >= 2 and sys.argv[1] == '--no-interactive':
pass
else:
try:
input = raw_input
except NameError:
pass
input('[Enter] to close')