Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All Rights Reserved. | 2 # Copyright 2016 The Chromium Authors. All Rights Reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 # Load jinja2. | 12 # Load jinja2. |
| 13 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 14 BASE_DIR = os.path.abspath(os.path.join( | 14 BASE_DIR = os.path.abspath(os.path.join( |
| 15 CURRENT_DIR, '..', '..', '..', '..', '..')) | 15 CURRENT_DIR, '..', '..', '..', '..', '..')) |
| 16 sys.path.append(os.path.join(BASE_DIR, 'third_party', 'markupsafe')) | 16 sys.path.append(os.path.join(BASE_DIR, 'third_party', 'markupsafe')) |
| 17 sys.path.append(os.path.join(BASE_DIR, 'third_party', 'jinja2')) | 17 sys.path.append(os.path.join(BASE_DIR, 'third_party', 'jinja2')) |
| 18 import jinja2 | 18 import jinja2 |
| 19 jinja_environment = jinja2.Environment( | 19 jinja_environment = jinja2.Environment( |
| 20 loader = jinja2.FileSystemLoader(os.path.dirname(__file__))) | 20 loader = jinja2.FileSystemLoader(os.path.dirname(__file__))) |
| 21 | 21 |
| 22 # Get result details from json path and then convert results to html. | 22 # Get result details from json path and then convert results to html. |
| 23 def result_details(json_path): | 23 def result_details(json_path, cs_base_url): |
| 24 with open(json_path) as json_file: | 24 with open(json_path) as json_file: |
| 25 json_object = json.loads(json_file.read()) | 25 json_object = json.loads(json_file.read()) |
| 26 results_list = [] | 26 results_list = [] |
| 27 for testsuite_run in json_object['per_iteration_data']: | 27 for testsuite_run in json_object['per_iteration_data']: |
| 28 for test, test_runs in testsuite_run.iteritems(): | 28 for test, test_runs in testsuite_run.iteritems(): |
| 29 results_list.extend([ | 29 results_list.extend([ |
| 30 { | 30 { |
| 31 'name': test, | 31 'name': test, |
| 32 'status': tr['status'], | 32 'status': tr['status'], |
| 33 'duration': tr['elapsed_time_ms'], | 33 'duration': tr['elapsed_time_ms'], |
| 34 'output_snippet' : tr['output_snippet'] | 34 'output_snippet' : tr['output_snippet'] |
| 35 } for tr in test_runs]) | 35 } for tr in test_runs]) |
| 36 return results_to_html(results_list) | 36 return results_to_html(results_list, cs_base_url) |
| 37 | 37 |
| 38 # Convert list of test results into html format. | 38 # Convert list of test results into html format. |
| 39 def results_to_html(results): | 39 def results_to_html(results, cs_base_url): |
| 40 def code_search(test): | |
| 41 search = test.replace('#', '.') | |
| 42 url = cs_base_url if cs_base_url else 'https://cs.chromium.org' | |
| 43 return '%s/?q=%s&type=cs' % (url, search) | |
| 44 | |
| 40 suite_row_dict = {} | 45 suite_row_dict = {} |
| 41 test_row_list = [] | 46 test_row_list = [] |
| 42 for result in results: | 47 for result in results: |
| 43 data = [{'data': result['name'], 'class': 'align-left'}, | 48 # Constructing test_row_list. |
| 44 {'data': result['status'], 'class': 'align-center'}, | 49 data = [{'data': result['name'], 'class': 'left', |
| 45 {'data': result['duration'], 'class': 'align-center'}, | 50 'link': code_search(result['name'])}, |
| 46 {'data': result['output_snippet'], 'class': 'align-left is-pre'}] | 51 {'data': result['status'], |
| 52 'class': 'center ' + result['status'].lower()}, | |
| 53 {'data': result['duration'], 'class': 'center'}, | |
| 54 {'data': result['output_snippet'], | |
| 55 'class': 'left', 'is_pre': True}] | |
| 47 | 56 |
| 48 test_row_list.append(data) | 57 test_row_list.append(data) |
| 49 suite_name = result['name'][:result['name'].index('#')] | |
| 50 | 58 |
| 59 # Constructing suite_row_dict | |
| 60 test_case_path = result['name'] | |
| 61 suite_name = test_case_path[:test_case_path.index('#')] | |
| 51 # 'suite_row' is [name, success_count, fail_count, all_count, time]. | 62 # 'suite_row' is [name, success_count, fail_count, all_count, time]. |
| 52 SUCCESS_COUNT = 1 | 63 SUCCESS_COUNT = 1 |
| 53 FAIL_COUNT = 2 | 64 FAIL_COUNT = 2 |
| 54 ALL_COUNT = 3 | 65 ALL_COUNT = 3 |
| 55 TIME = 4 | 66 TIME = 4 |
| 56 | 67 |
| 57 if suite_name in suite_row_dict: | 68 if suite_name in suite_row_dict: |
| 58 suite_row = suite_row_dict[suite_name] | 69 suite_row = suite_row_dict[suite_name] |
| 59 else: | 70 else: |
| 60 suite_row = [{'data': suite_name, 'class' : 'align-left'}, | 71 suite_row = [{'data': suite_name, 'class' : 'left'}, |
| 61 {'data': 0, 'class': 'align-center'}, | 72 {'data': 0, 'class': 'center'}, |
| 62 {'data': 0, 'class': 'align-center'}, | 73 {'data': 0, 'class': 'center'}, |
| 63 {'data': 0, 'class': 'align-center'}, | 74 {'data': 0, 'class': 'center'}, |
| 64 {'data': 0, 'class': 'align-center'}] | 75 {'data': 0, 'class': 'center'}] |
| 65 suite_row_dict[suite_name] = suite_row | 76 suite_row_dict[suite_name] = suite_row |
| 66 | 77 |
| 67 suite_row[ALL_COUNT]['data'] += 1 | 78 suite_row[ALL_COUNT]['data'] += 1 |
| 68 if result['status'] == 'SUCCESS': | 79 if result['status'] == 'SUCCESS': |
| 69 suite_row[SUCCESS_COUNT]['data'] += 1 | 80 suite_row[SUCCESS_COUNT]['data'] += 1 |
| 70 elif result['status'] == 'FAILURE': | 81 elif result['status'] == 'FAILURE': |
| 71 suite_row[FAIL_COUNT]['data'] += 1 | 82 suite_row[FAIL_COUNT]['data'] += 1 |
| 72 suite_row[TIME]['data'] += result['duration'] | 83 suite_row[TIME]['data'] += result['duration'] |
| 73 | 84 |
| 85 for suite in suite_row_dict.values(): | |
| 86 if suite[FAIL_COUNT]['data'] > 0: | |
| 87 suite[FAIL_COUNT]['class'] += ' failure' | |
| 88 else: | |
| 89 suite[FAIL_COUNT]['class'] += ' success' | |
| 90 | |
| 74 test_table_values = { | 91 test_table_values = { |
| 75 'table_id' : 'test_table', | 92 'table_id' : 'test_table', |
| 76 'table_headers' : [('text', 'test_name'), | 93 'table_headers' : [('text', 'test_name'), |
| 77 ('text', 'status'), | 94 ('text', 'status'), |
| 78 ('number', 'duration'), | 95 ('number', 'duration'), |
| 79 ('text', 'output_snippet'), | 96 ('text', 'output_snippet'), |
| 80 ], | 97 ], |
| 98 'table_class' : 'info', | |
|
the real yoland
2016/08/11 06:35:16
I think all the table class are just info, right?
BigBossZhiling
2016/08/11 06:49:45
Done.
| |
| 81 'table_rows' : test_row_list, | 99 'table_rows' : test_row_list, |
| 82 } | 100 } |
| 83 | 101 |
| 84 suite_table_values = { | 102 suite_table_values = { |
| 85 'table_id' : 'suite_table', | 103 'table_id' : 'suite_table', |
| 86 'table_headers' : [('text', 'suite_name'), | 104 'table_headers' : [('text', 'suite_name'), |
| 87 ('number', 'number_success_tests'), | 105 ('number', 'number_success_tests'), |
| 88 ('number', 'number_fail_tests'), | 106 ('number', 'number_fail_tests'), |
| 89 ('number', 'all_tests'), | 107 ('number', 'all_tests'), |
| 90 ('number', 'elapsed_time_ms'), | 108 ('number', 'elapsed_time_ms'), |
| 91 ], | 109 ], |
| 110 'table_class' : 'info', | |
| 92 'table_rows' : suite_row_dict.values(), | 111 'table_rows' : suite_row_dict.values(), |
| 93 } | 112 } |
| 94 | 113 |
| 95 main_template = jinja_environment.get_template( | 114 main_template = jinja_environment.get_template( |
| 96 os.path.join('template', 'main.html')) | 115 os.path.join('template', 'main.html')) |
| 97 return main_template.render( | 116 return main_template.render( |
| 98 {'tb_values': [suite_table_values, test_table_values]}) | 117 {'tb_values': [suite_table_values, test_table_values]}) |
| 99 | 118 |
| 100 def main(): | 119 def main(): |
| 101 logging.basicConfig(level=logging.INFO) | 120 logging.basicConfig(level=logging.INFO) |
| 102 parser = argparse.ArgumentParser() | 121 parser = argparse.ArgumentParser() |
| 103 parser.add_argument('--json-file', help='Path of json file.', required=True) | 122 parser.add_argument('--json-file', help='Path of json file.', required=True) |
| 104 parser.add_argument('--html-file', help='Path to store html file.', | 123 parser.add_argument('--html-file', help='Path to store html file.', |
| 105 required=True) | 124 required=True) |
| 125 parser.add_argument('--cs-base-url', help='Base url for code search.') | |
| 126 | |
| 106 args = parser.parse_args() | 127 args = parser.parse_args() |
| 107 if os.path.exists(args.json_file): | 128 if os.path.exists(args.json_file): |
| 108 result_html_string = result_details(args.json_file) | 129 result_html_string = result_details(args.json_file, args.cs_base_url) |
| 109 | 130 |
| 110 with open(args.html_file, 'w') as html: | 131 with open(args.html_file, 'w') as html: |
| 111 html.write(result_html_string) | 132 html.write(result_html_string) |
| 112 else: | 133 else: |
| 113 raise exception('Json file of result details is not found.') | 134 raise Exception('Json file of result details is not found.') |
| 114 | 135 |
| 115 if __name__ == '__main__': | 136 if __name__ == '__main__': |
| 116 sys.exit(main()) | 137 sys.exit(main()) |
| OLD | NEW |