Also check Windows files

Check Windows files for some issues, including permissions. Omit the
checks related to special characters (whitespace, line endings,
encoding) as appropriate.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2020-03-24 22:26:01 +01:00
parent 1978b68a2f
commit cecc726b91

View file

@ -81,6 +81,12 @@ class LineIssueTracker(FileIssueTracker):
for i, line in enumerate(iter(f.readline, b"")): for i, line in enumerate(iter(f.readline, b"")):
self.check_file_line(filepath, line, i + 1) self.check_file_line(filepath, line, i + 1)
def is_windows_file(filepath):
_root, ext = os.path.splitext(filepath)
return ext in ('.dsp', '.sln', '.vcxproj')
class PermissionIssueTracker(FileIssueTracker): class PermissionIssueTracker(FileIssueTracker):
"""Track files with bad permissions. """Track files with bad permissions.
@ -113,16 +119,21 @@ class Utf8BomIssueTracker(FileIssueTracker):
heading = "UTF-8 BOM present:" heading = "UTF-8 BOM present:"
files_exemptions = frozenset([".vcxproj", ".sln"])
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:
if f.read().startswith(codecs.BOM_UTF8): if f.read().startswith(codecs.BOM_UTF8):
self.files_with_issues[filepath] = None self.files_with_issues[filepath] = None
class LineEndingIssueTracker(LineIssueTracker): class UnixLineEndingIssueTracker(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)."""
heading = "Non Unix line endings:" heading = "Non-Unix line endings:"
def should_check_file(self, filepath):
return not is_windows_file(filepath)
def issue_with_line(self, line, _filepath): def issue_with_line(self, line, _filepath):
return b"\r" in line return b"\r" in line
@ -132,7 +143,7 @@ class TrailingWhitespaceIssueTracker(LineIssueTracker):
"""Track lines with trailing whitespace.""" """Track lines with trailing whitespace."""
heading = "Trailing whitespace:" heading = "Trailing whitespace:"
files_exemptions = frozenset(".md") files_exemptions = frozenset([".dsp", ".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()
@ -143,6 +154,7 @@ class TabIssueTracker(LineIssueTracker):
heading = "Tabs present:" heading = "Tabs present:"
files_exemptions = frozenset([ files_exemptions = frozenset([
".sln",
"/Makefile", "/Makefile",
"/generate_visualc_files.pl", "/generate_visualc_files.pl",
]) ])
@ -182,12 +194,15 @@ class IntegrityChecker(object):
self.extensions_to_check = ( self.extensions_to_check = (
".c", ".c",
".data", ".data",
".dsp",
".function", ".function",
".h", ".h",
".md", ".md",
".pl", ".pl",
".py", ".py",
".sh", ".sh",
".sln",
".vcxproj",
"/CMakeLists.txt", "/CMakeLists.txt",
"/ChangeLog", "/ChangeLog",
"/Makefile", "/Makefile",
@ -204,7 +219,7 @@ class IntegrityChecker(object):
PermissionIssueTracker(), PermissionIssueTracker(),
EndOfFileNewlineIssueTracker(), EndOfFileNewlineIssueTracker(),
Utf8BomIssueTracker(), Utf8BomIssueTracker(),
LineEndingIssueTracker(), UnixLineEndingIssueTracker(),
TrailingWhitespaceIssueTracker(), TrailingWhitespaceIssueTracker(),
TabIssueTracker(), TabIssueTracker(),
MergeArtifactIssueTracker(), MergeArtifactIssueTracker(),