check-files.py: use class fields for class-wide constants

In an issue tracker, heading and files_exemptions are class-wide
constants, so make them so instead of being per-instance fields.
This commit is contained in:
Gilles Peskine 2019-02-25 21:10:04 +01:00
parent 7194ecb3fb
commit fb8c373a15

View File

@ -23,12 +23,19 @@ class FileIssueTracker(object):
"""Base class for file-wide issue tracking. """Base class for file-wide issue tracking.
To implement a checker that processes a file as a whole, inherit from To implement a checker that processes a file as a whole, inherit from
this class and implement `check_file_for_issue`. this class and implement `check_file_for_issue` and define ``heading``.
``files_exemptions``: files whose name ends with a string in this set
will not be checked.
``heading``: human-readable description of the issue
""" """
files_exemptions = frozenset()
# heading must be defined in derived classes.
# pylint: disable=no-member
def __init__(self): def __init__(self):
self.heading = ""
self.files_exemptions = []
self.files_with_issues = {} self.files_with_issues = {}
def should_check_file(self, filepath): def should_check_file(self, filepath):
@ -81,9 +88,7 @@ class PermissionIssueTracker(FileIssueTracker):
Files that are not executable scripts must not be executable.""" Files that are not executable scripts must not be executable."""
def __init__(self): heading = "Incorrect permissions:"
super().__init__()
self.heading = "Incorrect permissions:"
def check_file_for_issue(self, filepath): def check_file_for_issue(self, filepath):
if not (os.access(filepath, os.X_OK) == if not (os.access(filepath, os.X_OK) ==
@ -95,9 +100,7 @@ class EndOfFileNewlineIssueTracker(FileIssueTracker):
"""Track files that end with an incomplete line """Track files that end with an incomplete line
(no newline character at the end of the last line).""" (no newline character at the end of the last line)."""
def __init__(self): heading = "Missing newline at end of file:"
super().__init__()
self.heading = "Missing newline at end of file:"
def check_file_for_issue(self, filepath): def check_file_for_issue(self, filepath):
with open(filepath, "rb") as f: with open(filepath, "rb") as f:
@ -109,9 +112,7 @@ class Utf8BomIssueTracker(FileIssueTracker):
"""Track files that start with a UTF-8 BOM. """Track files that start with a UTF-8 BOM.
Files should be ASCII or UTF-8. Valid UTF-8 does not start with a BOM.""" Files should be ASCII or UTF-8. Valid UTF-8 does not start with a BOM."""
def __init__(self): heading = "UTF-8 BOM present:"
super().__init__()
self.heading = "UTF-8 BOM present:"
def check_file_for_issue(self, filepath): def check_file_for_issue(self, filepath):
with open(filepath, "rb") as f: with open(filepath, "rb") as f:
@ -122,9 +123,7 @@ class Utf8BomIssueTracker(FileIssueTracker):
class LineEndingIssueTracker(LineIssueTracker): class LineEndingIssueTracker(LineIssueTracker):
"""Track files with non-Unix line endings (i.e. files with CR).""" """Track files with non-Unix line endings (i.e. files with CR)."""
def __init__(self): heading = "Non Unix line endings:"
super().__init__()
self.heading = "Non Unix line endings:"
def issue_with_line(self, line, _filepath): def issue_with_line(self, line, _filepath):
return b"\r" in line return b"\r" in line
@ -133,10 +132,8 @@ class LineEndingIssueTracker(LineIssueTracker):
class TrailingWhitespaceIssueTracker(LineIssueTracker): class TrailingWhitespaceIssueTracker(LineIssueTracker):
"""Track lines with trailing whitespace.""" """Track lines with trailing whitespace."""
def __init__(self): heading = "Trailing whitespace:"
super().__init__() files_exemptions = frozenset(".md")
self.heading = "Trailing whitespace:"
self.files_exemptions = [".md"]
def issue_with_line(self, line, _filepath): def issue_with_line(self, line, _filepath):
return line.rstrip(b"\r\n") != line.rstrip() return line.rstrip(b"\r\n") != line.rstrip()
@ -145,12 +142,11 @@ class TrailingWhitespaceIssueTracker(LineIssueTracker):
class TabIssueTracker(LineIssueTracker): class TabIssueTracker(LineIssueTracker):
"""Track lines with tabs.""" """Track lines with tabs."""
def __init__(self): heading = "Tabs present:"
super().__init__() files_exemptions = frozenset([
self.heading = "Tabs present:" "Makefile",
self.files_exemptions = [ "generate_visualc_files.pl",
"Makefile", "generate_visualc_files.pl" ])
]
def issue_with_line(self, line, _filepath): def issue_with_line(self, line, _filepath):
return b"\t" in line return b"\t" in line
@ -160,9 +156,7 @@ class MergeArtifactIssueTracker(LineIssueTracker):
"""Track lines with merge artifacts. """Track lines with merge artifacts.
These are leftovers from a ``git merge`` that wasn't fully edited.""" These are leftovers from a ``git merge`` that wasn't fully edited."""
def __init__(self): heading = "Merge artifact:"
super().__init__()
self.heading = "Merge artifact:"
def issue_with_line(self, line, _filepath): def issue_with_line(self, line, _filepath):
# Detect leftover git conflict markers. # Detect leftover git conflict markers.
@ -178,12 +172,12 @@ class MergeArtifactIssueTracker(LineIssueTracker):
class TodoIssueTracker(LineIssueTracker): class TodoIssueTracker(LineIssueTracker):
"""Track lines containing ``TODO``.""" """Track lines containing ``TODO``."""
def __init__(self): heading = "TODO present:"
super().__init__() files_exemptions = frozenset([
self.heading = "TODO present:" os.path.basename(__file__),
self.files_exemptions = [ "benchmark.c",
__file__, "benchmark.c", "pull_request_template.md" "pull_request_template.md",
] ])
def issue_with_line(self, line, _filepath): def issue_with_line(self, line, _filepath):
return b"todo" in line.lower() return b"todo" in line.lower()