comparison scripts/logformat.py @ 7666:4473f1094d3d

scripts: clean up and run the old scripts/logformat.py script
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 05 Jan 2019 14:57:49 +0100
parents 665dfa112f2c
children 0a277465fddf
comparison
equal deleted inserted replaced
7665:8fbcdfe364d4 7666:4473f1094d3d
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 2
3 import re 3 import re
4 import sys 4 import sys
5
6 if len(sys.argv) < 2:
7 print 'Cleanup of superfluous % formatting of log statements.'
8 print 'Usage:'
9 print ''' hg revert `hg loc '*.py'|grep -v logformat.py` && scripts/logformat.py `hg loc '*.py'` && hg diff'''
10 raise SystemExit(1)
11
12 5
13 logre = r''' 6 logre = r'''
14 (log\.(?:error|info|warning|debug) 7 (log\.(?:error|info|warning|debug)
15 [(][ \n]* 8 [(][ \n]*
16 ) 9 )
17 %s 10 %s
18 ( 11 (
19 [ \n]*[)] 12 [ \n]*[)]
20 ) 13 )
21 ''' 14 '''
15
16
22 res = [ 17 res = [
23 # handle % () - keeping spaces around the old % 18 # handle % () - keeping spaces around the old %
24 (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) \( ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) \) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), 19 (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) \( ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) \) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'),
25 # handle % without () - keeping spaces around the old % 20 # handle % without () - keeping spaces around the old %
26 (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), 21 (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'),
30 (re.compile(logre % r'''("[^"]*"|'[^']*') , [ ]+ () ( [\n ]+) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), 25 (re.compile(logre % r'''("[^"]*"|'[^']*') , [ ]+ () ( [\n ]+) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'),
31 # remove trailing , and space 26 # remove trailing , and space
32 (re.compile(logre % r'''("[^"]*"|'[^']*') , () ( [\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* [^(), \n] ) [ ,]*''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), 27 (re.compile(logre % r'''("[^"]*"|'[^']*') , () ( [\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* [^(), \n] ) [ ,]*''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'),
33 ] 28 ]
34 29
35 for f in sys.argv[1:]: 30
31 def rewrite(f):
36 s = open(f).read() 32 s = open(f).read()
37 for r, t in res: 33 for r, t in res:
38 s = r.sub(t, s) 34 s = r.sub(t, s)
39 open(f, 'w').write(s) 35 open(f, 'w').write(s)
36
37
38 if __name__ == '__main__':
39 if len(sys.argv) < 2:
40 print 'Cleanup of superfluous % formatting of log statements.'
41 print 'Usage:'
42 print ''' hg revert `hg loc '*.py'|grep -v logformat.py` && scripts/logformat.py `hg loc '*.py'` && hg diff'''
43 raise SystemExit(1)
44
45 for f in sys.argv[1:]:
46 rewrite(f)