Use Unicode consistently throughout the script
This is related to #186.
This commit is contained in:
parent
c6702db9ab
commit
0758088811
|
@ -1,7 +1,12 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import io
|
||||||
import sys
|
import sys
|
||||||
import optparse
|
import optparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
from pygments.lexers import guess_lexer_for_filename
|
from pygments.lexers import guess_lexer_for_filename
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
@ -183,7 +188,7 @@ if __name__ == '__main__':
|
||||||
parser.add_option("--file", dest="file", help="The cppcheck xml output file to read defects from. Default is reading from stdin.")
|
parser.add_option("--file", dest="file", help="The cppcheck xml output file to read defects from. Default is reading from stdin.")
|
||||||
parser.add_option("--report-dir", dest="report_dir", help="The directory where the HTML report content is written.")
|
parser.add_option("--report-dir", dest="report_dir", help="The directory where the HTML report content is written.")
|
||||||
parser.add_option("--source-dir", dest="source_dir", help="Base directory where source code files can be found.")
|
parser.add_option("--source-dir", dest="source_dir", help="Base directory where source code files can be found.")
|
||||||
parser.add_option("--source-encoding", dest="source_encoding", help="Encoding of source code.", default=None)
|
parser.add_option("--source-encoding", dest="source_encoding", help="Encoding of source code.", default='utf-8')
|
||||||
|
|
||||||
# Parse options and make sure that we have an output directory set.
|
# Parse options and make sure that we have an output directory set.
|
||||||
options, args = parser.parse_args()
|
options, args = parser.parse_args()
|
||||||
|
@ -200,7 +205,7 @@ if __name__ == '__main__':
|
||||||
if options.file:
|
if options.file:
|
||||||
if not os.path.exists(options.file):
|
if not os.path.exists(options.file):
|
||||||
parser.error("cppcheck xml file: %s not found." % options.file)
|
parser.error("cppcheck xml file: %s not found." % options.file)
|
||||||
stream = open(options.file, "r")
|
stream = io.open(options.file, "r")
|
||||||
|
|
||||||
# Parse the xml file and produce a simple list of errors.
|
# Parse the xml file and produce a simple list of errors.
|
||||||
print("Parsing xml report.")
|
print("Parsing xml report.")
|
||||||
|
@ -233,44 +238,48 @@ if __name__ == '__main__':
|
||||||
# file that contains one or more errors.
|
# file that contains one or more errors.
|
||||||
print("Processing errors")
|
print("Processing errors")
|
||||||
for filename, data in files.items():
|
for filename, data in files.items():
|
||||||
try:
|
htmlfile = data["htmlfile"]
|
||||||
htmlfile = data["htmlfile"]
|
errors = data["errors"]
|
||||||
errors = data["errors"]
|
|
||||||
|
|
||||||
lines = []
|
lines = []
|
||||||
for error in errors:
|
for error in errors:
|
||||||
lines.append(error["line"])
|
lines.append(error["line"])
|
||||||
|
|
||||||
if filename == "":
|
if filename == "":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
source_file = os.path.join(source_dir, filename)
|
source_filename = os.path.join(source_dir, filename)
|
||||||
if not os.path.isfile(source_file):
|
if not os.path.isfile(source_filename):
|
||||||
sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_file)
|
sys.stderr.write("ERROR: Source file '%s' not found.\n" %
|
||||||
continue
|
source_filename)
|
||||||
stream = open(source_file)
|
continue
|
||||||
content = stream.read()
|
with io.open(source_filename, 'r') as input_file:
|
||||||
stream.close()
|
content = input_file.read()
|
||||||
|
|
||||||
htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', hl_lines=lines, lineanchors="line", encoding=options.source_encoding)
|
htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', hl_lines=lines, lineanchors="line", encoding=options.source_encoding)
|
||||||
htmlFormatter.errors = errors
|
htmlFormatter.errors = errors
|
||||||
stream = open(os.path.join(options.report_dir, htmlfile), "w")
|
with io.open(os.path.join(options.report_dir, htmlfile), 'w') as output_file:
|
||||||
stream.write(HTML_HEAD % (options.title, htmlFormatter.get_style_defs(".highlight"), options.title))
|
output_file.write(HTML_HEAD %
|
||||||
lexer = guess_lexer_for_filename(source_file, "")
|
(options.title,
|
||||||
|
htmlFormatter.get_style_defs(".highlight"),
|
||||||
|
options.title))
|
||||||
|
|
||||||
|
lexer = guess_lexer_for_filename(source_filename, "")
|
||||||
if options.source_encoding:
|
if options.source_encoding:
|
||||||
lexer.encoding = options.source_encoding
|
lexer.encoding = options.source_encoding
|
||||||
stream.write(highlight(content, lexer, htmlFormatter))
|
|
||||||
stream.write(HTML_FOOTER)
|
|
||||||
stream.close()
|
|
||||||
|
|
||||||
print(" " + filename)
|
output_file.write(
|
||||||
except Exception as message:
|
highlight(content, lexer, htmlFormatter).decode(
|
||||||
print("ERROR: Filename: %s, %s" % (filename, message))
|
options.source_encoding))
|
||||||
|
|
||||||
|
output_file.write(HTML_FOOTER)
|
||||||
|
|
||||||
|
print(" " + filename)
|
||||||
|
|
||||||
# Generate a master index.html file that will contain a list of
|
# Generate a master index.html file that will contain a list of
|
||||||
# all the errors created.
|
# all the errors created.
|
||||||
print("Creating index.html")
|
print("Creating index.html")
|
||||||
stream = open(os.path.join(options.report_dir, "index.html"), "w")
|
stream = io.open(os.path.join(options.report_dir, "index.html"), "w")
|
||||||
stream.write(HTML_HEAD % (options.title, "", options.title))
|
stream.write(HTML_HEAD % (options.title, "", options.title))
|
||||||
stream.write("<table>")
|
stream.write("<table>")
|
||||||
stream.write("<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>")
|
stream.write("<tr><th>Line</th><th>Id</th><th>Severity</th><th>Message</th></tr>")
|
||||||
|
@ -294,6 +303,6 @@ if __name__ == '__main__':
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
print("Creating style.css file")
|
print("Creating style.css file")
|
||||||
stream = open(os.path.join(options.report_dir, "style.css"), "w")
|
stream = io.open(os.path.join(options.report_dir, "style.css"), "w")
|
||||||
stream.write(STYLE_FILE)
|
stream.write(STYLE_FILE)
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
Loading…
Reference in New Issue