-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorcargo.py
executable file
·312 lines (248 loc) · 9.52 KB
/
colorcargo.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/env python3
'''
MIT License
Copyright (c) 2016-2019 Alexander Lopatin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import configparser
import os
import re
import sys
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
import traceback
import colorama
from colorama import Fore, Style
DEBUG = False
DIRPATH_SPACES = ' ' * 23 # FIXME
HASH_LENGTH = 16
UNKNOWN_POLL_RESULT = 101
BEFORE_FUNC_DELIMITER_PATTERN = ' - '
FILEPATH_PATTERN = ' at '
FUNC_DELIMITER_PATTERN = '::'
OK_PATTERN = 'ok'
PANICKED_AT_PATTERN = "' panicked at '"
TEST_RESULT_PATTERN = 'test result: '
BORING_LINE_MATCHER = re.compile(r'(/rustc|(src/(libstd|libpanic_unwind|libtest))|/var/tmp/portage|/sysdeps/unix/sysv/linux)/')
TEST_LINE_MATCHER = re.compile(r'(test .* \.\.\. )(' + OK_PATTERN + r'|FAILED)')
def debug(prompt, error):
if DEBUG:
print(prompt, str(error), file=sys.stderr)
traceback.print_exc()
def set_func_color(trace, line, our_project):
result = ''
text = trace[line].split('\n')[0]
block_color = Fore.RESET
func_color = Fore.CYAN + Style.NORMAL
if our_project:
block_color = Fore.YELLOW
func_color = Fore.MAGENTA + Style.NORMAL
func_pos = text.find(BEFORE_FUNC_DELIMITER_PATTERN)
if func_pos >= 0:
func_pos += len(BEFORE_FUNC_DELIMITER_PATTERN)
before_func = text[:func_pos]
func = text[func_pos:]
hash_delimiter = FUNC_DELIMITER_PATTERN + 'h'
func_hash_pos = func.rfind(hash_delimiter)
hash_length = len(func) - (func_hash_pos + len(hash_delimiter))
if func_hash_pos < 0 or hash_length != HASH_LENGTH:
func_hash_pos = len(func)
func_hash = func[func_hash_pos:]
func = func[:func_hash_pos]
if len(before_func) > 0:
before_func = Style.DIM + before_func
result += block_color + before_func
if len(func) > 0:
func_prefix_pos = func.rfind(FUNC_DELIMITER_PATTERN)
if func_prefix_pos >= 0:
func_prefix_pos += len(FUNC_DELIMITER_PATTERN)
func_prefix = func[:func_prefix_pos]
func_name = func[func_prefix_pos:]
result += func_color + func_prefix + Style.BRIGHT + func_name
else:
result += Style.BRIGHT + func_color + func
result += Style.NORMAL
if len(func_hash) > 0:
result += func_hash
else:
result = block_color + text
result += Fore.RESET + '\n'
trace[line] = result
def set_file_and_line_color(trace, line, our_project):
result = ''
text = trace[line]
try:
block_color = Fore.RESET
filename_color = Fore.RESET
file_line_color = Fore.RESET
if our_project:
block_color = Fore.YELLOW
filename_color = Style.BRIGHT + Fore.GREEN
file_line_color = Fore.WHITE
filename_and_line_pos = text.rfind('/')
assert filename_and_line_pos >= 0
filename_and_line_pos += 1
filename_and_line = text[filename_and_line_pos:]
file_line_pos = filename_and_line.rfind(':')
assert file_line_pos >= 0
filename = filename_and_line[:file_line_pos]
file_line = filename_and_line[file_line_pos:]
dirpath = text[:filename_and_line_pos]
dirpath_pos = dirpath.find(FILEPATH_PATTERN)
assert dirpath_pos >= 0
dirpath = DIRPATH_SPACES + block_color + dirpath[dirpath_pos:]
result = dirpath + filename_color + filename + \
file_line_color + file_line
result += Style.NORMAL + Fore.RESET
except Exception as error:
debug('Parsing error: ', error)
result = text
finally:
trace[line] = result
def set_panicked_line_color(text):
result = ''
try:
func_color = Fore.MAGENTA
block_color = Fore.YELLOW
assert_color = Fore.RED
filename_color = Style.BRIGHT + Fore.GREEN
file_line_color = Fore.WHITE
begin, thread_name, panicked_at, assert_failed, end = text.split("'")
full_file_name = end.split(' ')[1]
filename_pos = full_file_name.rfind('/')
assert filename_pos >= 0
filename_pos += 1
dirpath = full_file_name[:filename_pos]
filename_and_line_number = full_file_name[filename_pos:]
filename, file_line, column = filename_and_line_number.split(':')
result += begin
result += "'" + func_color + thread_name + Fore.RESET + "'"
result += panicked_at
result += "'" + assert_color + assert_failed + Fore.RESET + "'"
result += ', ' + block_color + dirpath
result += filename_color + filename
result += file_line_color + ':' + file_line + ':' + column
result += Style.NORMAL + Fore.RESET
except Exception as error:
debug('Parsing error: ', error)
result = text
finally:
return result
def set_test_line_color(text):
result = ''
color = Fore.RED
test, test_result = TEST_LINE_MATCHER.match(text).group(1, 2)
if test_result == OK_PATTERN:
color = Fore.GREEN
return test + color + test_result + Fore.RESET + '\n'
def set_test_result_line_color(text):
result = ''
result += Style.BRIGHT
color = Fore.RED
if text.find(': {}.'.format(OK_PATTERN)) >= 0:
color = Fore.GREEN
result += color + text
result += Style.NORMAL + Fore.RESET
return result
def set_colors(trace, our_package_pattern):
n = len(trace)
our_project = False
for i in range(n):
line = trace[i]
is_filepath = line.find(FILEPATH_PATTERN) >= 0
if is_filepath:
set_file_and_line_color(trace, i, our_project)
else:
our_project = bool(our_package_pattern is not None and our_package_pattern.match(trace[i]))
set_func_color(trace, i, our_project)
def parse_backtrace_and_print(trace, our_package_pattern, verbose):
try:
set_colors(trace, our_package_pattern)
except Exception as error:
debug('Parsing error: ', error)
finally:
for text in trace:
if verbose or BORING_LINE_MATCHER.search(text) is None:
sys.stdout.write(text)
def find_project_config():
config_directory = os.getcwd()
filename = 'Cargo.toml'
while True:
config_path = os.path.join(config_directory, filename)
if os.path.isfile(config_path):
return config_path
elif len(config_directory) <= 1:
break
else:
config_directory = os.path.split(config_directory)[0]
def compile_our_package_pattern():
config_path = find_project_config()
if config_path is not None:
config = configparser.ConfigParser()
config.read(config_path)
package_name = config['package']['name'].strip('"').replace('-', '_')
return re.compile(r'.* - [<]{0,1}' + package_name + FUNC_DELIMITER_PATTERN + r'.*')
def consume(pipe, verbose):
our_package_pattern = compile_our_package_pattern()
found_backtrace = False
trace = []
while True:
poll_result = pipe.poll()
if poll_result is not None and poll_result != UNKNOWN_POLL_RESULT:
break
ch = pipe.stdout.read(1)
if len(ch) == 0:
break
text = ch + pipe.stdout.readline()
text = text.decode()
if found_backtrace:
trace.append(text)
if text.find('0x0 - <unknown>') >= 0:
found_backtrace = False
parse_backtrace_and_print(trace, our_package_pattern, verbose)
elif text.find('stack backtrace:') >= 0:
found_backtrace = True
trace.append(text)
else:
if text.find(PANICKED_AT_PATTERN) >= 0:
text = set_panicked_line_color(text)
elif TEST_LINE_MATCHER.match(text):
text = set_test_line_color(text)
elif text.find(TEST_RESULT_PATTERN) == 1:
text = set_test_result_line_color(text)
sys.stdout.write(text)
sys.stdout.flush()
def main(argv):
colorama.init()
os.environ['RUST_BACKTRACE'] = 'full'
verbose = os.getenv('COLORCARGO_VERBOSE', '') == '1'
args = ['cargo']
if len(argv) < 2:
args += argv[1:]
else:
args += [argv[1], '--color=always'] + argv[2:]
pipe = Popen(args=args, stdout=PIPE, stderr=STDOUT)
try:
thread = Thread(target=consume, args=(pipe, verbose))
thread.start()
thread.join()
except KeyboardInterrupt:
pipe.terminate()
try:
main(sys.argv)
except Exception as error:
debug('Parsing error: ', error)