92 lines · python
1import os2import sys3import argparse4import re5 6 7class Checks(object):8 class CheckError(Exception):9 pass10 11 def __init__(self, filename, prefix):12 self.checks = []13 self.lines = []14 self.check_no_output = False15 self.filename = filename16 self.prefix = prefix17 18 def readStdin(self):19 self.lines = [l.rstrip("\r\n") for l in sys.stdin.readlines()]20 21 def readChecks(self):22 with open(self.filename) as f:23 for line in f:24 match = re.search("{}: NO_OUTPUT".format(self.prefix), line)25 if match is not None:26 self.check_no_output = True27 return28 match = re.search(29 "{}: num_threads=([0-9]+) (.*)$".format(self.prefix), line30 )31 if match is not None:32 num_threads = int(match.group(1))33 for i in range(num_threads):34 self.checks.append(match.group(2))35 continue36 37 def check(self):38 # If no checks at all, then nothing to do39 if len(self.checks) == 0 and not self.check_no_output:40 print("Nothing to check for")41 return42 # Check if we are expecting no output43 if self.check_no_output:44 if len(self.lines) == 0:45 return46 else:47 raise Checks.CheckError(48 "{}: Output was found when expecting none.".format(self.prefix)49 )50 # Run through each check line and see if it exists in the output51 # If it does, then delete the line from output and look for the52 # next check line.53 # If you don't find the line then raise Checks.CheckError54 # If there are extra lines of output then raise Checks.CheckError55 for c in self.checks:56 found = False57 index = -158 for idx, line in enumerate(self.lines):59 if re.search(c, line) is not None:60 found = True61 index = idx62 break63 if not found:64 raise Checks.CheckError("{}: Did not find: {}".format(self.prefix, c))65 else:66 del self.lines[index]67 if len(self.lines) != 0:68 raise Checks.CheckError(69 "{}: Extra output: {}".format(self.prefix, self.lines)70 )71 72 73# Setup argument parsing74parser = argparse.ArgumentParser(75 description="""This script checks output of76 a program against "CHECK" lines in filename"""77)78parser.add_argument("filename", default=None, help="filename to check against")79parser.add_argument(80 "-c",81 "--check-prefix",82 dest="prefix",83 default="CHECK",84 help="check prefix token default: %(default)s",85)86command_args = parser.parse_args()87# Do the checking88checks = Checks(command_args.filename, command_args.prefix)89checks.readStdin()90checks.readChecks()91checks.check()92