blob: bed25ed6ff407845ba678c15257d8e05aa3139bd [file] [log] [blame]
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -08001# 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
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -070018# Many functions in this module have too many arguments to be refactored.
19# pylint:disable=too-many-arguments,missing-function-docstring
20
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -080021# To emit html page of warning messages:
22# flags: --byproject, --url, --separator
23# Old stuff for static html components:
24# html_script_style: static html scripts and styles
25# htmlbig:
26# dump_stats, dump_html_prologue, dump_html_epilogue:
27# emit_buttons:
28# dump_fixed
29# sort_warnings:
30# emit_stats_by_project:
31# all_patterns,
32# findproject, classify_warning
33# dump_html
34#
35# New dynamic HTML page's static JavaScript data:
36# Some data are copied from Python to JavaScript, to generate HTML elements.
37# FlagPlatform flags.platform
38# FlagURL flags.url, used by 'android'
39# FlagSeparator flags.separator, used by 'android'
40# SeverityColors: list of colors for all severity levels
41# SeverityHeaders: list of headers for all severity levels
42# SeverityColumnHeaders: list of column_headers for all severity levels
43# ProjectNames: project_names, or project_list[*][0]
44# WarnPatternsSeverity: warn_patterns[*]['severity']
45# WarnPatternsDescription: warn_patterns[*]['description']
46# WarningMessages: warning_messages
47# Warnings: warning_records
48# StatsHeader: warning count table header row
49# StatsRows: array of warning count table rows
50#
51# New dynamic HTML page's dynamic JavaScript data:
52#
53# New dynamic HTML related function to emit data:
54# escape_string, strip_escape_string, emit_warning_arrays
55# emit_js_data():
56
57from __future__ import print_function
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -080058import csv
Chih-Hung Hsieh7cc0e152021-04-26 17:09:36 -070059import html
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -080060import sys
61
62# pylint:disable=relative-beyond-top-level
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -080063from .severity import Severity
64
65
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -070066HTML_HEAD_SCRIPTS = """\
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -080067 <script type="text/javascript">
68 function expand(id) {
69 var e = document.getElementById(id);
70 var f = document.getElementById(id + "_mark");
71 if (e.style.display == 'block') {
72 e.style.display = 'none';
73 f.innerHTML = '&#x2295';
74 }
75 else {
76 e.style.display = 'block';
77 f.innerHTML = '&#x2296';
78 }
79 };
80 function expandCollapse(show) {
81 for (var id = 1; ; id++) {
82 var e = document.getElementById(id + "");
83 var f = document.getElementById(id + "_mark");
84 if (!e || !f) break;
85 e.style.display = (show ? 'block' : 'none');
86 f.innerHTML = (show ? '&#x2296' : '&#x2295');
87 }
88 };
89 </script>
90 <style type="text/css">
91 th,td{border-collapse:collapse; border:1px solid black;}
92 .button{color:blue;font-size:110%;font-weight:bolder;}
93 .bt{color:black;background-color:transparent;border:none;outline:none;
94 font-size:140%;font-weight:bolder;}
95 .c0{background-color:#e0e0e0;}
96 .c1{background-color:#d0d0d0;}
97 .t1{border-collapse:collapse; width:100%; border:1px solid black;}
98 </style>
99 <script src="https://www.gstatic.com/charts/loader.js"></script>
100"""
101
102
103def make_writer(output_stream):
104
105 def writer(text):
106 return output_stream.write(text + '\n')
107
108 return writer
109
110
111def html_big(param):
112 return '<font size="+2">' + param + '</font>'
113
114
115def dump_html_prologue(title, writer, warn_patterns, project_names):
116 writer('<html>\n<head>')
117 writer('<title>' + title + '</title>')
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700118 writer(HTML_HEAD_SCRIPTS)
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800119 emit_stats_by_project(writer, warn_patterns, project_names)
120 writer('</head>\n<body>')
121 writer(html_big(title))
122 writer('<p>')
123
124
125def dump_html_epilogue(writer):
126 writer('</body>\n</head>\n</html>')
127
128
129def sort_warnings(warn_patterns):
130 for i in warn_patterns:
131 i['members'] = sorted(set(i['members']))
132
133
134def create_warnings(warn_patterns, project_names):
135 """Creates warnings s.t.
136
137 warnings[p][s] is as specified in above docs.
138
139 Args:
140 warn_patterns: list of warning patterns for specified platform
141 project_names: list of project names
142
143 Returns:
144 2D warnings array where warnings[p][s] is # of warnings in project name p of
145 severity level s
146 """
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700147 # pylint:disable=invalid-name
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800148 warnings = {p: {s.value: 0 for s in Severity.levels} for p in project_names}
149 for i in warn_patterns:
150 s = i['severity'].value
151 for p in i['projects']:
152 warnings[p][s] += i['projects'][p]
153 return warnings
154
155
156def get_total_by_project(warnings, project_names):
157 """Returns dict, project as key and # warnings for that project as value."""
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800158 return {
159 p: sum(warnings[p][s.value] for s in Severity.levels)
160 for p in project_names
161 }
162
163
164def get_total_by_severity(warnings, project_names):
165 """Returns dict, severity as key and # warnings of that severity as value."""
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800166 return {
167 s.value: sum(warnings[p][s.value] for p in project_names)
168 for s in Severity.levels
169 }
170
171
172def 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
185def 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
215def 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)
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700238 writer(DRAW_TABLE_JAVASCRIPT)
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800239 writer('</script>')
240
241
242def 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)
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700249 total_all_projects, stats_rows = emit_row_counts_per_project(
250 warnings, total_by_project, total_by_severity, project_names)
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800251 emit_row_counts_per_severity(total_by_severity, stats_header, stats_rows,
252 total_all_projects, writer)
253
254
255def 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
289def emit_buttons(writer):
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700290 """Write the button elements in HTML."""
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800291 writer('<button class="button" onclick="expandCollapse(1);">'
292 'Expand all warnings</button>\n'
293 '<button class="button" onclick="expandCollapse(0);">'
294 'Collapse all warnings</button>\n'
295 '<button class="button" onclick="groupBySeverity();">'
296 'Group warnings by severity</button>\n'
297 '<button class="button" onclick="groupByProject();">'
298 'Group warnings by project</button><br>')
299
300
301def all_patterns(category):
302 patterns = ''
303 for i in category['patterns']:
304 patterns += i
305 patterns += ' / '
306 return patterns
307
308
309def dump_fixed(writer, warn_patterns):
310 """Show which warnings no longer occur."""
311 anchor = 'fixed_warnings'
312 mark = anchor + '_mark'
313 writer('\n<br><p style="background-color:lightblue"><b>'
314 '<button id="' + mark + '" '
315 'class="bt" onclick="expand(\'' + anchor + '\');">'
316 '&#x2295</button> Fixed warnings. '
317 'No more occurrences. Please consider turning these into '
318 'errors if possible, before they are reintroduced in to the build'
319 ':</b></p>')
320 writer('<blockquote>')
321 fixed_patterns = []
322 for i in warn_patterns:
323 if not i['members']:
324 fixed_patterns.append(i['description'] + ' (' + all_patterns(i) + ')')
325 fixed_patterns = sorted(fixed_patterns)
326 writer('<div id="' + anchor + '" style="display:none;"><table>')
327 cur_row_class = 0
328 for text in fixed_patterns:
329 cur_row_class = 1 - cur_row_class
330 # remove last '\n'
331 t = text[:-1] if text[-1] == '\n' else text
332 writer('<tr><td class="c' + str(cur_row_class) + '">' + t + '</td></tr>')
333 writer('</table></div>')
334 writer('</blockquote>')
335
336
337def write_severity(csvwriter, sev, kind, warn_patterns):
338 """Count warnings of given severity and write CSV entries to writer."""
339 total = 0
340 for pattern in warn_patterns:
341 if pattern['severity'] == sev and pattern['members']:
342 n = len(pattern['members'])
343 total += n
344 warning = kind + ': ' + (pattern['description'] or '?')
345 csvwriter.writerow([n, '', warning])
346 # print number of warnings for each project, ordered by project name
347 projects = sorted(pattern['projects'].keys())
348 for project in projects:
349 csvwriter.writerow([pattern['projects'][project], project, warning])
350 csvwriter.writerow([total, '', kind + ' warnings'])
351 return total
352
353
354def dump_csv(csvwriter, warn_patterns):
355 """Dump number of warnings in CSV format to writer."""
356 sort_warnings(warn_patterns)
357 total = 0
358 for s in Severity.levels:
359 total += write_severity(csvwriter, s, s.column_header, warn_patterns)
360 csvwriter.writerow([total, '', 'All warnings'])
361
362
Saeid Farivar Asanjan75dc8d22020-11-18 00:29:43 +0000363def dump_csv_with_description(csvwriter, warning_records, warning_messages,
364 warn_patterns, project_names):
365 """Outputs all the warning messages by project."""
366 csv_output = []
367 for record in warning_records:
368 project_name = project_names[record[1]]
369 pattern = warn_patterns[record[0]]
370 severity = pattern['severity'].header
371 category = pattern['category']
372 description = pattern['description']
373 warning = warning_messages[record[2]]
374 csv_output.append([project_name, severity,
375 category, description,
376 warning])
377 csv_output = sorted(csv_output)
378 for output in csv_output:
379 csvwriter.writerow(output)
380
381
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800382# Return s with escaped backslash and quotation characters.
383def escape_string(s):
384 return s.replace('\\', '\\\\').replace('"', '\\"')
385
386
387# Return s without trailing '\n' and escape the quotation characters.
388def strip_escape_string(s):
389 if not s:
390 return s
391 s = s[:-1] if s[-1] == '\n' else s
392 return escape_string(s)
393
394
395def emit_warning_array(name, writer, warn_patterns):
396 writer('var warning_{} = ['.format(name))
397 for w in warn_patterns:
398 if name == 'severity':
399 writer('{},'.format(w[name].value))
400 else:
401 writer('{},'.format(w[name]))
402 writer('];')
403
404
405def emit_warning_arrays(writer, warn_patterns):
406 emit_warning_array('severity', writer, warn_patterns)
407 writer('var warning_description = [')
408 for w in warn_patterns:
409 if w['members']:
410 writer('"{}",'.format(escape_string(w['description'])))
411 else:
412 writer('"",') # no such warning
413 writer('];')
414
415
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700416SCRIPTS_FOR_WARNING_GROUPS = """
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800417 function compareMessages(x1, x2) { // of the same warning type
418 return (WarningMessages[x1[2]] <= WarningMessages[x2[2]]) ? -1 : 1;
419 }
420 function byMessageCount(x1, x2) {
421 return x2[2] - x1[2]; // reversed order
422 }
423 function bySeverityMessageCount(x1, x2) {
424 // orer by severity first
425 if (x1[1] != x2[1])
426 return x1[1] - x2[1];
427 return byMessageCount(x1, x2);
428 }
429 const ParseLinePattern = /^([^ :]+):(\\d+):(.+)/;
430 function addURL(line) { // used by Android
431 if (FlagURL == "") return line;
432 if (FlagSeparator == "") {
433 return line.replace(ParseLinePattern,
434 "<a target='_blank' href='" + FlagURL + "/$1'>$1</a>:$2:$3");
435 }
436 return line.replace(ParseLinePattern,
437 "<a target='_blank' href='" + FlagURL + "/$1" + FlagSeparator +
438 "$2'>$1:$2</a>:$3");
439 }
440 function addURLToLine(line, link) { // used by Chrome
441 let line_split = line.split(":");
442 let path = line_split.slice(0,3).join(":");
443 let msg = line_split.slice(3).join(":");
444 let html_link = `<a target="_blank" href="${link}">${path}</a>${msg}`;
445 return html_link;
446 }
447 function createArrayOfDictionaries(n) {
448 var result = [];
449 for (var i=0; i<n; i++) result.push({});
450 return result;
451 }
452 function groupWarningsBySeverity() {
453 // groups is an array of dictionaries,
454 // each dictionary maps from warning type to array of warning messages.
455 var groups = createArrayOfDictionaries(SeverityColors.length);
456 for (var i=0; i<Warnings.length; i++) {
457 var w = Warnings[i][0];
458 var s = WarnPatternsSeverity[w];
459 var k = w.toString();
460 if (!(k in groups[s]))
461 groups[s][k] = [];
462 groups[s][k].push(Warnings[i]);
463 }
464 return groups;
465 }
466 function groupWarningsByProject() {
467 var groups = createArrayOfDictionaries(ProjectNames.length);
468 for (var i=0; i<Warnings.length; i++) {
469 var w = Warnings[i][0];
470 var p = Warnings[i][1];
471 var k = w.toString();
472 if (!(k in groups[p]))
473 groups[p][k] = [];
474 groups[p][k].push(Warnings[i]);
475 }
476 return groups;
477 }
478 var GlobalAnchor = 0;
479 function createWarningSection(header, color, group) {
480 var result = "";
481 var groupKeys = [];
482 var totalMessages = 0;
483 for (var k in group) {
484 totalMessages += group[k].length;
485 groupKeys.push([k, WarnPatternsSeverity[parseInt(k)], group[k].length]);
486 }
487 groupKeys.sort(bySeverityMessageCount);
488 for (var idx=0; idx<groupKeys.length; idx++) {
489 var k = groupKeys[idx][0];
490 var messages = group[k];
491 var w = parseInt(k);
492 var wcolor = SeverityColors[WarnPatternsSeverity[w]];
493 var description = WarnPatternsDescription[w];
494 if (description.length == 0)
495 description = "???";
496 GlobalAnchor += 1;
497 result += "<table class='t1'><tr bgcolor='" + wcolor + "'><td>" +
498 "<button class='bt' id='" + GlobalAnchor + "_mark" +
499 "' onclick='expand(\\"" + GlobalAnchor + "\\");'>" +
500 "&#x2295</button> " +
501 description + " (" + messages.length + ")</td></tr></table>";
502 result += "<div id='" + GlobalAnchor +
503 "' style='display:none;'><table class='t1'>";
504 var c = 0;
505 messages.sort(compareMessages);
506 if (FlagPlatform == "chrome") {
507 for (var i=0; i<messages.length; i++) {
508 result += "<tr><td class='c" + c + "'>" +
509 addURLToLine(WarningMessages[messages[i][2]], WarningLinks[messages[i][3]]) + "</td></tr>";
510 c = 1 - c;
511 }
512 } else {
513 for (var i=0; i<messages.length; i++) {
514 result += "<tr><td class='c" + c + "'>" +
515 addURL(WarningMessages[messages[i][2]]) + "</td></tr>";
516 c = 1 - c;
517 }
518 }
519 result += "</table></div>";
520 }
521 if (result.length > 0) {
522 return "<br><span style='background-color:" + color + "'><b>" +
523 header + ": " + totalMessages +
524 "</b></span><blockquote><table class='t1'>" +
525 result + "</table></blockquote>";
526
527 }
528 return ""; // empty section
529 }
530 function generateSectionsBySeverity() {
531 var result = "";
532 var groups = groupWarningsBySeverity();
533 for (s=0; s<SeverityColors.length; s++) {
534 result += createWarningSection(SeverityHeaders[s], SeverityColors[s],
535 groups[s]);
536 }
537 return result;
538 }
539 function generateSectionsByProject() {
540 var result = "";
541 var groups = groupWarningsByProject();
542 for (i=0; i<groups.length; i++) {
543 result += createWarningSection(ProjectNames[i], 'lightgrey', groups[i]);
544 }
545 return result;
546 }
547 function groupWarnings(generator) {
548 GlobalAnchor = 0;
549 var e = document.getElementById("warning_groups");
550 e.innerHTML = generator();
551 }
552 function groupBySeverity() {
553 groupWarnings(generateSectionsBySeverity);
554 }
555 function groupByProject() {
556 groupWarnings(generateSectionsByProject);
557 }
558"""
559
560
561# Emit a JavaScript const string
562def emit_const_string(name, value, writer):
563 writer('const ' + name + ' = "' + escape_string(value) + '";')
564
565
566# Emit a JavaScript const integer array.
567def emit_const_int_array(name, array, writer):
568 writer('const ' + name + ' = [')
569 for n in array:
570 writer(str(n) + ',')
571 writer('];')
572
573
574# Emit a JavaScript const string array.
575def emit_const_string_array(name, array, writer):
576 writer('const ' + name + ' = [')
577 for s in array:
578 writer('"' + strip_escape_string(s) + '",')
579 writer('];')
580
581
582# Emit a JavaScript const string array for HTML.
583def emit_const_html_string_array(name, array, writer):
584 writer('const ' + name + ' = [')
585 for s in array:
Chih-Hung Hsieh7cc0e152021-04-26 17:09:36 -0700586 writer('"' + html.escape(strip_escape_string(s)) + '",')
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800587 writer('];')
588
589
590# Emit a JavaScript const object array.
591def emit_const_object_array(name, array, writer):
592 writer('const ' + name + ' = [')
593 for x in array:
594 writer(str(x) + ',')
595 writer('];')
596
597
598def emit_js_data(writer, flags, warning_messages, warning_links,
599 warning_records, warn_patterns, project_names):
600 """Dump dynamic HTML page's static JavaScript data."""
601 emit_const_string('FlagPlatform', flags.platform, writer)
602 emit_const_string('FlagURL', flags.url, writer)
603 emit_const_string('FlagSeparator', flags.separator, writer)
604 emit_const_string_array('SeverityColors', [s.color for s in Severity.levels],
605 writer)
606 emit_const_string_array('SeverityHeaders',
607 [s.header for s in Severity.levels], writer)
608 emit_const_string_array('SeverityColumnHeaders',
609 [s.column_header for s in Severity.levels], writer)
610 emit_const_string_array('ProjectNames', project_names, writer)
611 # pytype: disable=attribute-error
612 emit_const_int_array('WarnPatternsSeverity',
613 [w['severity'].value for w in warn_patterns], writer)
614 # pytype: enable=attribute-error
615 emit_const_html_string_array('WarnPatternsDescription',
616 [w['description'] for w in warn_patterns],
617 writer)
618 emit_const_html_string_array('WarningMessages', warning_messages, writer)
619 emit_const_object_array('Warnings', warning_records, writer)
620 if flags.platform == 'chrome':
621 emit_const_html_string_array('WarningLinks', warning_links, writer)
622
623
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700624DRAW_TABLE_JAVASCRIPT = """
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800625google.charts.load('current', {'packages':['table']});
626google.charts.setOnLoadCallback(drawTable);
627function drawTable() {
628 var data = new google.visualization.DataTable();
629 data.addColumn('string', StatsHeader[0]);
630 for (var i=1; i<StatsHeader.length; i++) {
631 data.addColumn('number', StatsHeader[i]);
632 }
633 data.addRows(StatsRows);
634 for (var i=0; i<StatsRows.length; i++) {
635 for (var j=0; j<StatsHeader.length; j++) {
636 data.setProperty(i, j, 'style', 'border:1px solid black;');
637 }
638 }
639 var table = new google.visualization.Table(
640 document.getElementById('stats_table'));
641 table.draw(data, {allowHtml: true, alternatingRowStyle: true});
642}
643"""
644
645
646def dump_html(flags, output_stream, warning_messages, warning_links,
647 warning_records, header_str, warn_patterns, project_names):
648 """Dump the flags output to output_stream."""
649 writer = make_writer(output_stream)
650 dump_html_prologue('Warnings for ' + header_str, writer, warn_patterns,
651 project_names)
652 dump_stats(writer, warn_patterns)
653 writer('<br><div id="stats_table"></div><br>')
654 writer('\n<script>')
655 emit_js_data(writer, flags, warning_messages, warning_links, warning_records,
656 warn_patterns, project_names)
Chih-Hung Hsieh98b285d2021-04-28 14:49:32 -0700657 writer(SCRIPTS_FOR_WARNING_GROUPS)
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800658 writer('</script>')
659 emit_buttons(writer)
660 # Warning messages are grouped by severities or project names.
661 writer('<br><div id="warning_groups"></div>')
662 if flags.byproject:
663 writer('<script>groupByProject();</script>')
664 else:
665 writer('<script>groupBySeverity();</script>')
666 dump_fixed(writer, warn_patterns)
667 dump_html_epilogue(writer)
668
669
670def write_html(flags, project_names, warn_patterns, html_path, warning_messages,
671 warning_links, warning_records, header_str):
672 """Write warnings html file."""
673 if html_path:
674 with open(html_path, 'w') as f:
675 dump_html(flags, f, warning_messages, warning_links, warning_records,
676 header_str, warn_patterns, project_names)
677
678
679def write_out_csv(flags, warn_patterns, warning_messages, warning_links,
680 warning_records, header_str, project_names):
681 """Write warnings csv file."""
682 if flags.csvpath:
683 with open(flags.csvpath, 'w') as f:
684 dump_csv(csv.writer(f, lineterminator='\n'), warn_patterns)
685
Saeid Farivar Asanjan75dc8d22020-11-18 00:29:43 +0000686 if flags.csvwithdescription:
687 with open(flags.csvwithdescription, 'w') as f:
688 dump_csv_with_description(csv.writer(f, lineterminator='\n'),
689 warning_records, warning_messages,
690 warn_patterns, project_names)
691
Chih-Hung Hsieh3cce2bc2020-02-27 15:39:18 -0800692 if flags.gencsv:
693 dump_csv(csv.writer(sys.stdout, lineterminator='\n'), warn_patterns)
694 else:
695 dump_html(flags, sys.stdout, warning_messages, warning_links,
696 warning_records, header_str, warn_patterns, project_names)