Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 2 | |
| 3 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 4 | # Copyright (c) 2018, Arm Limited, All Rights Reserved |
| 5 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 6 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 7 | This script checks the current state of the source code for minor issues, |
| 8 | including incorrect file permissions, presence of tabs, non-Unix line endings, |
Gilles Peskine | 55b49ee | 2019-07-04 19:31:33 +0200 | [diff] [blame] | 9 | trailing whitespace, and presence of UTF-8 BOM. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 10 | Note: requires python 3, must be run from Mbed TLS root. |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | import argparse |
| 15 | import logging |
| 16 | import codecs |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 17 | import re |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 18 | import subprocess |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 19 | import sys |
| 20 | |
| 21 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 22 | class FileIssueTracker: |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 23 | """Base class for file-wide issue tracking. |
| 24 | |
| 25 | To implement a checker that processes a file as a whole, inherit from |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 26 | this class and implement `check_file_for_issue` and define ``heading``. |
| 27 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 28 | ``suffix_exemptions``: files whose name ends with a string in this set |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 29 | will not be checked. |
| 30 | |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 31 | ``path_exemptions``: files whose path (relative to the root of the source |
| 32 | tree) matches this regular expression will not be checked. This can be |
| 33 | ``None`` to match no path. Paths are normalized and converted to ``/`` |
| 34 | separators before matching. |
| 35 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 36 | ``heading``: human-readable description of the issue |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 37 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 38 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 39 | suffix_exemptions = frozenset() |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 40 | path_exemptions = None |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 41 | # heading must be defined in derived classes. |
| 42 | # pylint: disable=no-member |
| 43 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 44 | def __init__(self): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 45 | self.files_with_issues = {} |
| 46 | |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 47 | @staticmethod |
| 48 | def normalize_path(filepath): |
| 49 | """Normalize ``filepath`` """ |
| 50 | filepath = os.path.normpath(filepath) |
| 51 | seps = os.path.sep |
| 52 | if os.path.altsep is not None: |
| 53 | seps += os.path.altsep |
| 54 | return '/'.join(filepath.split(seps)) |
| 55 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 56 | def should_check_file(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 57 | """Whether the given file name should be checked. |
| 58 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 59 | Files whose name ends with a string listed in ``self.suffix_exemptions`` |
| 60 | or whose path matches ``self.path_exemptions`` will not be checked. |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 61 | """ |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 62 | for files_exemption in self.suffix_exemptions: |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 63 | if filepath.endswith(files_exemption): |
| 64 | return False |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 65 | if self.path_exemptions and \ |
| 66 | re.match(self.path_exemptions, self.normalize_path(filepath)): |
| 67 | return False |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 68 | return True |
| 69 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 70 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 71 | """Check the specified file for the issue that this class is for. |
| 72 | |
| 73 | Subclasses must implement this method. |
| 74 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 75 | raise NotImplementedError |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 76 | |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 77 | def record_issue(self, filepath, line_number): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 78 | """Record that an issue was found at the specified location.""" |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 79 | if filepath not in self.files_with_issues.keys(): |
| 80 | self.files_with_issues[filepath] = [] |
| 81 | self.files_with_issues[filepath].append(line_number) |
| 82 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 83 | def output_file_issues(self, logger): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 84 | """Log all the locations where the issue was found.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 85 | if self.files_with_issues.values(): |
| 86 | logger.info(self.heading) |
| 87 | for filename, lines in sorted(self.files_with_issues.items()): |
| 88 | if lines: |
| 89 | logger.info("{}: {}".format( |
| 90 | filename, ", ".join(str(x) for x in lines) |
| 91 | )) |
| 92 | else: |
| 93 | logger.info(filename) |
| 94 | logger.info("") |
| 95 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 96 | BINARY_FILE_PATH_RE_LIST = [ |
| 97 | r'docs/.*\.pdf\Z', |
| 98 | r'programs/fuzz/corpuses/[^.]+\Z', |
| 99 | r'tests/data_files/[^.]+\Z', |
| 100 | r'tests/data_files/.*\.(crt|csr|db|der|key|pubkey)\Z', |
| 101 | r'tests/data_files/.*\.req\.[^/]+\Z', |
| 102 | r'tests/data_files/.*malformed[^/]+\Z', |
| 103 | r'tests/data_files/format_pkcs12\.fmt\Z', |
| 104 | ] |
| 105 | BINARY_FILE_PATH_RE = re.compile('|'.join(BINARY_FILE_PATH_RE_LIST)) |
| 106 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 107 | class LineIssueTracker(FileIssueTracker): |
| 108 | """Base class for line-by-line issue tracking. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 109 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 110 | To implement a checker that processes files line by line, inherit from |
| 111 | this class and implement `line_with_issue`. |
| 112 | """ |
| 113 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 114 | # Exclude binary files. |
| 115 | path_exemptions = BINARY_FILE_PATH_RE |
| 116 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 117 | def issue_with_line(self, line, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 118 | """Check the specified line for the issue that this class is for. |
| 119 | |
| 120 | Subclasses must implement this method. |
| 121 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 122 | raise NotImplementedError |
| 123 | |
| 124 | def check_file_line(self, filepath, line, line_number): |
| 125 | if self.issue_with_line(line, filepath): |
| 126 | self.record_issue(filepath, line_number) |
| 127 | |
| 128 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 129 | """Check the lines of the specified file. |
| 130 | |
| 131 | Subclasses must implement the ``issue_with_line`` method. |
| 132 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 133 | with open(filepath, "rb") as f: |
| 134 | for i, line in enumerate(iter(f.readline, b"")): |
| 135 | self.check_file_line(filepath, line, i + 1) |
| 136 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 137 | |
| 138 | def is_windows_file(filepath): |
| 139 | _root, ext = os.path.splitext(filepath) |
Gilles Peskine | d2df86f | 2020-05-10 17:36:51 +0200 | [diff] [blame] | 140 | return ext in ('.bat', '.dsp', '.dsw', '.sln', '.vcxproj') |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 141 | |
| 142 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 143 | class PermissionIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 144 | """Track files with bad permissions. |
| 145 | |
| 146 | Files that are not executable scripts must not be executable.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 147 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 148 | heading = "Incorrect permissions:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 149 | |
| 150 | def check_file_for_issue(self, filepath): |
Gilles Peskine | 23e64f2 | 2019-02-25 21:24:27 +0100 | [diff] [blame] | 151 | is_executable = os.access(filepath, os.X_OK) |
| 152 | should_be_executable = filepath.endswith((".sh", ".pl", ".py")) |
| 153 | if is_executable != should_be_executable: |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 154 | self.files_with_issues[filepath] = None |
| 155 | |
| 156 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 157 | class EndOfFileNewlineIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 158 | """Track files that end with an incomplete line |
| 159 | (no newline character at the end of the last line).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 160 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 161 | heading = "Missing newline at end of file:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 162 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 163 | path_exemptions = BINARY_FILE_PATH_RE |
| 164 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 165 | def check_file_for_issue(self, filepath): |
| 166 | with open(filepath, "rb") as f: |
Gilles Peskine | 12b180a | 2020-05-10 17:36:42 +0200 | [diff] [blame] | 167 | try: |
| 168 | f.seek(-1, 2) |
| 169 | except OSError: |
| 170 | # This script only works on regular files. If we can't seek |
| 171 | # 1 before the end, it means that this position is before |
| 172 | # the beginning of the file, i.e. that the file is empty. |
| 173 | return |
| 174 | if f.read(1) != b"\n": |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 175 | self.files_with_issues[filepath] = None |
| 176 | |
| 177 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 178 | class Utf8BomIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 179 | """Track files that start with a UTF-8 BOM. |
| 180 | Files should be ASCII or UTF-8. Valid UTF-8 does not start with a BOM.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 181 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 182 | heading = "UTF-8 BOM present:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 183 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 184 | suffix_exemptions = frozenset([".vcxproj", ".sln"]) |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 185 | path_exemptions = BINARY_FILE_PATH_RE |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 186 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 187 | def check_file_for_issue(self, filepath): |
| 188 | with open(filepath, "rb") as f: |
| 189 | if f.read().startswith(codecs.BOM_UTF8): |
| 190 | self.files_with_issues[filepath] = None |
| 191 | |
| 192 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 193 | class UnixLineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 194 | """Track files with non-Unix line endings (i.e. files with CR).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 195 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 196 | heading = "Non-Unix line endings:" |
| 197 | |
| 198 | def should_check_file(self, filepath): |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 199 | if not super().should_check_file(filepath): |
| 200 | return False |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 201 | return not is_windows_file(filepath) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 202 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 203 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 204 | return b"\r" in line |
| 205 | |
| 206 | |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 207 | class WindowsLineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | d703a2e | 2020-04-01 13:35:46 +0200 | [diff] [blame] | 208 | """Track files with non-Windows line endings (i.e. CR or LF not in CRLF).""" |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 209 | |
| 210 | heading = "Non-Windows line endings:" |
| 211 | |
| 212 | def should_check_file(self, filepath): |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 213 | if not super().should_check_file(filepath): |
| 214 | return False |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 215 | return is_windows_file(filepath) |
| 216 | |
| 217 | def issue_with_line(self, line, _filepath): |
Gilles Peskine | d703a2e | 2020-04-01 13:35:46 +0200 | [diff] [blame] | 218 | return not line.endswith(b"\r\n") or b"\r" in line[:-2] |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 219 | |
| 220 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 221 | class TrailingWhitespaceIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 222 | """Track lines with trailing whitespace.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 223 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 224 | heading = "Trailing whitespace:" |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 225 | suffix_exemptions = frozenset([".dsp", ".md"]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 226 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 227 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 228 | return line.rstrip(b"\r\n") != line.rstrip() |
| 229 | |
| 230 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 231 | class TabIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 232 | """Track lines with tabs.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 233 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 234 | heading = "Tabs present:" |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 235 | suffix_exemptions = frozenset([ |
Gilles Peskine | 344da1c | 2020-05-10 17:37:02 +0200 | [diff] [blame] | 236 | ".pem", # some openssl dumps have tabs |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 237 | ".sln", |
Gilles Peskine | 6e8d5a0 | 2020-03-24 22:01:28 +0100 | [diff] [blame] | 238 | "/Makefile", |
| 239 | "/Makefile.inc", |
| 240 | "/generate_visualc_files.pl", |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 241 | ]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 242 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 243 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 244 | return b"\t" in line |
| 245 | |
| 246 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 247 | class MergeArtifactIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 248 | """Track lines with merge artifacts. |
| 249 | These are leftovers from a ``git merge`` that wasn't fully edited.""" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 250 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 251 | heading = "Merge artifact:" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 252 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 253 | def issue_with_line(self, line, _filepath): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 254 | # Detect leftover git conflict markers. |
| 255 | if line.startswith(b'<<<<<<< ') or line.startswith(b'>>>>>>> '): |
| 256 | return True |
| 257 | if line.startswith(b'||||||| '): # from merge.conflictStyle=diff3 |
| 258 | return True |
| 259 | if line.rstrip(b'\r\n') == b'=======' and \ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 260 | not _filepath.endswith('.md'): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 261 | return True |
| 262 | return False |
| 263 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 264 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 265 | class IntegrityChecker: |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 266 | """Sanity-check files under the current directory.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 267 | |
| 268 | def __init__(self, log_file): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 269 | """Instantiate the sanity checker. |
| 270 | Check files under the current directory. |
| 271 | Write a report of issues to log_file.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 272 | self.check_repo_path() |
| 273 | self.logger = None |
| 274 | self.setup_logger(log_file) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 275 | self.issues_to_check = [ |
| 276 | PermissionIssueTracker(), |
| 277 | EndOfFileNewlineIssueTracker(), |
| 278 | Utf8BomIssueTracker(), |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 279 | UnixLineEndingIssueTracker(), |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 280 | WindowsLineEndingIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 281 | TrailingWhitespaceIssueTracker(), |
| 282 | TabIssueTracker(), |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 283 | MergeArtifactIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 284 | ] |
| 285 | |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 286 | @staticmethod |
| 287 | def check_repo_path(): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 288 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
| 289 | raise Exception("Must be run from Mbed TLS root") |
| 290 | |
| 291 | def setup_logger(self, log_file, level=logging.INFO): |
| 292 | self.logger = logging.getLogger() |
| 293 | self.logger.setLevel(level) |
| 294 | if log_file: |
| 295 | handler = logging.FileHandler(log_file) |
| 296 | self.logger.addHandler(handler) |
| 297 | else: |
| 298 | console = logging.StreamHandler() |
| 299 | self.logger.addHandler(console) |
| 300 | |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 301 | @staticmethod |
| 302 | def collect_files(): |
| 303 | bytes_output = subprocess.check_output(['git', 'ls-files', '-z']) |
| 304 | bytes_filepaths = bytes_output.split(b'\0')[:-1] |
| 305 | ascii_filepaths = map(lambda fp: fp.decode('ascii'), bytes_filepaths) |
| 306 | # Prepend './' to files in the top-level directory so that |
| 307 | # something like `'/Makefile' in fp` matches in the top-level |
| 308 | # directory as well as in subdirectories. |
| 309 | return [fp if os.path.dirname(fp) else os.path.join(os.curdir, fp) |
| 310 | for fp in ascii_filepaths] |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 311 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 312 | def check_files(self): |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 313 | for issue_to_check in self.issues_to_check: |
| 314 | for filepath in self.collect_files(): |
| 315 | if issue_to_check.should_check_file(filepath): |
| 316 | issue_to_check.check_file_for_issue(filepath) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 317 | |
| 318 | def output_issues(self): |
| 319 | integrity_return_code = 0 |
| 320 | for issue_to_check in self.issues_to_check: |
| 321 | if issue_to_check.files_with_issues: |
| 322 | integrity_return_code = 1 |
| 323 | issue_to_check.output_file_issues(self.logger) |
| 324 | return integrity_return_code |
| 325 | |
| 326 | |
| 327 | def run_main(): |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 328 | parser = argparse.ArgumentParser(description=__doc__) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 329 | parser.add_argument( |
| 330 | "-l", "--log_file", type=str, help="path to optional output log", |
| 331 | ) |
| 332 | check_args = parser.parse_args() |
| 333 | integrity_check = IntegrityChecker(check_args.log_file) |
| 334 | integrity_check.check_files() |
| 335 | return_code = integrity_check.output_issues() |
| 336 | sys.exit(return_code) |
| 337 | |
| 338 | |
| 339 | if __name__ == "__main__": |
| 340 | run_main() |