Created
September 1, 2014 02:06
-
-
Save imerr/d2faa8a0c1d38a922cf4 to your computer and use it in GitHub Desktop.
Log format changer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
syslogre = re.compile('sys_log\([0-9]+, "([^"]*?)".*?\);', re.DOTALL) | |
# http://stackoverflow.com/a/8915445/1318435 | |
printfre = re.compile("(%(?:\d+\$)?[+-]?(?:[ 0]|'.{1})?-?\d*(?:\.\d+)?[bcdeEufFgGosxX])") | |
def processFile(file): | |
def replacer(m): | |
def incr(m): | |
incr.counter+=1 | |
return "{{{0}}}".format(incr.counter-1) | |
incr.counter = 0 | |
return m.string[ m.start(0): m.start(1)] + printfre.sub(incr, m.string[ m.start(1): m.end(1)]) + m.string[ m.end(1): m.end(0)] | |
with open(file, "r+") as f: | |
contents = f.read() | |
replaced = syslogre.sub(replacer, contents) | |
f.seek(0) | |
f.truncate() | |
f.write(replaced) | |
for root, dirs, files in os.walk("."): | |
for name in files: | |
file = os.path.join(root, name) | |
baseName, ext = os.path.splitext(file) | |
if ext in [".cpp", ".c", ".h", ".hpp"]: | |
print file | |
processFile(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment