forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautofix_debuglog_calls.py
More file actions
72 lines (61 loc) · 1.51 KB
/
autofix_debuglog_calls.py
File metadata and controls
72 lines (61 loc) · 1.51 KB
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
#!/usr/bin/env python2
#
# Automatically fix one-line broken debug log calls. Adds a missing
# wrapper for such lines, e.g. changes:
#
# DUK_DPRINT(...);
#
# into:
#
# DUK_D(DUK_DPRINT(...));
#
# Does not handle multiline log calls.
#
# Usage:
#
# $ python autofix_debuglog_calls.py src-input/*.c
#
# WARNING: works in place, so commit any changes before running, then
# check diff.
#
import os
import sys
import re
re_callsite = re.compile(r'^\s*(DUK_D+PRINT).*?;$')
wrappers = {
'DUK_DPRINT': 'DUK_D',
'DUK_DDPRINT': 'DUK_DD',
'DUK_DDDPRINT': 'DUK_DDD'
}
warnings = []
def process(filename):
f = open(filename, 'rb')
output = []
linenumber = 0
fixes = 0
for line in f:
linenumber += 1
if 'DPRINT' not in line:
output.append(line)
continue
m = re_callsite.match(line)
if m is None:
output.append(line)
continue
log_macro = m.group(1)
log_wrapper = wrappers[log_macro]
line = line.replace(log_macro, log_wrapper + '(' + log_macro) # DUK_DPRINT( -> DUK_D(DUK_DPRINT(
line = line.replace(');', '));') # ...); -> ...));
output.append(line)
fixes += 1
f.close()
if fixes > 0:
print '%s: %d fixes' % (filename, fixes)
f = open(filename, 'wb')
f.write(''.join(output))
f.close()
def main():
for filename in sys.argv[1:]:
process(filename)
if __name__ == '__main__':
main()