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 | |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 3 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 17 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 18 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 19 | This script checks the current state of the source code for minor issues, |
| 20 | including incorrect file permissions, presence of tabs, non-Unix line endings, |
Gilles Peskine | 55b49ee | 2019-07-04 19:31:33 +0200 | [diff] [blame] | 21 | trailing whitespace, and presence of UTF-8 BOM. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 22 | Note: requires python 3, must be run from Mbed TLS root. |
| 23 | """ |
| 24 | |
| 25 | import os |
| 26 | import argparse |
| 27 | import logging |
| 28 | import codecs |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 29 | import re |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 30 | import subprocess |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 31 | import sys |
Gilles Peskine | ac9e7c0 | 2020-08-11 15:11:50 +0200 | [diff] [blame^] | 32 | try: |
| 33 | from typing import FrozenSet, Optional, Pattern # pylint: disable=unused-import |
| 34 | except ImportError: |
| 35 | pass |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 36 | |
| 37 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 38 | class FileIssueTracker: |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 39 | """Base class for file-wide issue tracking. |
| 40 | |
| 41 | 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] | 42 | this class and implement `check_file_for_issue` and define ``heading``. |
| 43 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 44 | ``suffix_exemptions``: files whose name ends with a string in this set |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 45 | will not be checked. |
| 46 | |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 47 | ``path_exemptions``: files whose path (relative to the root of the source |
| 48 | tree) matches this regular expression will not be checked. This can be |
| 49 | ``None`` to match no path. Paths are normalized and converted to ``/`` |
| 50 | separators before matching. |
| 51 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 52 | ``heading``: human-readable description of the issue |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 53 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 54 | |
Gilles Peskine | ac9e7c0 | 2020-08-11 15:11:50 +0200 | [diff] [blame^] | 55 | suffix_exemptions = frozenset() #type: FrozenSet[str] |
| 56 | path_exemptions = None #type: Optional[Pattern[str]] |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 57 | # heading must be defined in derived classes. |
| 58 | # pylint: disable=no-member |
| 59 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 60 | def __init__(self): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 61 | self.files_with_issues = {} |
| 62 | |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 63 | @staticmethod |
| 64 | def normalize_path(filepath): |
Gilles Peskine | eca95db | 2020-05-28 18:19:20 +0200 | [diff] [blame] | 65 | """Normalize ``filepath`` with / as the directory separator.""" |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 66 | filepath = os.path.normpath(filepath) |
Gilles Peskine | eca95db | 2020-05-28 18:19:20 +0200 | [diff] [blame] | 67 | # On Windows, we may have backslashes to separate directories. |
| 68 | # We need slashes to match exemption lists. |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 69 | seps = os.path.sep |
| 70 | if os.path.altsep is not None: |
| 71 | seps += os.path.altsep |
| 72 | return '/'.join(filepath.split(seps)) |
| 73 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 74 | def should_check_file(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 75 | """Whether the given file name should be checked. |
| 76 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 77 | Files whose name ends with a string listed in ``self.suffix_exemptions`` |
| 78 | or whose path matches ``self.path_exemptions`` will not be checked. |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 79 | """ |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 80 | for files_exemption in self.suffix_exemptions: |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 81 | if filepath.endswith(files_exemption): |
| 82 | return False |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 83 | if self.path_exemptions and \ |
| 84 | re.match(self.path_exemptions, self.normalize_path(filepath)): |
| 85 | return False |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 86 | return True |
| 87 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 88 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 89 | """Check the specified file for the issue that this class is for. |
| 90 | |
| 91 | Subclasses must implement this method. |
| 92 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 93 | raise NotImplementedError |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 94 | |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 95 | def record_issue(self, filepath, line_number): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 96 | """Record that an issue was found at the specified location.""" |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 97 | if filepath not in self.files_with_issues.keys(): |
| 98 | self.files_with_issues[filepath] = [] |
| 99 | self.files_with_issues[filepath].append(line_number) |
| 100 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 101 | def output_file_issues(self, logger): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 102 | """Log all the locations where the issue was found.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 103 | if self.files_with_issues.values(): |
| 104 | logger.info(self.heading) |
| 105 | for filename, lines in sorted(self.files_with_issues.items()): |
| 106 | if lines: |
| 107 | logger.info("{}: {}".format( |
| 108 | filename, ", ".join(str(x) for x in lines) |
| 109 | )) |
| 110 | else: |
| 111 | logger.info(filename) |
| 112 | logger.info("") |
| 113 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 114 | BINARY_FILE_PATH_RE_LIST = [ |
| 115 | r'docs/.*\.pdf\Z', |
| 116 | r'programs/fuzz/corpuses/[^.]+\Z', |
| 117 | r'tests/data_files/[^.]+\Z', |
| 118 | r'tests/data_files/.*\.(crt|csr|db|der|key|pubkey)\Z', |
| 119 | r'tests/data_files/.*\.req\.[^/]+\Z', |
| 120 | r'tests/data_files/.*malformed[^/]+\Z', |
| 121 | r'tests/data_files/format_pkcs12\.fmt\Z', |
| 122 | ] |
| 123 | BINARY_FILE_PATH_RE = re.compile('|'.join(BINARY_FILE_PATH_RE_LIST)) |
| 124 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 125 | class LineIssueTracker(FileIssueTracker): |
| 126 | """Base class for line-by-line issue tracking. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 127 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 128 | To implement a checker that processes files line by line, inherit from |
| 129 | this class and implement `line_with_issue`. |
| 130 | """ |
| 131 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 132 | # Exclude binary files. |
| 133 | path_exemptions = BINARY_FILE_PATH_RE |
| 134 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 135 | def issue_with_line(self, line, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 136 | """Check the specified line for the issue that this class is for. |
| 137 | |
| 138 | Subclasses must implement this method. |
| 139 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 140 | raise NotImplementedError |
| 141 | |
| 142 | def check_file_line(self, filepath, line, line_number): |
| 143 | if self.issue_with_line(line, filepath): |
| 144 | self.record_issue(filepath, line_number) |
| 145 | |
| 146 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 147 | """Check the lines of the specified file. |
| 148 | |
| 149 | Subclasses must implement the ``issue_with_line`` method. |
| 150 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 151 | with open(filepath, "rb") as f: |
| 152 | for i, line in enumerate(iter(f.readline, b"")): |
| 153 | self.check_file_line(filepath, line, i + 1) |
| 154 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 155 | |
| 156 | def is_windows_file(filepath): |
| 157 | _root, ext = os.path.splitext(filepath) |
Gilles Peskine | d2df86f | 2020-05-10 17:36:51 +0200 | [diff] [blame] | 158 | return ext in ('.bat', '.dsp', '.dsw', '.sln', '.vcxproj') |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 159 | |
| 160 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 161 | class PermissionIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 162 | """Track files with bad permissions. |
| 163 | |
| 164 | Files that are not executable scripts must not be executable.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 165 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 166 | heading = "Incorrect permissions:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 167 | |
Gilles Peskine | 15898ee | 2020-08-08 23:14:27 +0200 | [diff] [blame] | 168 | # .py files can be either full scripts or modules, so they may or may |
| 169 | # not be executable. |
| 170 | suffix_exemptions = frozenset({".py"}) |
| 171 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 172 | def check_file_for_issue(self, filepath): |
Gilles Peskine | 23e64f2 | 2019-02-25 21:24:27 +0100 | [diff] [blame] | 173 | is_executable = os.access(filepath, os.X_OK) |
Gilles Peskine | 15898ee | 2020-08-08 23:14:27 +0200 | [diff] [blame] | 174 | should_be_executable = filepath.endswith((".sh", ".pl")) |
Gilles Peskine | 23e64f2 | 2019-02-25 21:24:27 +0100 | [diff] [blame] | 175 | if is_executable != should_be_executable: |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 176 | self.files_with_issues[filepath] = None |
| 177 | |
| 178 | |
Gilles Peskine | 4aebb8d | 2020-08-08 23:15:18 +0200 | [diff] [blame] | 179 | class ShebangIssueTracker(FileIssueTracker): |
| 180 | """Track files with a bad, missing or extraneous shebang line. |
| 181 | |
| 182 | Executable scripts must start with a valid shebang (#!) line. |
| 183 | """ |
| 184 | |
| 185 | heading = "Invalid shebang line:" |
| 186 | |
| 187 | # Allow either /bin/sh, /bin/bash, or /usr/bin/env. |
| 188 | # Allow at most one argument (this is a Linux limitation). |
| 189 | # For sh and bash, the argument if present must be options. |
| 190 | # For env, the argument must be the base name of the interpeter. |
| 191 | _shebang_re = re.compile(rb'^#! ?(?:/bin/(bash|sh)(?: -[^\n ]*)?' |
| 192 | rb'|/usr/bin/env ([^\n /]+))$') |
| 193 | _extensions = { |
| 194 | b'bash': 'sh', |
| 195 | b'perl': 'pl', |
| 196 | b'python3': 'py', |
| 197 | b'sh': 'sh', |
| 198 | } |
| 199 | |
| 200 | def is_valid_shebang(self, first_line, filepath): |
| 201 | m = re.match(self._shebang_re, first_line) |
| 202 | if not m: |
| 203 | return False |
| 204 | interpreter = m.group(1) or m.group(2) |
| 205 | if interpreter not in self._extensions: |
| 206 | return False |
| 207 | if not filepath.endswith('.' + self._extensions[interpreter]): |
| 208 | return False |
| 209 | return True |
| 210 | |
| 211 | def check_file_for_issue(self, filepath): |
| 212 | is_executable = os.access(filepath, os.X_OK) |
| 213 | with open(filepath, "rb") as f: |
| 214 | first_line = f.readline() |
| 215 | if first_line.startswith(b'#!'): |
| 216 | if not is_executable: |
| 217 | # Shebang on a non-executable file |
| 218 | self.files_with_issues[filepath] = None |
| 219 | elif not self.is_valid_shebang(first_line, filepath): |
| 220 | self.files_with_issues[filepath] = [1] |
| 221 | elif is_executable: |
| 222 | # Executable without a shebang |
| 223 | self.files_with_issues[filepath] = None |
| 224 | |
| 225 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 226 | class EndOfFileNewlineIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 227 | """Track files that end with an incomplete line |
| 228 | (no newline character at the end of the last line).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 229 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 230 | heading = "Missing newline at end of file:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 231 | |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 232 | path_exemptions = BINARY_FILE_PATH_RE |
| 233 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 234 | def check_file_for_issue(self, filepath): |
| 235 | with open(filepath, "rb") as f: |
Gilles Peskine | 12b180a | 2020-05-10 17:36:42 +0200 | [diff] [blame] | 236 | try: |
| 237 | f.seek(-1, 2) |
| 238 | except OSError: |
| 239 | # This script only works on regular files. If we can't seek |
| 240 | # 1 before the end, it means that this position is before |
| 241 | # the beginning of the file, i.e. that the file is empty. |
| 242 | return |
| 243 | if f.read(1) != b"\n": |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 244 | self.files_with_issues[filepath] = None |
| 245 | |
| 246 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 247 | class Utf8BomIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 248 | """Track files that start with a UTF-8 BOM. |
| 249 | 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] | 250 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 251 | heading = "UTF-8 BOM present:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 252 | |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 253 | suffix_exemptions = frozenset([".vcxproj", ".sln"]) |
Gilles Peskine | d4a853d | 2020-05-10 16:57:59 +0200 | [diff] [blame] | 254 | path_exemptions = BINARY_FILE_PATH_RE |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 255 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 256 | def check_file_for_issue(self, filepath): |
| 257 | with open(filepath, "rb") as f: |
| 258 | if f.read().startswith(codecs.BOM_UTF8): |
| 259 | self.files_with_issues[filepath] = None |
| 260 | |
| 261 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 262 | class UnixLineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 263 | """Track files with non-Unix line endings (i.e. files with CR).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 264 | |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 265 | heading = "Non-Unix line endings:" |
| 266 | |
| 267 | def should_check_file(self, filepath): |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 268 | if not super().should_check_file(filepath): |
| 269 | return False |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 270 | return not is_windows_file(filepath) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 271 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 272 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 273 | return b"\r" in line |
| 274 | |
| 275 | |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 276 | class WindowsLineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | d703a2e | 2020-04-01 13:35:46 +0200 | [diff] [blame] | 277 | """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] | 278 | |
| 279 | heading = "Non-Windows line endings:" |
| 280 | |
| 281 | def should_check_file(self, filepath): |
Gilles Peskine | 0598db8 | 2020-05-10 16:57:16 +0200 | [diff] [blame] | 282 | if not super().should_check_file(filepath): |
| 283 | return False |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 284 | return is_windows_file(filepath) |
| 285 | |
| 286 | def issue_with_line(self, line, _filepath): |
Gilles Peskine | d703a2e | 2020-04-01 13:35:46 +0200 | [diff] [blame] | 287 | 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] | 288 | |
| 289 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 290 | class TrailingWhitespaceIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 291 | """Track lines with trailing whitespace.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 292 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 293 | heading = "Trailing whitespace:" |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 294 | suffix_exemptions = frozenset([".dsp", ".md"]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 295 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 296 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 297 | return line.rstrip(b"\r\n") != line.rstrip() |
| 298 | |
| 299 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 300 | class TabIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 301 | """Track lines with tabs.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 302 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 303 | heading = "Tabs present:" |
Gilles Peskine | 05a51a8 | 2020-05-10 16:52:44 +0200 | [diff] [blame] | 304 | suffix_exemptions = frozenset([ |
Gilles Peskine | 344da1c | 2020-05-10 17:37:02 +0200 | [diff] [blame] | 305 | ".pem", # some openssl dumps have tabs |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 306 | ".sln", |
Gilles Peskine | 6e8d5a0 | 2020-03-24 22:01:28 +0100 | [diff] [blame] | 307 | "/Makefile", |
| 308 | "/Makefile.inc", |
| 309 | "/generate_visualc_files.pl", |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 310 | ]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 311 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 312 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 313 | return b"\t" in line |
| 314 | |
| 315 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 316 | class MergeArtifactIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 317 | """Track lines with merge artifacts. |
| 318 | These are leftovers from a ``git merge`` that wasn't fully edited.""" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 319 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 320 | heading = "Merge artifact:" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 321 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 322 | def issue_with_line(self, line, _filepath): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 323 | # Detect leftover git conflict markers. |
| 324 | if line.startswith(b'<<<<<<< ') or line.startswith(b'>>>>>>> '): |
| 325 | return True |
| 326 | if line.startswith(b'||||||| '): # from merge.conflictStyle=diff3 |
| 327 | return True |
| 328 | if line.rstrip(b'\r\n') == b'=======' and \ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 329 | not _filepath.endswith('.md'): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 330 | return True |
| 331 | return False |
| 332 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 333 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 334 | class IntegrityChecker: |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 335 | """Sanity-check files under the current directory.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 336 | |
| 337 | def __init__(self, log_file): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 338 | """Instantiate the sanity checker. |
| 339 | Check files under the current directory. |
| 340 | Write a report of issues to log_file.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 341 | self.check_repo_path() |
| 342 | self.logger = None |
| 343 | self.setup_logger(log_file) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 344 | self.issues_to_check = [ |
| 345 | PermissionIssueTracker(), |
Gilles Peskine | 4aebb8d | 2020-08-08 23:15:18 +0200 | [diff] [blame] | 346 | ShebangIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 347 | EndOfFileNewlineIssueTracker(), |
| 348 | Utf8BomIssueTracker(), |
Gilles Peskine | 2c61873 | 2020-03-24 22:26:01 +0100 | [diff] [blame] | 349 | UnixLineEndingIssueTracker(), |
Gilles Peskine | 545e13f | 2020-03-24 22:29:11 +0100 | [diff] [blame] | 350 | WindowsLineEndingIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 351 | TrailingWhitespaceIssueTracker(), |
| 352 | TabIssueTracker(), |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 353 | MergeArtifactIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 354 | ] |
| 355 | |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 356 | @staticmethod |
| 357 | def check_repo_path(): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 358 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
| 359 | raise Exception("Must be run from Mbed TLS root") |
| 360 | |
| 361 | def setup_logger(self, log_file, level=logging.INFO): |
| 362 | self.logger = logging.getLogger() |
| 363 | self.logger.setLevel(level) |
| 364 | if log_file: |
| 365 | handler = logging.FileHandler(log_file) |
| 366 | self.logger.addHandler(handler) |
| 367 | else: |
| 368 | console = logging.StreamHandler() |
| 369 | self.logger.addHandler(console) |
| 370 | |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 371 | @staticmethod |
| 372 | def collect_files(): |
| 373 | bytes_output = subprocess.check_output(['git', 'ls-files', '-z']) |
| 374 | bytes_filepaths = bytes_output.split(b'\0')[:-1] |
| 375 | ascii_filepaths = map(lambda fp: fp.decode('ascii'), bytes_filepaths) |
| 376 | # Prepend './' to files in the top-level directory so that |
| 377 | # something like `'/Makefile' in fp` matches in the top-level |
| 378 | # directory as well as in subdirectories. |
| 379 | return [fp if os.path.dirname(fp) else os.path.join(os.curdir, fp) |
| 380 | for fp in ascii_filepaths] |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 381 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 382 | def check_files(self): |
Gilles Peskine | 3e2ee3c | 2020-05-10 17:18:06 +0200 | [diff] [blame] | 383 | for issue_to_check in self.issues_to_check: |
| 384 | for filepath in self.collect_files(): |
| 385 | if issue_to_check.should_check_file(filepath): |
| 386 | issue_to_check.check_file_for_issue(filepath) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 387 | |
| 388 | def output_issues(self): |
| 389 | integrity_return_code = 0 |
| 390 | for issue_to_check in self.issues_to_check: |
| 391 | if issue_to_check.files_with_issues: |
| 392 | integrity_return_code = 1 |
| 393 | issue_to_check.output_file_issues(self.logger) |
| 394 | return integrity_return_code |
| 395 | |
| 396 | |
| 397 | def run_main(): |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 398 | parser = argparse.ArgumentParser(description=__doc__) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 399 | parser.add_argument( |
| 400 | "-l", "--log_file", type=str, help="path to optional output log", |
| 401 | ) |
| 402 | check_args = parser.parse_args() |
| 403 | integrity_check = IntegrityChecker(check_args.log_file) |
| 404 | integrity_check.check_files() |
| 405 | return_code = integrity_check.output_issues() |
| 406 | sys.exit(return_code) |
| 407 | |
| 408 | |
| 409 | if __name__ == "__main__": |
| 410 | run_main() |