-
Notifications
You must be signed in to change notification settings - Fork 1
/
format-command-lines.py
87 lines (73 loc) · 2.47 KB
/
format-command-lines.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
import os
import sys
import errno
if len(sys.argv) < 2:
sys.stderr.write('Usage: %s <file> <modes> [<replacements> ...]' % sys.argv[0])
sys.exit(1)
input_file = sys.argv[1]
modes = sys.argv[2].split(',') # format-calls, apply-replacements
format_calls = 'format-calls' in modes
apply_replacements = 'apply-replacements' in modes
replacement_args = []
if len(sys.argv) > 2:
replacement_args = sys.argv[3:]
def show(line):
try:
sys.stdout.write(line)
sys.stdout.flush()
except IOError as e:
if e.errno == errno.EPIPE:
input.close()
sys.exit(0)
input = open(input_file, 'r')
command_line_counter = 0
if apply_replacements:
replacements = [] # a list because we want to keep order
for arg in replacement_args:
rep = arg.split('=')
if len(rep) == 2:
replacements.append((rep[0], rep[1]))
else:
sys.stderr.write('WARNING: invalid replacement: %s\n' % arg)
if replacements:
print('===\n=== Replacements made by ye\n===\n')
for orig, rep in replacements:
print('%s => %s' % (orig, rep))
print('\n' + '=' * 80 + '\n')
while True:
build_line = input.readline()
if not build_line:
break
if apply_replacements:
for orig, rep in replacements:
build_line = build_line.replace(orig, rep)
if format_calls:
tokens = build_line.split()
if len(tokens) > 0:
prev = None
first_token = tokens[0]
filename = os.path.basename(first_token)
if (filename.endswith('gcc') or
filename.endswith('g++') or
filename.endswith('xgcc') or
filename.endswith('clang') or
filename.endswith('clang++') or
filename.endswith('javac')):
command_line_counter += 1
dashes = '-' * 15
print('%s[ command line %s ]%s' % (dashes, command_line_counter, dashes))
lines = [first_token]
for token in tokens[1:]:
if prev in ['-o', '-isystem']:
lines[-1] += ' ' + token
else:
lines.append(token)
prev = token
show(lines[0] + '\n')
for line in lines[1:]:
show(' ' + line + '\n')
else:
show(build_line)
else:
show(build_line)
input.close()