|
| 1 | +# |
| 2 | +# ---------------------------------------------------------------------------------------------------- |
| 3 | +# |
| 4 | +# Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved. |
| 5 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 6 | +# |
| 7 | +# This code is free software; you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU General Public License version 2 only, as |
| 9 | +# published by the Free Software Foundation. |
| 10 | +# |
| 11 | +# This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | +# version 2 for more details (a copy is included in the LICENSE file that |
| 15 | +# accompanied this code). |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License version |
| 18 | +# 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +# |
| 21 | +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | +# or visit www.oracle.com if you need additional information or have any |
| 23 | +# questions. |
| 24 | +# |
| 25 | +# ---------------------------------------------------------------------------------------------------- |
| 26 | +# |
| 27 | + |
| 28 | +import html |
| 29 | +import os |
| 30 | +import time |
| 31 | + |
| 32 | +from .. import mx |
| 33 | + |
| 34 | +def write_task_report(f, task): |
| 35 | + f.write('\n<div class="task">\n') |
| 36 | + f.write(f' <h2>{task.name}</h2>\n') |
| 37 | + f.write(f' <p class="result {task.status}">{task.statusInfo}</p>\n') |
| 38 | + l = str(task._log).strip() |
| 39 | + if l: |
| 40 | + f.write(' <span class="log"><pre>\n') |
| 41 | + f.write(html.escape(l)) |
| 42 | + f.write('\n </pre></span>\n') |
| 43 | + f.write('</div>\n') |
| 44 | + |
| 45 | +def write_style(f): |
| 46 | + f.write(''' |
| 47 | + <style> |
| 48 | + .header dt { font-weight: bold; } |
| 49 | + .success:before { content: "success"; color: green; } |
| 50 | + .failed:before { content: "failed"; color: red; } |
| 51 | + .skipped:before { content: "skipped"; margin-right: 2em; color: blue; } |
| 52 | + .log:before { content: "build log:"; } |
| 53 | + .log pre { border: 1px inset; padding: 5px; max-height: 350px; overflow: auto; } |
| 54 | + </style> |
| 55 | + ''') |
| 56 | + |
| 57 | +class BuildReport: |
| 58 | + def __init__(self, cmd_args): |
| 59 | + self.tasks = [] |
| 60 | + self.properties = {} |
| 61 | + if cmd_args: |
| 62 | + self.properties['arguments'] = " ".join(cmd_args) |
| 63 | + |
| 64 | + def set_tasks(self, tasks): |
| 65 | + self.tasks = tasks |
| 66 | + |
| 67 | + def add_info(self, key, value): |
| 68 | + self.properties[key] = value |
| 69 | + |
| 70 | + def _write_header(self, f): |
| 71 | + f.write('<h1>mx build report</h1>\n<dl class="header">\n') |
| 72 | + for k in self.properties: |
| 73 | + f.write(f" <dt>{k}</dt>\n") |
| 74 | + v = self.properties[k] |
| 75 | + if isinstance(v, list): |
| 76 | + f.write(" <dd>\n") |
| 77 | + for i in v: |
| 78 | + f.write(f" {html.escape(str(i))}<br/>\n") |
| 79 | + f.write(" </dd>\n") |
| 80 | + else: |
| 81 | + f.write(f" <dd>{html.escape(str(v))}</dd>\n") |
| 82 | + f.write('</dl>\n') |
| 83 | + |
| 84 | + def _write_report(self, filename): |
| 85 | + allSkipped = True |
| 86 | + for t in self.tasks: |
| 87 | + if t.status != "skipped": |
| 88 | + allSkipped = False |
| 89 | + break |
| 90 | + if allSkipped: |
| 91 | + # don't bother writing a build log if there was nothing to do |
| 92 | + return |
| 93 | + with open(filename, 'w', encoding='utf-8') as f: |
| 94 | + f.write('<!DOCTYPE html>\n') |
| 95 | + f.write('<html>\n') |
| 96 | + f.write('<body>\n') |
| 97 | + self._write_header(f) |
| 98 | + for t in self.tasks: |
| 99 | + if t.status is not None: |
| 100 | + # only report on tasks that were started |
| 101 | + write_task_report(f, t) |
| 102 | + write_style(f) |
| 103 | + f.write('</body>\n') |
| 104 | + f.write('</html>\n') |
| 105 | + mx.log(f"mx build log written to {filename}") |
| 106 | + |
| 107 | + def __enter__(self): |
| 108 | + self.properties['started'] = time.strftime("%Y-%m-%d %H:%M:%S") |
| 109 | + return self |
| 110 | + |
| 111 | + def __exit__(self, exc_type, exc_value, traceback): |
| 112 | + self.properties['finished'] = time.strftime("%Y-%m-%d %H:%M:%S") |
| 113 | + |
| 114 | + reportDir = mx.primary_suite().get_output_root(jdkDependent=False) |
| 115 | + mx.ensure_dir_exists(reportDir) |
| 116 | + base_name = time.strftime("buildlog-%Y%m%d-%H%M%S") |
| 117 | + reportFile = os.path.join(reportDir, base_name + ".html") |
| 118 | + reportIdx = 0 |
| 119 | + while os.path.exists(reportFile): |
| 120 | + reportIdx += 1 |
| 121 | + reportFile = os.path.join(reportDir, f'{base_name}_{reportIdx}.html') |
| 122 | + self._write_report(reportFile) |
0 commit comments