Merge "Attempt to make pylint output more useful"

This commit is contained in:
Jenkins 2015-08-12 09:34:55 +00:00 committed by Gerrit Code Review
commit c31739dbdd

View File

@ -37,6 +37,8 @@ ENABLED_CODES=(
# convention category # convention category
"C1001") "C1001")
LINE_PATTERN = r"(\S+):(\d+): \[(\S+)(, \S*)?] (.*)"
KNOWN_PYLINT_EXCEPTIONS_FILE = "tools/pylint_exceptions" KNOWN_PYLINT_EXCEPTIONS_FILE = "tools/pylint_exceptions"
@ -46,13 +48,14 @@ class LintOutput(object):
_cached_content = None _cached_content = None
def __init__(self, filename, lineno, line_content, code, message, def __init__(self, filename, lineno, line_content, code, message,
lintoutput): lintoutput, additional_content):
self.filename = filename self.filename = filename
self.lineno = lineno self.lineno = lineno
self.line_content = line_content self.line_content = line_content
self.code = code self.code = code
self.message = message self.message = message
self.lintoutput = lintoutput self.lintoutput = lintoutput
self.additional_content = additional_content
@classmethod @classmethod
def get_duplicate_code_location(cls, remaining_lines): def get_duplicate_code_location(cls, remaining_lines):
@ -76,22 +79,32 @@ class LintOutput(object):
@classmethod @classmethod
def from_line(cls, line, remaining_lines): def from_line(cls, line, remaining_lines):
m = re.search(r"(\S+):(\d+): \[(\S+)(, \S*)?] (.*)", line) m = re.search(LINE_PATTERN, line)
if not m: if not m:
return None return None
matched = m.groups() matched = m.groups()
filename, lineno, code, message = (matched[0], int(matched[1]), filename, lineno, code, message = (matched[0], int(matched[1]),
matched[2], matched[-1]) matched[2], matched[-1])
additional_content = None
# duplicate code output needs special handling # duplicate code output needs special handling
if "duplicate-code" in code: if "duplicate-code" in code:
line_count = 0
for next_line in remaining_lines:
if re.search(LINE_PATTERN, next_line):
break
line_count += 1
if line_count:
additional_content = remaining_lines[0:line_count]
filename, lineno = cls.get_duplicate_code_location(remaining_lines) filename, lineno = cls.get_duplicate_code_location(remaining_lines)
# fixes incorrectly reported file path # fixes incorrectly reported file path
line = line.replace(matched[0], filename) line = line.replace(matched[0], filename)
line = line.replace(":%s:" % matched[1], "")
line_content = cls.get_line_content(filename, lineno) line_content = cls.get_line_content(filename, lineno)
return cls(filename, lineno, line_content, code, message, return cls(filename, lineno, line_content, code, message,
line.rstrip()) line.rstrip(), additional_content)
@classmethod @classmethod
def from_msg_to_dict(cls, msg): def from_msg_to_dict(cls, msg):
@ -178,6 +191,11 @@ def validate(newmsg=None):
print() print()
print(err.lintoutput) print(err.lintoutput)
print(err.review_str()) print(err.review_str())
if err.additional_content:
max_len = max(map(len, err.additional_content))
print("-" * max_len)
print(os.linesep.join(err.additional_content))
print("-" * max_len)
passed = False passed = False
if passed: if passed:
print("Congrats! pylint check passed.") print("Congrats! pylint check passed.")