8 class CheckError(Exception):
11 def __init__(self
, filename
, prefix
):
14 self
.check_no_output
= False
15 self
.filename
= filename
19 self
.lines
= [l
.rstrip("\r\n") for l
in sys
.stdin
.readlines()]
22 with
open(self
.filename
) as f
:
24 match
= re
.search("{}: NO_OUTPUT".format(self
.prefix
), line
)
26 self
.check_no_output
= True
29 "{}: num_threads=([0-9]+) (.*)$".format(self
.prefix
), line
32 num_threads
= int(match
.group(1))
33 for i
in range(num_threads
):
34 self
.checks
.append(match
.group(2))
38 # If no checks at all, then nothing to do
39 if len(self
.checks
) == 0 and not self
.check_no_output
:
40 print("Nothing to check for")
42 # Check if we are expecting no output
43 if self
.check_no_output
:
44 if len(self
.lines
) == 0:
47 raise Checks
.CheckError(
48 "{}: Output was found when expecting none.".format(self
.prefix
)
50 # Run through each check line and see if it exists in the output
51 # If it does, then delete the line from output and look for the
53 # If you don't find the line then raise Checks.CheckError
54 # If there are extra lines of output then raise Checks.CheckError
58 for idx
, line
in enumerate(self
.lines
):
59 if re
.search(c
, line
) is not None:
64 raise Checks
.CheckError("{}: Did not find: {}".format(self
.prefix
, c
))
67 if len(self
.lines
) != 0:
68 raise Checks
.CheckError(
69 "{}: Extra output: {}".format(self
.prefix
, self
.lines
)
73 # Setup argument parsing
74 parser
= argparse
.ArgumentParser(
75 description
="""This script checks output of
76 a program against "CHECK" lines in filename"""
78 parser
.add_argument("filename", default
=None, help="filename to check against")
84 help="check prefix token default: %(default)s",
86 command_args
= parser
.parse_args()
88 checks
= Checks(command_args
.filename
, command_args
.prefix
)