htmlreport: fix #5998, crash on some files with "UnicodeDecodeError".
For those files that we can't decode, don't generate a html-preview but note into index.html that we couldn't process the file. At the end, summarize on which files we failed and suggest using --source-encoding option.
This commit is contained in:
parent
57a05bc341
commit
46a0ab1754
|
@ -308,6 +308,7 @@ if __name__ == '__main__':
|
||||||
# Generate a HTML file with syntax highlighted source code for each
|
# Generate a HTML file with syntax highlighted source code for each
|
||||||
# file that contains one or more errors.
|
# file that contains one or more errors.
|
||||||
print('Processing errors')
|
print('Processing errors')
|
||||||
|
decode_errors = []
|
||||||
for filename, data in sorted(files.items()):
|
for filename, data in sorted(files.items()):
|
||||||
htmlfile = data['htmlfile']
|
htmlfile = data['htmlfile']
|
||||||
errors = data['errors']
|
errors = data['errors']
|
||||||
|
@ -327,6 +328,11 @@ if __name__ == '__main__':
|
||||||
sys.stderr.write("ERROR: Source file '%s' not found.\n" %
|
sys.stderr.write("ERROR: Source file '%s' not found.\n" %
|
||||||
source_filename)
|
source_filename)
|
||||||
continue
|
continue
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
sys.stderr.write("WARNING: Unicode decode error in '%s'.\n" %
|
||||||
|
source_filename)
|
||||||
|
decode_errors.append(source_filename[2:]) # "[2:]" gets rid of "./" at beginning
|
||||||
|
continue
|
||||||
|
|
||||||
htmlFormatter = AnnotateCodeFormatter(linenos=True,
|
htmlFormatter = AnnotateCodeFormatter(linenos=True,
|
||||||
style='colorful',
|
style='colorful',
|
||||||
|
@ -369,33 +375,42 @@ if __name__ == '__main__':
|
||||||
output_file.write(
|
output_file.write(
|
||||||
' <tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>')
|
' <tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>')
|
||||||
for filename, data in sorted(files.items()):
|
for filename, data in sorted(files.items()):
|
||||||
output_file.write(
|
if filename in decode_errors: # don't print a link but a note
|
||||||
|
output_file.write("\n <tr><td colspan='4'>%s</td></tr>" % (filename))
|
||||||
|
output_file.write("\n <tr><td colspan='4'> Could not generated due to UnicodeDecodeError</td></tr>")
|
||||||
|
else:
|
||||||
|
output_file.write(
|
||||||
"\n <tr><td colspan='4'><a href='%s'>%s</a></td></tr>" %
|
"\n <tr><td colspan='4'><a href='%s'>%s</a></td></tr>" %
|
||||||
(data['htmlfile'], filename))
|
(data['htmlfile'], filename))
|
||||||
for error in data['errors']:
|
for error in data['errors']:
|
||||||
error_class = ''
|
error_class = ''
|
||||||
try:
|
try:
|
||||||
if error['inconclusive'] == 'true':
|
if error['inconclusive'] == 'true':
|
||||||
error_class = 'class="inconclusive"'
|
error_class = 'class="inconclusive"'
|
||||||
error['severity'] += ", inconcl."
|
error['severity'] += ", inconcl."
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if error['severity'] == 'error':
|
||||||
|
error_class = 'class="error"'
|
||||||
|
if error['id'] == 'missingInclude':
|
||||||
|
output_file.write(
|
||||||
|
'\n <tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>' %
|
||||||
|
(error['id'], error['severity'], error['msg']))
|
||||||
|
else:
|
||||||
|
output_file.write(
|
||||||
|
"\n <tr><td><a href='%s#line-%d'>%d</a></td><td>%s</td><td>%s</td><td %s>%s</td></tr>" %
|
||||||
|
(data['htmlfile'], error['line'], error['line'],
|
||||||
|
error['id'], error['severity'], error_class,
|
||||||
|
error['msg']))
|
||||||
|
|
||||||
if error['severity'] == 'error':
|
|
||||||
error_class = 'class="error"'
|
|
||||||
if error['id'] == 'missingInclude':
|
|
||||||
output_file.write(
|
|
||||||
'\n <tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>' %
|
|
||||||
(error['id'], error['severity'], error['msg']))
|
|
||||||
else:
|
|
||||||
output_file.write(
|
|
||||||
"\n <tr><td><a href='%s#line-%d'>%d</a></td><td>%s</td><td>%s</td><td %s>%s</td></tr>" %
|
|
||||||
(data['htmlfile'], error['line'], error['line'],
|
|
||||||
error['id'], error['severity'], error_class,
|
|
||||||
error['msg']))
|
|
||||||
output_file.write('\n </table>')
|
output_file.write('\n </table>')
|
||||||
output_file.write(HTML_FOOTER % contentHandler.versionCppcheck)
|
output_file.write(HTML_FOOTER % contentHandler.versionCppcheck)
|
||||||
|
|
||||||
|
if (decode_errors):
|
||||||
|
sys.stderr.write("\nGenerating html failed for the following files: " + ' '.join(decode_errors))
|
||||||
|
sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n")
|
||||||
|
|
||||||
print('Creating style.css file')
|
print('Creating style.css file')
|
||||||
with io.open(os.path.join(options.report_dir, 'style.css'),
|
with io.open(os.path.join(options.report_dir, 'style.css'),
|
||||||
'w') as css_file:
|
'w') as css_file:
|
||||||
|
|
Loading…
Reference in New Issue