1#!/usr/bin/python 2""" 3Program that parses the autotest results and return a nicely printed final test 4result. 5 6@copyright: Red Hat 2008-2009 7""" 8 9def parse_results(text): 10 """ 11 Parse text containing Autotest results. 12 13 @return: A list of result 4-tuples. 14 """ 15 result_list = [] 16 start_time_list = [] 17 info_list = [] 18 19 lines = text.splitlines() 20 for line in lines: 21 line = line.strip() 22 parts = line.split("\t") 23 24 # Found a START line -- get start time 25 if (line.startswith("START") and len(parts) >= 5 and 26 parts[3].startswith("timestamp")): 27 start_time = float(parts[3].split("=")[1]) 28 start_time_list.append(start_time) 29 info_list.append("") 30 31 # Found an END line -- get end time, name and status 32 elif (line.startswith("END") and len(parts) >= 5 and 33 parts[3].startswith("timestamp")): 34 end_time = float(parts[3].split("=")[1]) 35 start_time = start_time_list.pop() 36 info = info_list.pop() 37 test_name = parts[2] 38 test_status = parts[0].split()[1] 39 # Remove "kvm." prefix 40 if test_name.startswith("kvm."): 41 test_name = test_name[4:] 42 result_list.append((test_name, test_status, 43 int(end_time - start_time), info)) 44 45 # Found a FAIL/ERROR/GOOD line -- get failure/success info 46 elif (len(parts) >= 6 and parts[3].startswith("timestamp") and 47 parts[4].startswith("localtime")): 48 info_list[-1] = parts[5] 49 50 return result_list 51 52 53def print_result(result, name_width): 54 """ 55 Nicely print a single Autotest result. 56 57 @param result: a 4-tuple 58 @param name_width: test name maximum width 59 """ 60 if result: 61 format = "%%-%ds %%-10s %%-8s %%s" % name_width 62 print format % result 63 64 65def main(resfiles): 66 result_lists = [] 67 name_width = 40 68 69 for resfile in resfiles: 70 try: 71 text = open(resfile).read() 72 except IOError: 73 print "Bad result file: %s" % resfile 74 continue 75 results = parse_results(text) 76 result_lists.append((resfile, results)) 77 name_width = max([name_width] + [len(r[0]) for r in results]) 78 79 print_result(("Test", "Status", "Seconds", "Info"), name_width) 80 print_result(("----", "------", "-------", "----"), name_width) 81 82 for resfile, results in result_lists: 83 print " (Result file: %s)" % resfile 84 for r in results: 85 print_result(r, name_width) 86 87 88if __name__ == "__main__": 89 import sys, glob 90 91 resfiles = glob.glob("../../results/default/status*") 92 if len(sys.argv) > 1: 93 if sys.argv[1] == "-h" or sys.argv[1] == "--help": 94 print "Usage: %s [result files]" % sys.argv[0] 95 sys.exit(0) 96 resfiles = sys.argv[1:] 97 main(resfiles) 98