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