| 1 |
#!/usr/bin/env python3 |
| 2 |
''' |
| 3 |
Clean log file output from the fuzztest test harness. This tool collapses |
| 4 |
duplicate outputs into a single copy preceded by all the fuzz lines that |
| 5 |
triggered it. To allow useful comparisons of fuzztest logs it imposes |
| 6 |
a canonical ordering on the entries. |
| 7 |
''' |
| 8 |
import argparse |
| 9 |
import collections |
| 10 |
import errno |
| 11 |
import operator |
| 12 |
import re |
| 13 |
import sys |
| 14 |
from itertools import chain, groupby, zip_longest, repeat |
| 15 |
|
| 16 |
__version__ = "1.0" |
| 17 |
__date__ = "15 September 2012" |
| 18 |
__author__ = "Tim Eves <[email protected]>" |
| 19 |
__license__ = ''' |
| 20 |
SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later |
| 21 |
Copyright 2012, SIL International, All rights reserved. |
| 22 |
''' |
| 23 |
|
| 24 |
recm = re.compile(r'^(-?\d+)?\s*,\s*(0[xX][\da-fA-F]+)\s*,\s*' |
| 25 |
r'(0[xX][\da-fA-F]+|\d+)\s*,?(.*)$') |
| 26 |
valgm = re.compile(r'^==\d+==(\s+(?:at|by)?\s*)(?:0[xX][\da-fA-F]+:)?', |
| 27 |
re.MULTILINE) |
| 28 |
|
| 29 |
|
| 30 |
class fuzz(collections.namedtuple('fuzz', 'ret position value comment')): |
| 31 |
def __str__(self): |
| 32 |
return "{0},{1.position:#010X},{1.value: >3d}{2!s}{3!s}".format( |
| 33 |
self.ret or '', |
| 34 |
self, |
| 35 |
(self.comment or '') and ',', |
| 36 |
self.comment or '') |
| 37 |
|
| 38 |
|
| 39 |
class fuzz_log(collections.defaultdict): |
| 40 |
@staticmethod |
| 41 |
def __is_rec(s, rm=recm): |
| 42 |
return bool(rm.match(s)) |
| 43 |
|
| 44 |
@staticmethod |
| 45 |
def __recs(rs): |
| 46 |
rs = [fuzz(int(r, 0), int(p, 0), int(o, 0), c) for r in rs |
| 47 |
for r, p, o, c in [recm.match(r).groups('0')]] |
| 48 |
return chain(*list(zip(rs[:-1], repeat(None))) + [(rs[-1],)]) |
| 49 |
|
| 50 |
def __init__(self, fileobj): |
| 51 |
super(fuzz_log, self).__init__(list) |
| 52 |
cs = chain.from_iterable( |
| 53 |
self.__recs(ls) if r else (valgm.sub(r'\1', ''.join(ls)).lstrip(),) |
| 54 |
for r, ls in groupby(fileobj, self.__is_rec)) |
| 55 |
for rec in zip_longest(cs, cs): |
| 56 |
self[rec[1]].append(rec[0]) |
| 57 |
for rs in self.values(): |
| 58 |
rs.sort(key=operator.itemgetter(1)) |
| 59 |
|
| 60 |
|
| 61 |
def record_sort_key(r): |
| 62 |
return r[1][0].position |
| 63 |
|
| 64 |
|
| 65 |
parser = argparse.ArgumentParser(description=__doc__) |
| 66 |
parser.add_argument('log', nargs='?', type=argparse.FileType('rt'), |
| 67 |
default=sys.stdin, help='A log file generated by fuzztest') |
| 68 |
parser.add_argument('out', nargs='?', |
| 69 |
type=argparse.FileType('wt'), default=sys.stdout, |
| 70 |
help='The file to send the processed log to') |
| 71 |
parser.add_argument('--version', action='version', version=__version__) |
| 72 |
|
| 73 |
if __name__ == '__main__': |
| 74 |
args = parser.parse_args() |
| 75 |
try: |
| 76 |
for err, rs in sorted(fuzz_log(args.log).items(), |
| 77 |
key=record_sort_key): |
| 78 |
args.out.writelines(['\n'.join(map(str, rs)), '\n']) |
| 79 |
if err: |
| 80 |
args.out.write(err) |
| 81 |
args.out.flush() |
| 82 |
except IOError as io: |
| 83 |
if io.errno != errno.EPIPE: |
| 84 |
sys.stderr.write("{0}: {1!s}\n".format(parser.prog, io)) |
| 85 |
sys.exit(1) |