Commit 8c548cc6 authored by norvell's avatar norvell
Browse files

Added script to replace strings, respecting WMC_TOOL_SKIP

parent f183d8b2
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
#!/bin/python3

import argparse
import re

parser = argparse.ArgumentParser(description='Substitute strings in files, skipping blocks in WMC_TOOL_SKIP')
parser.add_argument('input',type=str,help='C source file')
parser.add_argument('target',type=str,help='Target string to replace')
parser.add_argument('dest',type=str,help='Destination string to insert')
args = parser.parse_args()
fileIn  = args.input
target  = args.target
dest    = args.dest

skip = 0

with open(fileIn, 'r') as f_in:
    lines = f_in.readlines()
with open(fileIn, 'w') as f_out:
    for line in lines:
        if re.search(r'#define\W+WMC_TOOL_SKIP', line):
            skip = 1
        elif re.search(r'#undef\W+WMC_TOOL_SKIP', line):
            skip = 0
        else:
            if not skip:
                line = re.sub(r'\b%s\b' % (target), '%s' % (dest), line)
        f_out.write(line)
 No newline at end of file