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