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