Chih-Hung Hsieh | 3cce2bc | 2020-02-27 15:39:18 -0800 | [diff] [blame] | 1 | # Lint as: python3 |
| 2 | # Copyright (C) 2019 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """Emit warning messages to html or csv files.""" |
| 17 | |
| 18 | # To emit html page of warning messages: |
| 19 | # flags: --byproject, --url, --separator |
| 20 | # Old stuff for static html components: |
| 21 | # html_script_style: static html scripts and styles |
| 22 | # htmlbig: |
| 23 | # dump_stats, dump_html_prologue, dump_html_epilogue: |
| 24 | # emit_buttons: |
| 25 | # dump_fixed |
| 26 | # sort_warnings: |
| 27 | # emit_stats_by_project: |
| 28 | # all_patterns, |
| 29 | # findproject, classify_warning |
| 30 | # dump_html |
| 31 | # |
| 32 | # New dynamic HTML page's static JavaScript data: |
| 33 | # Some data are copied from Python to JavaScript, to generate HTML elements. |
| 34 | # FlagPlatform flags.platform |
| 35 | # FlagURL flags.url, used by 'android' |
| 36 | # FlagSeparator flags.separator, used by 'android' |
| 37 | # SeverityColors: list of colors for all severity levels |
| 38 | # SeverityHeaders: list of headers for all severity levels |
| 39 | # SeverityColumnHeaders: list of column_headers for all severity levels |
| 40 | # ProjectNames: project_names, or project_list[*][0] |
| 41 | # WarnPatternsSeverity: warn_patterns[*]['severity'] |
| 42 | # WarnPatternsDescription: warn_patterns[*]['description'] |
| 43 | # WarningMessages: warning_messages |
| 44 | # Warnings: warning_records |
| 45 | # StatsHeader: warning count table header row |
| 46 | # StatsRows: array of warning count table rows |
| 47 | # |
| 48 | # New dynamic HTML page's dynamic JavaScript data: |
| 49 | # |
| 50 | # New dynamic HTML related function to emit data: |
| 51 | # escape_string, strip_escape_string, emit_warning_arrays |
| 52 | # emit_js_data(): |
| 53 | |
| 54 | from __future__ import print_function |
| 55 | import cgi |
| 56 | import csv |
| 57 | import sys |
| 58 | |
| 59 | # pylint:disable=relative-beyond-top-level |
| 60 | # pylint:disable=g-importing-member |
| 61 | from .severity import Severity |
| 62 | |
| 63 | |
| 64 | html_head_scripts = """\ |
| 65 | <script type="text/javascript"> |
| 66 | function expand(id) { |
| 67 | var e = document.getElementById(id); |
| 68 | var f = document.getElementById(id + "_mark"); |
| 69 | if (e.style.display == 'block') { |
| 70 | e.style.display = 'none'; |
| 71 | f.innerHTML = '⊕'; |
| 72 | } |
| 73 | else { |
| 74 | e.style.display = 'block'; |
| 75 | f.innerHTML = '⊖'; |
| 76 | } |
| 77 | }; |
| 78 | function expandCollapse(show) { |
| 79 | for (var id = 1; ; id++) { |
| 80 | var e = document.getElementById(id + ""); |
| 81 | var f = document.getElementById(id + "_mark"); |
| 82 | if (!e || !f) break; |
| 83 | e.style.display = (show ? 'block' : 'none'); |
| 84 | f.innerHTML = (show ? '⊖' : '⊕'); |
| 85 | } |
| 86 | }; |
| 87 | </script> |
| 88 | <style type="text/css"> |
| 89 | th,td{border-collapse:collapse; border:1px solid black;} |
| 90 | .button{color:blue;font-size:110%;font-weight:bolder;} |
| 91 | .bt{color:black;background-color:transparent;border:none;outline:none; |
| 92 | font-size:140%;font-weight:bolder;} |
| 93 | .c0{background-color:#e0e0e0;} |
| 94 | .c1{background-color:#d0d0d0;} |
| 95 | .t1{border-collapse:collapse; width:100%; border:1px solid black;} |
| 96 | </style> |
| 97 | <script src="https://www.gstatic.com/charts/loader.js"></script> |
| 98 | """ |
| 99 | |
| 100 | |
| 101 | def make_writer(output_stream): |
| 102 | |
| 103 | def writer(text): |
| 104 | return output_stream.write(text + '\n') |
| 105 | |
| 106 | return writer |
| 107 | |
| 108 | |
| 109 | def html_big(param): |
| 110 | return '<font size="+2">' + param + '</font>' |
| 111 | |
| 112 | |
| 113 | def dump_html_prologue(title, writer, warn_patterns, project_names): |
| 114 | writer('<html>\n<head>') |
| 115 | writer('<title>' + title + '</title>') |
| 116 | writer(html_head_scripts) |
| 117 | emit_stats_by_project(writer, warn_patterns, project_names) |
| 118 | writer('</head>\n<body>') |
| 119 | writer(html_big(title)) |
| 120 | writer('<p>') |
| 121 | |
| 122 | |
| 123 | def dump_html_epilogue(writer): |
| 124 | writer('</body>\n</head>\n</html>') |
| 125 | |
| 126 | |
| 127 | def sort_warnings(warn_patterns): |
| 128 | for i in warn_patterns: |
| 129 | i['members'] = sorted(set(i['members'])) |
| 130 | |
| 131 | |
| 132 | def create_warnings(warn_patterns, project_names): |
| 133 | """Creates warnings s.t. |
| 134 | |
| 135 | warnings[p][s] is as specified in above docs. |
| 136 | |
| 137 | Args: |
| 138 | warn_patterns: list of warning patterns for specified platform |
| 139 | project_names: list of project names |
| 140 | |
| 141 | Returns: |
| 142 | 2D warnings array where warnings[p][s] is # of warnings in project name p of |
| 143 | severity level s |
| 144 | """ |
| 145 | # pylint:disable=g-complex-comprehension |
| 146 | warnings = {p: {s.value: 0 for s in Severity.levels} for p in project_names} |
| 147 | for i in warn_patterns: |
| 148 | s = i['severity'].value |
| 149 | for p in i['projects']: |
| 150 | warnings[p][s] += i['projects'][p] |
| 151 | return warnings |
| 152 | |
| 153 | |
| 154 | def get_total_by_project(warnings, project_names): |
| 155 | """Returns dict, project as key and # warnings for that project as value.""" |
| 156 | # pylint:disable=g-complex-comprehension |
| 157 | return { |
| 158 | p: sum(warnings[p][s.value] for s in Severity.levels) |
| 159 | for p in project_names |
| 160 | } |
| 161 | |
| 162 | |
| 163 | def get_total_by_severity(warnings, project_names): |
| 164 | """Returns dict, severity as key and # warnings of that severity as value.""" |
| 165 | # pylint:disable=g-complex-comprehension |
| 166 | return { |
| 167 | s.value: sum(warnings[p][s.value] for p in project_names) |
| 168 | for s in Severity.levels |
| 169 | } |
| 170 | |
| 171 | |
| 172 | def emit_table_header(total_by_severity): |
| 173 | """Returns list of HTML-formatted content for severity stats.""" |
| 174 | |
| 175 | stats_header = ['Project'] |
| 176 | for s in Severity.levels: |
| 177 | if total_by_severity[s.value]: |
| 178 | stats_header.append( |
| 179 | '<span style=\'background-color:{}\'>{}</span>'.format( |
| 180 | s.color, s.column_header)) |
| 181 | stats_header.append('TOTAL') |
| 182 | return stats_header |
| 183 | |
| 184 | |
| 185 | def emit_row_counts_per_project(warnings, total_by_project, total_by_severity, |
| 186 | project_names): |
| 187 | """Returns total project warnings and row of stats for each project. |
| 188 | |
| 189 | Args: |
| 190 | warnings: output of create_warnings(warn_patterns, project_names) |
| 191 | total_by_project: output of get_total_by_project(project_names) |
| 192 | total_by_severity: output of get_total_by_severity(project_names) |
| 193 | project_names: list of project names |
| 194 | |
| 195 | Returns: |
| 196 | total_all_projects, the total number of warnings over all projects |
| 197 | stats_rows, a 2d list where each row is [Project Name, <severity counts>, |
| 198 | total # warnings for this project] |
| 199 | """ |
| 200 | |
| 201 | total_all_projects = 0 |
| 202 | stats_rows = [] |
| 203 | for p in project_names: |
| 204 | if total_by_project[p]: |
| 205 | one_row = [p] |
| 206 | for s in Severity.levels: |
| 207 | if total_by_severity[s.value]: |
| 208 | one_row.append(warnings[p][s.value]) |
| 209 | one_row.append(total_by_project[p]) |
| 210 | stats_rows.append(one_row) |
| 211 | total_all_projects += total_by_project[p] |
| 212 | return total_all_projects, stats_rows |
| 213 | |
| 214 | |
| 215 | def emit_row_counts_per_severity(total_by_severity, stats_header, stats_rows, |
| 216 | total_all_projects, writer): |
| 217 | """Emits stats_header and stats_rows as specified above. |
| 218 | |
| 219 | Args: |
| 220 | total_by_severity: output of get_total_by_severity() |
| 221 | stats_header: output of emit_table_header() |
| 222 | stats_rows: output of emit_row_counts_per_project() |
| 223 | total_all_projects: output of emit_row_counts_per_project() |
| 224 | writer: writer returned by make_writer(output_stream) |
| 225 | """ |
| 226 | |
| 227 | total_all_severities = 0 |
| 228 | one_row = ['<b>TOTAL</b>'] |
| 229 | for s in Severity.levels: |
| 230 | if total_by_severity[s.value]: |
| 231 | one_row.append(total_by_severity[s.value]) |
| 232 | total_all_severities += total_by_severity[s.value] |
| 233 | one_row.append(total_all_projects) |
| 234 | stats_rows.append(one_row) |
| 235 | writer('<script>') |
| 236 | emit_const_string_array('StatsHeader', stats_header, writer) |
| 237 | emit_const_object_array('StatsRows', stats_rows, writer) |
| 238 | writer(draw_table_javascript) |
| 239 | writer('</script>') |
| 240 | |
| 241 | |
| 242 | def emit_stats_by_project(writer, warn_patterns, project_names): |
| 243 | """Dump a google chart table of warnings per project and severity.""" |
| 244 | |
| 245 | warnings = create_warnings(warn_patterns, project_names) |
| 246 | total_by_project = get_total_by_project(warnings, project_names) |
| 247 | total_by_severity = get_total_by_severity(warnings, project_names) |
| 248 | stats_header = emit_table_header(total_by_severity) |
| 249 | total_all_projects, stats_rows = \ |
| 250 | emit_row_counts_per_project(warnings, total_by_project, total_by_severity, project_names) |
| 251 | emit_row_counts_per_severity(total_by_severity, stats_header, stats_rows, |
| 252 | total_all_projects, writer) |
| 253 | |
| 254 | |
| 255 | def dump_stats(writer, warn_patterns): |
| 256 | """Dump some stats about total number of warnings and such.""" |
| 257 | |
| 258 | known = 0 |
| 259 | skipped = 0 |
| 260 | unknown = 0 |
| 261 | sort_warnings(warn_patterns) |
| 262 | for i in warn_patterns: |
| 263 | if i['severity'] == Severity.UNMATCHED: |
| 264 | unknown += len(i['members']) |
| 265 | elif i['severity'] == Severity.SKIP: |
| 266 | skipped += len(i['members']) |
| 267 | else: |
| 268 | known += len(i['members']) |
| 269 | writer('Number of classified warnings: <b>' + str(known) + '</b><br>') |
| 270 | writer('Number of skipped warnings: <b>' + str(skipped) + '</b><br>') |
| 271 | writer('Number of unclassified warnings: <b>' + str(unknown) + '</b><br>') |
| 272 | total = unknown + known + skipped |
| 273 | extra_msg = '' |
| 274 | if total < 1000: |
| 275 | extra_msg = ' (low count may indicate incremental build)' |
| 276 | writer('Total number of warnings: <b>' + str(total) + '</b>' + extra_msg) |
| 277 | |
| 278 | |
| 279 | # New base table of warnings, [severity, warn_id, project, warning_message] |
| 280 | # Need buttons to show warnings in different grouping options. |
| 281 | # (1) Current, group by severity, id for each warning pattern |
| 282 | # sort by severity, warn_id, warning_message |
| 283 | # (2) Current --byproject, group by severity, |
| 284 | # id for each warning pattern + project name |
| 285 | # sort by severity, warn_id, project, warning_message |
| 286 | # (3) New, group by project + severity, |
| 287 | # id for each warning pattern |
| 288 | # sort by project, severity, warn_id, warning_message |
| 289 | def emit_buttons(writer): |
| 290 | writer('<button class="button" onclick="expandCollapse(1);">' |
| 291 | 'Expand all warnings</button>\n' |
| 292 | '<button class="button" onclick="expandCollapse(0);">' |
| 293 | 'Collapse all warnings</button>\n' |
| 294 | '<button class="button" onclick="groupBySeverity();">' |
| 295 | 'Group warnings by severity</button>\n' |
| 296 | '<button class="button" onclick="groupByProject();">' |
| 297 | 'Group warnings by project</button><br>') |
| 298 | |
| 299 | |
| 300 | def all_patterns(category): |
| 301 | patterns = '' |
| 302 | for i in category['patterns']: |
| 303 | patterns += i |
| 304 | patterns += ' / ' |
| 305 | return patterns |
| 306 | |
| 307 | |
| 308 | def dump_fixed(writer, warn_patterns): |
| 309 | """Show which warnings no longer occur.""" |
| 310 | anchor = 'fixed_warnings' |
| 311 | mark = anchor + '_mark' |
| 312 | writer('\n<br><p style="background-color:lightblue"><b>' |
| 313 | '<button id="' + mark + '" ' |
| 314 | 'class="bt" onclick="expand(\'' + anchor + '\');">' |
| 315 | '⊕</button> Fixed warnings. ' |
| 316 | 'No more occurrences. Please consider turning these into ' |
| 317 | 'errors if possible, before they are reintroduced in to the build' |
| 318 | ':</b></p>') |
| 319 | writer('<blockquote>') |
| 320 | fixed_patterns = [] |
| 321 | for i in warn_patterns: |
| 322 | if not i['members']: |
| 323 | fixed_patterns.append(i['description'] + ' (' + all_patterns(i) + ')') |
| 324 | fixed_patterns = sorted(fixed_patterns) |
| 325 | writer('<div id="' + anchor + '" style="display:none;"><table>') |
| 326 | cur_row_class = 0 |
| 327 | for text in fixed_patterns: |
| 328 | cur_row_class = 1 - cur_row_class |
| 329 | # remove last '\n' |
| 330 | t = text[:-1] if text[-1] == '\n' else text |
| 331 | writer('<tr><td class="c' + str(cur_row_class) + '">' + t + '</td></tr>') |
| 332 | writer('</table></div>') |
| 333 | writer('</blockquote>') |
| 334 | |
| 335 | |
| 336 | def write_severity(csvwriter, sev, kind, warn_patterns): |
| 337 | """Count warnings of given severity and write CSV entries to writer.""" |
| 338 | total = 0 |
| 339 | for pattern in warn_patterns: |
| 340 | if pattern['severity'] == sev and pattern['members']: |
| 341 | n = len(pattern['members']) |
| 342 | total += n |
| 343 | warning = kind + ': ' + (pattern['description'] or '?') |
| 344 | csvwriter.writerow([n, '', warning]) |
| 345 | # print number of warnings for each project, ordered by project name |
| 346 | projects = sorted(pattern['projects'].keys()) |
| 347 | for project in projects: |
| 348 | csvwriter.writerow([pattern['projects'][project], project, warning]) |
| 349 | csvwriter.writerow([total, '', kind + ' warnings']) |
| 350 | return total |
| 351 | |
| 352 | |
| 353 | def dump_csv(csvwriter, warn_patterns): |
| 354 | """Dump number of warnings in CSV format to writer.""" |
| 355 | sort_warnings(warn_patterns) |
| 356 | total = 0 |
| 357 | for s in Severity.levels: |
| 358 | total += write_severity(csvwriter, s, s.column_header, warn_patterns) |
| 359 | csvwriter.writerow([total, '', 'All warnings']) |
| 360 | |
| 361 | |
Saeid Farivar Asanjan | 75dc8d2 | 2020-11-18 00:29:43 +0000 | [diff] [blame] | 362 | def dump_csv_with_description(csvwriter, warning_records, warning_messages, |
| 363 | warn_patterns, project_names): |
| 364 | """Outputs all the warning messages by project.""" |
| 365 | csv_output = [] |
| 366 | for record in warning_records: |
| 367 | project_name = project_names[record[1]] |
| 368 | pattern = warn_patterns[record[0]] |
| 369 | severity = pattern['severity'].header |
| 370 | category = pattern['category'] |
| 371 | description = pattern['description'] |
| 372 | warning = warning_messages[record[2]] |
| 373 | csv_output.append([project_name, severity, |
| 374 | category, description, |
| 375 | warning]) |
| 376 | csv_output = sorted(csv_output) |
| 377 | for output in csv_output: |
| 378 | csvwriter.writerow(output) |
| 379 | |
| 380 | |
Chih-Hung Hsieh | 3cce2bc | 2020-02-27 15:39:18 -0800 | [diff] [blame] | 381 | # Return s with escaped backslash and quotation characters. |
| 382 | def escape_string(s): |
| 383 | return s.replace('\\', '\\\\').replace('"', '\\"') |
| 384 | |
| 385 | |
| 386 | # Return s without trailing '\n' and escape the quotation characters. |
| 387 | def strip_escape_string(s): |
| 388 | if not s: |
| 389 | return s |
| 390 | s = s[:-1] if s[-1] == '\n' else s |
| 391 | return escape_string(s) |
| 392 | |
| 393 | |
| 394 | def emit_warning_array(name, writer, warn_patterns): |
| 395 | writer('var warning_{} = ['.format(name)) |
| 396 | for w in warn_patterns: |
| 397 | if name == 'severity': |
| 398 | writer('{},'.format(w[name].value)) |
| 399 | else: |
| 400 | writer('{},'.format(w[name])) |
| 401 | writer('];') |
| 402 | |
| 403 | |
| 404 | def emit_warning_arrays(writer, warn_patterns): |
| 405 | emit_warning_array('severity', writer, warn_patterns) |
| 406 | writer('var warning_description = [') |
| 407 | for w in warn_patterns: |
| 408 | if w['members']: |
| 409 | writer('"{}",'.format(escape_string(w['description']))) |
| 410 | else: |
| 411 | writer('"",') # no such warning |
| 412 | writer('];') |
| 413 | |
| 414 | |
| 415 | scripts_for_warning_groups = """ |
| 416 | function compareMessages(x1, x2) { // of the same warning type |
| 417 | return (WarningMessages[x1[2]] <= WarningMessages[x2[2]]) ? -1 : 1; |
| 418 | } |
| 419 | function byMessageCount(x1, x2) { |
| 420 | return x2[2] - x1[2]; // reversed order |
| 421 | } |
| 422 | function bySeverityMessageCount(x1, x2) { |
| 423 | // orer by severity first |
| 424 | if (x1[1] != x2[1]) |
| 425 | return x1[1] - x2[1]; |
| 426 | return byMessageCount(x1, x2); |
| 427 | } |
| 428 | const ParseLinePattern = /^([^ :]+):(\\d+):(.+)/; |
| 429 | function addURL(line) { // used by Android |
| 430 | if (FlagURL == "") return line; |
| 431 | if (FlagSeparator == "") { |
| 432 | return line.replace(ParseLinePattern, |
| 433 | "<a target='_blank' href='" + FlagURL + "/$1'>$1</a>:$2:$3"); |
| 434 | } |
| 435 | return line.replace(ParseLinePattern, |
| 436 | "<a target='_blank' href='" + FlagURL + "/$1" + FlagSeparator + |
| 437 | "$2'>$1:$2</a>:$3"); |
| 438 | } |
| 439 | function addURLToLine(line, link) { // used by Chrome |
| 440 | let line_split = line.split(":"); |
| 441 | let path = line_split.slice(0,3).join(":"); |
| 442 | let msg = line_split.slice(3).join(":"); |
| 443 | let html_link = `<a target="_blank" href="${link}">${path}</a>${msg}`; |
| 444 | return html_link; |
| 445 | } |
| 446 | function createArrayOfDictionaries(n) { |
| 447 | var result = []; |
| 448 | for (var i=0; i<n; i++) result.push({}); |
| 449 | return result; |
| 450 | } |
| 451 | function groupWarningsBySeverity() { |
| 452 | // groups is an array of dictionaries, |
| 453 | // each dictionary maps from warning type to array of warning messages. |
| 454 | var groups = createArrayOfDictionaries(SeverityColors.length); |
| 455 | for (var i=0; i<Warnings.length; i++) { |
| 456 | var w = Warnings[i][0]; |
| 457 | var s = WarnPatternsSeverity[w]; |
| 458 | var k = w.toString(); |
| 459 | if (!(k in groups[s])) |
| 460 | groups[s][k] = []; |
| 461 | groups[s][k].push(Warnings[i]); |
| 462 | } |
| 463 | return groups; |
| 464 | } |
| 465 | function groupWarningsByProject() { |
| 466 | var groups = createArrayOfDictionaries(ProjectNames.length); |
| 467 | for (var i=0; i<Warnings.length; i++) { |
| 468 | var w = Warnings[i][0]; |
| 469 | var p = Warnings[i][1]; |
| 470 | var k = w.toString(); |
| 471 | if (!(k in groups[p])) |
| 472 | groups[p][k] = []; |
| 473 | groups[p][k].push(Warnings[i]); |
| 474 | } |
| 475 | return groups; |
| 476 | } |
| 477 | var GlobalAnchor = 0; |
| 478 | function createWarningSection(header, color, group) { |
| 479 | var result = ""; |
| 480 | var groupKeys = []; |
| 481 | var totalMessages = 0; |
| 482 | for (var k in group) { |
| 483 | totalMessages += group[k].length; |
| 484 | groupKeys.push([k, WarnPatternsSeverity[parseInt(k)], group[k].length]); |
| 485 | } |
| 486 | groupKeys.sort(bySeverityMessageCount); |
| 487 | for (var idx=0; idx<groupKeys.length; idx++) { |
| 488 | var k = groupKeys[idx][0]; |
| 489 | var messages = group[k]; |
| 490 | var w = parseInt(k); |
| 491 | var wcolor = SeverityColors[WarnPatternsSeverity[w]]; |
| 492 | var description = WarnPatternsDescription[w]; |
| 493 | if (description.length == 0) |
| 494 | description = "???"; |
| 495 | GlobalAnchor += 1; |
| 496 | result += "<table class='t1'><tr bgcolor='" + wcolor + "'><td>" + |
| 497 | "<button class='bt' id='" + GlobalAnchor + "_mark" + |
| 498 | "' onclick='expand(\\"" + GlobalAnchor + "\\");'>" + |
| 499 | "⊕</button> " + |
| 500 | description + " (" + messages.length + ")</td></tr></table>"; |
| 501 | result += "<div id='" + GlobalAnchor + |
| 502 | "' style='display:none;'><table class='t1'>"; |
| 503 | var c = 0; |
| 504 | messages.sort(compareMessages); |
| 505 | if (FlagPlatform == "chrome") { |
| 506 | for (var i=0; i<messages.length; i++) { |
| 507 | result += "<tr><td class='c" + c + "'>" + |
| 508 | addURLToLine(WarningMessages[messages[i][2]], WarningLinks[messages[i][3]]) + "</td></tr>"; |
| 509 | c = 1 - c; |
| 510 | } |
| 511 | } else { |
| 512 | for (var i=0; i<messages.length; i++) { |
| 513 | result += "<tr><td class='c" + c + "'>" + |
| 514 | addURL(WarningMessages[messages[i][2]]) + "</td></tr>"; |
| 515 | c = 1 - c; |
| 516 | } |
| 517 | } |
| 518 | result += "</table></div>"; |
| 519 | } |
| 520 | if (result.length > 0) { |
| 521 | return "<br><span style='background-color:" + color + "'><b>" + |
| 522 | header + ": " + totalMessages + |
| 523 | "</b></span><blockquote><table class='t1'>" + |
| 524 | result + "</table></blockquote>"; |
| 525 | |
| 526 | } |
| 527 | return ""; // empty section |
| 528 | } |
| 529 | function generateSectionsBySeverity() { |
| 530 | var result = ""; |
| 531 | var groups = groupWarningsBySeverity(); |
| 532 | for (s=0; s<SeverityColors.length; s++) { |
| 533 | result += createWarningSection(SeverityHeaders[s], SeverityColors[s], |
| 534 | groups[s]); |
| 535 | } |
| 536 | return result; |
| 537 | } |
| 538 | function generateSectionsByProject() { |
| 539 | var result = ""; |
| 540 | var groups = groupWarningsByProject(); |
| 541 | for (i=0; i<groups.length; i++) { |
| 542 | result += createWarningSection(ProjectNames[i], 'lightgrey', groups[i]); |
| 543 | } |
| 544 | return result; |
| 545 | } |
| 546 | function groupWarnings(generator) { |
| 547 | GlobalAnchor = 0; |
| 548 | var e = document.getElementById("warning_groups"); |
| 549 | e.innerHTML = generator(); |
| 550 | } |
| 551 | function groupBySeverity() { |
| 552 | groupWarnings(generateSectionsBySeverity); |
| 553 | } |
| 554 | function groupByProject() { |
| 555 | groupWarnings(generateSectionsByProject); |
| 556 | } |
| 557 | """ |
| 558 | |
| 559 | |
| 560 | # Emit a JavaScript const string |
| 561 | def emit_const_string(name, value, writer): |
| 562 | writer('const ' + name + ' = "' + escape_string(value) + '";') |
| 563 | |
| 564 | |
| 565 | # Emit a JavaScript const integer array. |
| 566 | def emit_const_int_array(name, array, writer): |
| 567 | writer('const ' + name + ' = [') |
| 568 | for n in array: |
| 569 | writer(str(n) + ',') |
| 570 | writer('];') |
| 571 | |
| 572 | |
| 573 | # Emit a JavaScript const string array. |
| 574 | def emit_const_string_array(name, array, writer): |
| 575 | writer('const ' + name + ' = [') |
| 576 | for s in array: |
| 577 | writer('"' + strip_escape_string(s) + '",') |
| 578 | writer('];') |
| 579 | |
| 580 | |
| 581 | # Emit a JavaScript const string array for HTML. |
| 582 | def emit_const_html_string_array(name, array, writer): |
| 583 | writer('const ' + name + ' = [') |
| 584 | for s in array: |
| 585 | # Not using html.escape yet, to work for both python 2 and 3, |
| 586 | # until all users switch to python 3. |
| 587 | # pylint:disable=deprecated-method |
| 588 | writer('"' + cgi.escape(strip_escape_string(s)) + '",') |
| 589 | writer('];') |
| 590 | |
| 591 | |
| 592 | # Emit a JavaScript const object array. |
| 593 | def emit_const_object_array(name, array, writer): |
| 594 | writer('const ' + name + ' = [') |
| 595 | for x in array: |
| 596 | writer(str(x) + ',') |
| 597 | writer('];') |
| 598 | |
| 599 | |
| 600 | def emit_js_data(writer, flags, warning_messages, warning_links, |
| 601 | warning_records, warn_patterns, project_names): |
| 602 | """Dump dynamic HTML page's static JavaScript data.""" |
| 603 | emit_const_string('FlagPlatform', flags.platform, writer) |
| 604 | emit_const_string('FlagURL', flags.url, writer) |
| 605 | emit_const_string('FlagSeparator', flags.separator, writer) |
| 606 | emit_const_string_array('SeverityColors', [s.color for s in Severity.levels], |
| 607 | writer) |
| 608 | emit_const_string_array('SeverityHeaders', |
| 609 | [s.header for s in Severity.levels], writer) |
| 610 | emit_const_string_array('SeverityColumnHeaders', |
| 611 | [s.column_header for s in Severity.levels], writer) |
| 612 | emit_const_string_array('ProjectNames', project_names, writer) |
| 613 | # pytype: disable=attribute-error |
| 614 | emit_const_int_array('WarnPatternsSeverity', |
| 615 | [w['severity'].value for w in warn_patterns], writer) |
| 616 | # pytype: enable=attribute-error |
| 617 | emit_const_html_string_array('WarnPatternsDescription', |
| 618 | [w['description'] for w in warn_patterns], |
| 619 | writer) |
| 620 | emit_const_html_string_array('WarningMessages', warning_messages, writer) |
| 621 | emit_const_object_array('Warnings', warning_records, writer) |
| 622 | if flags.platform == 'chrome': |
| 623 | emit_const_html_string_array('WarningLinks', warning_links, writer) |
| 624 | |
| 625 | |
| 626 | draw_table_javascript = """ |
| 627 | google.charts.load('current', {'packages':['table']}); |
| 628 | google.charts.setOnLoadCallback(drawTable); |
| 629 | function drawTable() { |
| 630 | var data = new google.visualization.DataTable(); |
| 631 | data.addColumn('string', StatsHeader[0]); |
| 632 | for (var i=1; i<StatsHeader.length; i++) { |
| 633 | data.addColumn('number', StatsHeader[i]); |
| 634 | } |
| 635 | data.addRows(StatsRows); |
| 636 | for (var i=0; i<StatsRows.length; i++) { |
| 637 | for (var j=0; j<StatsHeader.length; j++) { |
| 638 | data.setProperty(i, j, 'style', 'border:1px solid black;'); |
| 639 | } |
| 640 | } |
| 641 | var table = new google.visualization.Table( |
| 642 | document.getElementById('stats_table')); |
| 643 | table.draw(data, {allowHtml: true, alternatingRowStyle: true}); |
| 644 | } |
| 645 | """ |
| 646 | |
| 647 | |
| 648 | def dump_html(flags, output_stream, warning_messages, warning_links, |
| 649 | warning_records, header_str, warn_patterns, project_names): |
| 650 | """Dump the flags output to output_stream.""" |
| 651 | writer = make_writer(output_stream) |
| 652 | dump_html_prologue('Warnings for ' + header_str, writer, warn_patterns, |
| 653 | project_names) |
| 654 | dump_stats(writer, warn_patterns) |
| 655 | writer('<br><div id="stats_table"></div><br>') |
| 656 | writer('\n<script>') |
| 657 | emit_js_data(writer, flags, warning_messages, warning_links, warning_records, |
| 658 | warn_patterns, project_names) |
| 659 | writer(scripts_for_warning_groups) |
| 660 | writer('</script>') |
| 661 | emit_buttons(writer) |
| 662 | # Warning messages are grouped by severities or project names. |
| 663 | writer('<br><div id="warning_groups"></div>') |
| 664 | if flags.byproject: |
| 665 | writer('<script>groupByProject();</script>') |
| 666 | else: |
| 667 | writer('<script>groupBySeverity();</script>') |
| 668 | dump_fixed(writer, warn_patterns) |
| 669 | dump_html_epilogue(writer) |
| 670 | |
| 671 | |
| 672 | def write_html(flags, project_names, warn_patterns, html_path, warning_messages, |
| 673 | warning_links, warning_records, header_str): |
| 674 | """Write warnings html file.""" |
| 675 | if html_path: |
| 676 | with open(html_path, 'w') as f: |
| 677 | dump_html(flags, f, warning_messages, warning_links, warning_records, |
| 678 | header_str, warn_patterns, project_names) |
| 679 | |
| 680 | |
| 681 | def write_out_csv(flags, warn_patterns, warning_messages, warning_links, |
| 682 | warning_records, header_str, project_names): |
| 683 | """Write warnings csv file.""" |
| 684 | if flags.csvpath: |
| 685 | with open(flags.csvpath, 'w') as f: |
| 686 | dump_csv(csv.writer(f, lineterminator='\n'), warn_patterns) |
| 687 | |
Saeid Farivar Asanjan | 75dc8d2 | 2020-11-18 00:29:43 +0000 | [diff] [blame] | 688 | if flags.csvwithdescription: |
| 689 | with open(flags.csvwithdescription, 'w') as f: |
| 690 | dump_csv_with_description(csv.writer(f, lineterminator='\n'), |
| 691 | warning_records, warning_messages, |
| 692 | warn_patterns, project_names) |
| 693 | |
Chih-Hung Hsieh | 3cce2bc | 2020-02-27 15:39:18 -0800 | [diff] [blame] | 694 | if flags.gencsv: |
| 695 | dump_csv(csv.writer(sys.stdout, lineterminator='\n'), warn_patterns) |
| 696 | else: |
| 697 | dump_html(flags, sys.stdout, warning_messages, warning_links, |
| 698 | warning_records, header_str, warn_patterns, project_names) |