logformat.py
47 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
/ scripts / logformat.py
Mads Kiilerich
|
r8053 | #!/usr/bin/env python3 | ||
Mads Kiilerich
|
r5586 | |||
import re | ||||
import sys | ||||
Mads Kiilerich
|
r7718 | |||
Mads Kiilerich
|
r5586 | logre = r''' | ||
(log\.(?:error|info|warning|debug) | ||||
[(][ \n]* | ||||
) | ||||
%s | ||||
( | ||||
[ \n]*[)] | ||||
) | ||||
''' | ||||
Mads Kiilerich
|
r7572 | |||
Mads Kiilerich
|
r5586 | res = [ | ||
# handle % () - keeping spaces around the old % | ||||
Mads Kiilerich
|
r7727 | (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) \( ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) \) ''', flags=re.MULTILINE | re.VERBOSE), r'\1\2,\3\4\5\6'), | ||
Mads Kiilerich
|
r5586 | # handle % without () - keeping spaces around the old % | ||
Mads Kiilerich
|
r7727 | (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE | re.VERBOSE), r'\1\2,\3\4\5\6'), | ||
Mads Kiilerich
|
r5586 | # remove extra space if it is on next line | ||
Mads Kiilerich
|
r7727 | (re.compile(logre % r'''("[^"]*"|'[^']*') , (\n [ ]) ([ ][\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE | re.VERBOSE), r'\1\2,\3\4\5\6'), | ||
Mads Kiilerich
|
r5586 | # remove extra space if it is on same line | ||
Mads Kiilerich
|
r7727 | (re.compile(logre % r'''("[^"]*"|'[^']*') , [ ]+ () ( [\n ]+) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) ''', flags=re.MULTILINE | re.VERBOSE), r'\1\2,\3\4\5\6'), | ||
Mads Kiilerich
|
r5586 | # remove trailing , and space | ||
Mads Kiilerich
|
r7727 | (re.compile(logre % r'''("[^"]*"|'[^']*') , () ( [\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* [^(), \n] ) [ ,]*''', flags=re.MULTILINE | re.VERBOSE), r'\1\2,\3\4\5\6'), | ||
Mads Kiilerich
|
r5586 | ] | ||
Mads Kiilerich
|
r7572 | |||
def rewrite(f): | ||||
Lars Kruse
|
r6785 | s = open(f).read() | ||
Mads Kiilerich
|
r5586 | for r, t in res: | ||
s = r.sub(t, s) | ||||
Lars Kruse
|
r6785 | open(f, 'w').write(s) | ||
Mads Kiilerich
|
r7572 | |||
if __name__ == '__main__': | ||||
if len(sys.argv) < 2: | ||||
Mads Kiilerich
|
r7750 | print('Cleanup of superfluous % formatting of log statements.') | ||
print('Usage:') | ||||
Mads Kiilerich
|
r8548 | print(''' hg revert `hg files 'set:**.py'|grep -v logformat.py` && scripts/logformat.py `hg files 'set:**.py'` && hg diff''') | ||
Mads Kiilerich
|
r7572 | raise SystemExit(1) | ||
for f in sys.argv[1:]: | ||||
rewrite(f) | ||||