# # this is a parser function specially designed for the 'bzip2.log' # import re def log_parse(log_file): # set up rule for regex reObj = re.compile('^(PASS|FAIL+?): (.+)', re.MULTILINE) # init a dictionary, it stores the return result case_list = dict() # open file test_log = open(log_file, 'r') # start to read the new line line = test_log.readline() while line: if ("Testing decompression and recompression..." in line): # dectect trigger line for part 2, change regex rule reObj = re.compile('^(PASS|FAIL+?): /usr/lib/bzip2/ptest/bzip2-tests/(.+)', re.MULTILINE) elif ("Testing detection of bad input data..." in line): # dectect trigger line for part 3, change regex rule reObj = re.compile('^(PASS|FAIL+?): .+/usr/lib/bzip2/ptest/bzip2-tests/(.+)', re.MULTILINE) # checking regex status in line matchs = reObj.search(line) # if the line fits the regex rule if matchs: # spliting elements into a tuple # it looks like ('PASS', '') groups = matchs.groups() # adjust name tmp_name = groups[1] tmp_name = tmp_name.strip() case_name = tmp_name.replace('.bad', '.bad Check Integrity') # then just add a new key naming after case_name, along with its values case_list[case_name] = [case_name, groups[0], ""] # jump out if-else, read next line line = test_log.readline() test_log.close() return case_list