Make log parsing Python3, via 2to3 Signed-off-by: Luke Mondy <luke.mondy@data61.csiro.au>
diff --git a/libsel4test/tools/extract_results.py b/libsel4test/tools/extract_results.py index e57e469..8cc9b0a 100755 --- a/libsel4test/tools/extract_results.py +++ b/libsel4test/tools/extract_results.py
@@ -1,6 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -# Copyright 2017, Data61 +# Copyright 2020, Data61 # Commonwealth Scientific and Industrial Research Organisation (CSIRO) # ABN 41 687 119 230. # @@ -9,22 +9,27 @@ # See "LICENSE_BSD2.txt" for details. # # @TAG(DATA61_BSD) -import argparse, bs4, functools, re, sys +import argparse +import bs4 +import functools +import re +import sys SYS_OUT = 'system-out' -XML_SPECIAL_CHARS = {'<':"<",'&':"&",'>':">",'"':""","'":"'"} +XML_SPECIAL_CHARS = {'<': "<", '&': "&", '>': ">", '"': """, "'": "'"} TAG_WHITELIST = { # Keys are tags to be emitted, values are whether to emit their inner text. - 'error':True, - 'failure':True, - 'testsuite':False, - 'testcase':False, - SYS_OUT:True, + 'error': True, + 'failure': True, + 'testsuite': False, + 'testcase': False, + SYS_OUT: True, } TOP_TAG = 'testsuite' + def print_tag(f, tag): assert isinstance(tag, bs4.element.Tag) @@ -35,51 +40,50 @@ # If we want the inner text, just blindly dump the soup. if TAG_WHITELIST[tag.name]: if tag.name != SYS_OUT: - print >>f, tag + print(tag, file=f) else: - print >>f, '<%s>' % tag.name + print('<%s>' % tag.name, file=f) text = tag.get_text() for ch in text: if ch not in XML_SPECIAL_CHARS: f.write(ch) else: f.write(XML_SPECIAL_CHARS[ch]) - print >>f, '</%s>' % tag.name + print('</%s>' % tag.name, file=f) else: - print >>f, '<%(name)s %(attrs)s>' % { - 'name':tag.name, - 'attrs':' '.join(map(lambda x: '%s="%s"' % (x[0], x[1]), - tag.attrs.items())), - } + print('<%(name)s %(attrs)s>' % { + 'name': tag.name, + 'attrs': ' '.join(['%s="%s"' % (x[0], x[1]) for x in list(tag.attrs.items())]), + }, file=f) # Recurse for our children. - map(functools.partial(print_tag, f), - filter(lambda x: isinstance(x, bs4.element.Tag), - tag.children)) + list(map(functools.partial(print_tag, f), + [x for x in tag.children if isinstance(x, bs4.element.Tag)])) - print >>f, '</%s>' % tag.name + print('</%s>' % tag.name, file=f) + def main(): parser = argparse.ArgumentParser('Cleanup messy XML output from sel4test') parser.add_argument('input', - nargs='?', help='Input file', type=argparse.FileType('r'), - default=sys.stdin) + nargs='?', help='Input file', type=argparse.FileType('r'), + default=sys.stdin) parser.add_argument('output', - nargs='?', help='Output file', type=argparse.FileType('w'), - default=sys.stdout) + nargs='?', help='Output file', type=argparse.FileType('w'), + default=sys.stdout) parser.add_argument('--quiet', '-q', - help='Suppress unmodified output to stdout', action='store_true', - default=False) + help='Suppress unmodified output to stdout', action='store_true', + default=False) args = parser.parse_args() data = args.input.read() # Strip trailing crap around the XML we want to parse. Without this, even # BeautifulSoup sometimes backs away in horror. - regexp = re.compile(r'(<%(top)s>.*</%(top)s>)' % {'top':TOP_TAG}, re.S) + regexp = re.compile(r'(<%(top)s>.*</%(top)s>)' % {'top': TOP_TAG}, re.S) matches = re.search(regexp, data) if not matches or len(matches.groups()) != 1: - print >>sys.stderr, 'Failed to strip leading and trailing garbage' + print('Failed to strip leading and trailing garbage', file=sys.stderr) return -1 data = matches.group(0) @@ -87,28 +91,29 @@ # means end users have a chance of determining what went wrong from the # original output. if not args.quiet: - print data + print(data) # Parse the input as HTML even though BS supports XML. It seems the XML # parser is a bit more precious about the input. try: soup = bs4.BeautifulSoup(data, "lxml") except Exception as inst: - print >>sys.stderr, 'Failed to parse input: %s' % inst + print('Failed to parse input: %s' % inst, file=sys.stderr) return -1 try: top = soup.find_all(TOP_TAG)[0] except Exception as inst: - print >>sys.stderr, 'Failed to find initial %s tag: %s' % (TOP_TAG, inst) + print('Failed to find initial %s tag: %s' % (TOP_TAG, inst), file=sys.stderr) return -1 try: print_tag(args.output, top) except Exception as inst: - print >>sys.stderr, 'While navigating XML: %s' % inst + print('While navigating XML: %s' % inst, file=sys.stderr) return 0 + if __name__ == '__main__': sys.exit(main())