#!/usr/bin/env python

import sys, re

suppfile = sys.argv[1]
summaryfile = sys.argv[2]

f = open(suppfile, 'r')
suppressions = f.readlines()
f.close()

f = open(summaryfile, 'r')
summary = f.readlines()
f.close()

newsummary = list()
removedsupps = list()
for l in summary:
    line = l.strip()
    if line.startswith('[Error'):
        removed = False
        for s in suppressions:
            supp = s.strip()
            if not supp.startswith('#'):
                if re.search(r'\b'+supp+r'\b', line):
                    print 'Removing suppression for: '+supp
                    removedsupps.append(supp)
                    removed = True
                    break
        if not removed:
            newsummary.append(line)
    else:
        newsummary.append(line)

# overwrite old summary file

# date 'n stuff
f = open(summaryfile, 'w')
f.write(newsummary.pop(0)+'\n')

# missing suppressions
for s in suppressions:
    supp = s.strip()
    if not supp.startswith('#'):
        if removedsupps.count(supp) == 0:
            f.write('[Error: did not find expected suppression '+supp+']\n')

# new summary
for l in newsummary:
    f.write(l+'\n')

f.close()

sys.exit(0)
