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