diff --git a/scripts/logformat.py b/scripts/logformat.py --- a/scripts/logformat.py +++ b/scripts/logformat.py @@ -3,13 +3,6 @@ import re import sys -if len(sys.argv) < 2: - print 'Cleanup of superfluous % formatting of log statements.' - print 'Usage:' - print ''' hg revert `hg loc '*.py'|grep -v logformat.py` && scripts/logformat.py `hg loc '*.py'` && hg diff''' - raise SystemExit(1) - - logre = r''' (log\.(?:error|info|warning|debug) [(][ \n]* @@ -19,6 +12,8 @@ logre = r''' [ \n]*[)] ) ''' + + res = [ # handle % () - keeping spaces around the old % (re.compile(logre % r'''("[^"]*"|'[^']*') ([\n ]*) % ([\n ]*) \( ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* ) \) ''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), @@ -32,8 +27,20 @@ res = [ (re.compile(logre % r'''("[^"]*"|'[^']*') , () ( [\n ]*) ( (?:[^()]|\n)* (?: \( (?:[^()]|\n)* \) (?:[^()]|\n)* )* [^(), \n] ) [ ,]*''', flags=re.MULTILINE|re.VERBOSE), r'\1\2,\3\4\5\6'), ] -for f in sys.argv[1:]: + +def rewrite(f): s = open(f).read() for r, t in res: s = r.sub(t, s) open(f, 'w').write(s) + + +if __name__ == '__main__': + if len(sys.argv) < 2: + print 'Cleanup of superfluous % formatting of log statements.' + print 'Usage:' + print ''' hg revert `hg loc '*.py'|grep -v logformat.py` && scripts/logformat.py `hg loc '*.py'` && hg diff''' + raise SystemExit(1) + + for f in sys.argv[1:]: + rewrite(f)