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:
Matthias Krüger 2014-07-21 01:28:29 +02:00
parent 57a05bc341
commit 46a0ab1754
1 changed files with 36 additions and 21 deletions

View File

@ -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,6 +375,10 @@ 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()):
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( 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))
@ -393,9 +403,14 @@ if __name__ == '__main__':
(data['htmlfile'], error['line'], error['line'], (data['htmlfile'], error['line'], error['line'],
error['id'], error['severity'], error_class, error['id'], error['severity'], error_class,
error['msg'])) 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: