Harrison Mutai | 4126dc7 | 2021-11-23 11:34:41 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2022, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | import argparse |
| 9 | import csv |
| 10 | import os |
| 11 | |
| 12 | from gen_test_report import emit_header, print_error_message |
| 13 | |
| 14 | TABLE_HEADER = """\ |
| 15 | <table id="tf-results-panel"> |
| 16 | <tbody> |
| 17 | <tr> |
| 18 | <td class="results-col"> |
| 19 | <table> |
| 20 | <tbody> |
| 21 | <thead> |
| 22 | <tr> |
| 23 | <th>Passed</th> |
| 24 | <th>Skipped</th> |
| 25 | <th>Crashed</th> |
| 26 | <th>Failed</th> |
| 27 | </tr> |
| 28 | </thead> |
| 29 | <tbody> |
| 30 | <tr>""" |
| 31 | |
| 32 | TABLE_FOOTER = """\ |
| 33 | </tr> |
| 34 | </tbody> |
| 35 | </tbody> |
| 36 | </table> |
| 37 | </td> |
| 38 | <td class="button-col"> |
| 39 | <button id="tf-download-button" onclick="window.open('{}','_blank')">Download Plot</button> |
| 40 | </td> |
| 41 | </tr> |
| 42 | </tbody> |
| 43 | </table>""" |
| 44 | |
| 45 | # Open and sum the results of a comma-separated test result file. |
| 46 | def fetch_results(csv_path): |
| 47 | failed = passed = skipped = crashed = 0 |
| 48 | with open(csv_path, "r") as fd: |
| 49 | for test in csv.DictReader(fd): |
| 50 | failed = failed + int(test["Failed"]) |
| 51 | passed = passed + int(test["Passed"]) |
| 52 | skipped = skipped + int(test["Skipped"]) |
| 53 | crashed = crashed + int(test["Crashed"]) |
| 54 | return passed, skipped, crashed, failed |
| 55 | |
| 56 | |
| 57 | def main(fd, csv_path, png_path): |
| 58 | results_row = ("""<td>{}</td>\n""".rjust(28) * 4).format( |
| 59 | *fetch_results(csv_path) |
| 60 | ) |
| 61 | |
| 62 | # Emite header and style sheet. |
| 63 | emit_header(fd) |
| 64 | |
| 65 | print(TABLE_HEADER, file=fd) |
| 66 | print(results_row, file=fd) |
| 67 | |
| 68 | # Format table button to link to full-size plot of results. |
| 69 | TABLE_FOOTER.format(build_url + "/" + png_path) |
| 70 | print(TABLE_FOOTER, file=fd) |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | global build_url |
| 75 | |
| 76 | parser = argparse.ArgumentParser() |
| 77 | |
| 78 | # Add arguments |
| 79 | parser.add_argument( |
| 80 | "--output", |
| 81 | "-o", |
| 82 | default="report.html", |
| 83 | help="Path to output file", |
| 84 | ) |
| 85 | parser.add_argument( |
| 86 | "--csv", |
| 87 | default=None, |
| 88 | help="Path to input CSV with data for the target job", |
| 89 | ) |
| 90 | parser.add_argument( |
| 91 | "--png", default=None, help="Filename for PNG results plot" |
| 92 | ) |
| 93 | |
| 94 | # The build url and target for the results are needed in-case the user hasn't provided a |
| 95 | # paths to the inputs. |
| 96 | opts = parser.parse_args() |
| 97 | build_url = os.environ["BUILD_URL"] |
| 98 | target_job = os.environ["TARGET_BUILD"] |
| 99 | target_job_name = target_job[: target_job.find("/")] |
| 100 | |
| 101 | # Use filenames provided by user or try and infer based off the target name from the environment. |
| 102 | output_path = "report.html" if not opts.output else opts.output |
| 103 | csv_path = target_job_name + "results.csv" if not opts.csv else opts.csv |
| 104 | png_path = target_job_name + "results.png" if not opts.png else opts.png |
| 105 | |
| 106 | with open(output_path, "w") as fd: |
| 107 | try: |
| 108 | main(output_path, csv_path, png_path) |
| 109 | except: |
| 110 | print_error_message(fd) |
| 111 | raise |