1 #!/usr/bin/python2 2 # 3 # Copyright 2010 Google Inc. All Rights Reserved. 4 """Script to summarize the results of various log files.""" 5 6 from __future__ import print_function 7 8 __author__ = 'raymes (at] google.com (Raymes Khoury)' 9 10 from cros_utils import command_executer 11 import os 12 import sys 13 import re 14 15 RESULTS_DIR = 'results' 16 RESULTS_FILE = RESULTS_DIR + '/results.csv' 17 18 # pylint: disable=anomalous-backslash-in-string 19 20 class DejaGNUSummarizer(object): 21 """DejaGNU Summarizer Class""" 22 23 def __int__(self): 24 pass 25 26 def Matches(self, log_file): 27 for log_line in log_file: 28 if log_line.find("""tests ===""") > -1: 29 return True 30 return False 31 32 def Summarize(self, log_file, filename): 33 result = '' 34 pass_statuses = ['PASS', 'XPASS'] 35 fail_statuses = ['FAIL', 'XFAIL', 'UNSUPPORTED'] 36 name_count = {} 37 for line in log_file: 38 line = line.strip().split(':') 39 if len(line) > 1 and (line[0] in pass_statuses or 40 line[0] in fail_statuses): 41 test_name = (':'.join(line[1:])).replace('\t', ' ').strip() 42 count = name_count.get(test_name, 0) + 1 43 name_count[test_name] = count 44 test_name = '%s (%s)' % (test_name, str(count)) 45 if line[0] in pass_statuses: 46 test_result = 'pass' 47 else: 48 test_result = 'fail' 49 result += '%s\t%s\t%s\n' % (test_name, test_result, filename) 50 return result 51 52 53 class PerflabSummarizer(object): 54 """Perflab Summarizer class""" 55 56 def __init__(self): 57 pass 58 59 def Matches(self, log_file): 60 p = re.compile('METRIC isolated \w+') 61 for log_line in log_file: 62 if p.search(log_line): 63 return True 64 return False 65 66 def Summarize(self, log_file, filename): 67 result = '' 68 p = re.compile("METRIC isolated (\w+) .*\['(.*?)'\]") 69 log_file_lines = '\n'.join(log_file) 70 matches = p.findall(log_file_lines) 71 for match in matches: 72 if len(match) != 2: 73 continue 74 result += '%s\t%s\t%s\n' % (match[0], match[1], filename) 75 return result 76 77 78 class AutoTestSummarizer(object): 79 """AutoTest Summarizer class""" 80 81 def __init__(self): 82 pass 83 84 def Matches(self, log_file): 85 for log_line in log_file: 86 if log_line.find("""Installing autotest on""") > -1: 87 return True 88 return False 89 90 def Summarize(self, log_file, filename): 91 result = '' 92 pass_statuses = ['PASS'] 93 fail_statuses = ['FAIL'] 94 for line in log_file: 95 line = line.strip().split(' ') 96 if len(line) > 1 and (line[-1].strip() in pass_statuses or 97 line[-1].strip() in fail_statuses): 98 test_name = (line[0].strip()) 99 if line[-1].strip() in pass_statuses: 100 test_result = 'pass' 101 else: 102 test_result = 'fail' 103 result += '%s\t%s\t%s\n' % (test_name, test_result, filename) 104 return result 105 106 107 def Usage(): 108 print('Usage: %s log_file' % sys.argv[0]) 109 sys.exit(1) 110 111 112 def SummarizeFile(filename): 113 summarizers = [DejaGNUSummarizer(), AutoTestSummarizer(), PerflabSummarizer()] 114 inp = open(filename, 'rb') 115 executer = command_executer.GetCommandExecuter() 116 for summarizer in summarizers: 117 inp.seek(0) 118 if summarizer.Matches(inp): 119 executer.CopyFiles(filename, RESULTS_DIR, recursive=False) 120 inp.seek(0) 121 result = summarizer.Summarize(inp, os.path.basename(filename)) 122 inp.close() 123 return result 124 inp.close() 125 return None 126 127 128 def Main(argv): 129 if len(argv) != 2: 130 Usage() 131 filename = argv[1] 132 133 executer = command_executer.GetCommandExecuter() 134 executer.RunCommand('mkdir -p %s' % RESULTS_DIR) 135 summary = SummarizeFile(filename) 136 if summary is not None: 137 output = open(RESULTS_FILE, 'a') 138 output.write(summary.strip() + '\n') 139 output.close() 140 return 0 141 142 143 if __name__ == '__main__': 144 retval = Main(sys.argv) 145 sys.exit(retval) 146