fix_failure_locations.py [plain text]
import sys
import os
import re
from collections import defaultdict
from datetime import datetime,timezone
class LocationUpdater(object):
epoch = datetime(2020, 6, 17, 23, 22, 46, 562458, tzinfo=timezone.utc)
location_base = int((datetime.now(timezone.utc) - epoch).total_seconds() / 60)
fail_re = (re.compile('(?<=\\bRECORD_FAILURE\\()\\d+(?=,)'),re.compile('(?<=\\bRECORD_FAILURE_MSG\\()\\d+(?=,)'))
def __init__(self, path):
self.location = self.location_base
self.path = path
self.counts = defaultdict(int)
self.locations_changed = 0
def fixLocations(self):
def replace_loc(match):
location = int(match.group(0))
if location == 0:
self.location += 1
self.locations_changed += 1
location = self.location
self.counts[location] += 1
return str(location)
rootpath = self.path
for dirpath, dirnames, filenames in os.walk(rootpath):
for filename in filenames:
if filename.endswith(".c") or filename.endswith(".cpp"):
path = os.path.join(dirpath, filename)
content = open(path, "r").read()
for fail_re in self.fail_re:
if fail_re.search(content):
locations_changed_before = self.locations_changed
content = fail_re.sub(replace_loc, content)
if self.locations_changed != locations_changed_before:
print("Updating file {}".format(path))
open(path,"w").write(content)
def duplicates(self):
return [k for (k,v) in iter(self.counts.items()) if v > 1]
updater = LocationUpdater(sys.argv[1])
updater.fixLocations()
dups = updater.duplicates()
if len(dups):
print("WARNING! Duplicate location numbers: {}".format(dups))
sys.exit(1)