This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
forked from qca/boardfarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_human_readable.py
154 lines (140 loc) · 5.07 KB
/
make_human_readable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# Copyright (c) 2015
#
# All rights reserved.
#
# This file is distributed under the Clear BSD license.
# The full text can be found in LICENSE in the root directory.
import json
import os
import re
import sys
from string import Template
try:
from collections import Counter
except:
from future.moves.collections import Counter
import config
owrt_tests_dir = os.path.dirname(os.path.realpath(__file__))
def pick_template_filename():
'''
Decide which HTML file to use as template for results.
This allows for different format for different audiences.
'''
templates = {'basic': owrt_tests_dir+"/html/template_results_basic.html",
'full': owrt_tests_dir+"/html/template_results.html"}
if os.environ.get('test_suite') == 'daily_au':
return templates['basic']
else:
return templates['full']
def changes_to_html(changes):
'''
Input: "15408,8 17196,2 17204,1"
Output: String of html links, e.g.
<a href="https://gerrit.mysite.com/#/c/15408/">15408,8</a>,
<a href...
'''
if not changes:
return None
if not config.code_change_server:
return changes
base_url = config.code_change_server
list_changes = re.findall('\d+,\d+', changes)
if not list_changes:
return None
result = []
for c in list_changes:
try:
change_id, _ = c.split(',')
url = base_url + change_id
s = '<a href="%s">%s</a>' % (url, c)
result.append(s)
except:
continue
return ", ".join(result)
def xmlresults_to_html(test_results,
output_name=owrt_tests_dir+"/results/results.html",
title=None,
board_info={}):
parameters = {'build_url' : os.environ.get('BUILD_URL'),
'total_test_time' : 'unknown',
'summary_title' : title,
'changes': changes_to_html(os.environ.get('change_list')),
"board_type": "unknown",
"lan_device": "unknown",
"wan_device": "unknown",
"conn_cmd" : "unknown"}
try:
parameters.update(board_info)
except Exception as e:
print(e)
# categorize the results data
results_table_lines = []
results_fail_table_lines = []
grade_counter = Counter()
styles = {'OK': 'ok',
'Unexp OK': 'uok',
'SKIP': 'skip',
None: 'skip',
'FAIL': 'fail',
'Exp FAIL': 'efail'}
for i, t in enumerate(test_results):
t['num'] = i+1
t['style'] = styles[t['grade']]
if i % 2 == 0:
t['row_style'] = "even"
else:
t['row_style'] = "odd"
grade_counter[t['grade']] += 1
if 'FAIL' == t['grade']:
results_fail_table_lines.append('<tr class="%(row_style)s"><td>%(num)s</td><td class="%(style)s">%(grade)s</td><td>%(name)s</td></tr>' % t)
results_table_lines.append('<tr class="%(row_style)s"><td>%(num)s</td><td class="%(style)s">%(grade)s</td><td>%(name)s</td><td>%(message)s</td></tr>' % t)
if t['long_message'] != "":
results_table_lines.append('<tr class="%(row_style)s"><td colspan=4><pre align="left">' % t)
results_table_lines.append("%(long_message)s" % t)
results_table_lines.append('</pre></td></tr>')
# process the summary counter
results_summary_table_lines = []
for e, v in grade_counter.items():
t['style'] = styles[t['grade']]
results_summary_table_lines.append('<tr><td class="%s">%s: %d</td></tr>' % (styles[e], e, v))
# Create the results tables
parameters['table_results'] = "\n".join(results_table_lines)
if len(results_fail_table_lines) == 0:
parameters['table_fail_results'] = "<tr><td>None</td></tr>"
else:
parameters['table_fail_results'] = "\n".join(results_fail_table_lines)
parameters['table_summary_results'] = "\n".join(results_summary_table_lines)
# Other parameters
try:
test_seconds = int(os.environ.get('TEST_END_TIME'))-int(os.environ.get('TEST_START_TIME'))
parameters['total_test_time'] = "%s minutes" % (test_seconds/60)
except:
pass
# Substitute parameters into template html to create new html file
template_filename = pick_template_filename()
f = open(template_filename, "r").read()
s = Template(f)
f = open(output_name, "w")
f.write(s.substitute(parameters))
f.close()
print("Created %s" % output_name)
def get_title():
try:
title = os.environ.get('summary_title')
if title:
return title
except:
pass
try:
return os.environ.get('JOB_NAME')
except:
return None
if __name__ == '__main__':
try:
list_results = json.load(open(sys.argv[1], 'r'))['test_results']
xmlresults_to_html(list_results, title="Test Results")
except Exception as e:
print(e)
print("To use make_human_readable.py:")
print("./make_human_readable.py results/test_results.json")