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 | |
| 3 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 4 | # Copyright (c) 2018, Arm Limited, All Rights Reserved |
| 5 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 6 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 7 | This script checks the current state of the source code for minor issues, |
| 8 | including incorrect file permissions, presence of tabs, non-Unix line endings, |
Gilles Peskine | 55b49ee | 2019-07-04 19:31:33 +0200 | [diff] [blame] | 9 | trailing whitespace, and presence of UTF-8 BOM. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 10 | Note: requires python 3, must be run from Mbed TLS root. |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | import argparse |
| 15 | import logging |
| 16 | import codecs |
| 17 | import sys |
| 18 | |
| 19 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame^] | 20 | class FileIssueTracker: |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 21 | """Base class for file-wide issue tracking. |
| 22 | |
| 23 | 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] | 24 | this class and implement `check_file_for_issue` and define ``heading``. |
| 25 | |
| 26 | ``files_exemptions``: files whose name ends with a string in this set |
| 27 | will not be checked. |
| 28 | |
| 29 | ``heading``: human-readable description of the issue |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 30 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 31 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 32 | files_exemptions = frozenset() |
| 33 | # heading must be defined in derived classes. |
| 34 | # pylint: disable=no-member |
| 35 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 36 | def __init__(self): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 37 | self.files_with_issues = {} |
| 38 | |
| 39 | def should_check_file(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 40 | """Whether the given file name should be checked. |
| 41 | |
| 42 | Files whose name ends with a string listed in ``self.files_exemptions`` |
| 43 | will not be checked. |
| 44 | """ |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 45 | for files_exemption in self.files_exemptions: |
| 46 | if filepath.endswith(files_exemption): |
| 47 | return False |
| 48 | return True |
| 49 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 50 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 51 | """Check the specified file for the issue that this class is for. |
| 52 | |
| 53 | Subclasses must implement this method. |
| 54 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 55 | raise NotImplementedError |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 56 | |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 57 | def record_issue(self, filepath, line_number): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 58 | """Record that an issue was found at the specified location.""" |
Gilles Peskine | 0439805 | 2018-11-23 21:11:30 +0100 | [diff] [blame] | 59 | if filepath not in self.files_with_issues.keys(): |
| 60 | self.files_with_issues[filepath] = [] |
| 61 | self.files_with_issues[filepath].append(line_number) |
| 62 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 63 | def output_file_issues(self, logger): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 64 | """Log all the locations where the issue was found.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 65 | if self.files_with_issues.values(): |
| 66 | logger.info(self.heading) |
| 67 | for filename, lines in sorted(self.files_with_issues.items()): |
| 68 | if lines: |
| 69 | logger.info("{}: {}".format( |
| 70 | filename, ", ".join(str(x) for x in lines) |
| 71 | )) |
| 72 | else: |
| 73 | logger.info(filename) |
| 74 | logger.info("") |
| 75 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 76 | class LineIssueTracker(FileIssueTracker): |
| 77 | """Base class for line-by-line issue tracking. |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 78 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 79 | To implement a checker that processes files line by line, inherit from |
| 80 | this class and implement `line_with_issue`. |
| 81 | """ |
| 82 | |
| 83 | def issue_with_line(self, line, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 84 | """Check the specified line for the issue that this class is for. |
| 85 | |
| 86 | Subclasses must implement this method. |
| 87 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 88 | raise NotImplementedError |
| 89 | |
| 90 | def check_file_line(self, filepath, line, line_number): |
| 91 | if self.issue_with_line(line, filepath): |
| 92 | self.record_issue(filepath, line_number) |
| 93 | |
| 94 | def check_file_for_issue(self, filepath): |
Gilles Peskine | aaee444 | 2020-03-24 16:49:21 +0100 | [diff] [blame] | 95 | """Check the lines of the specified file. |
| 96 | |
| 97 | Subclasses must implement the ``issue_with_line`` method. |
| 98 | """ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 99 | with open(filepath, "rb") as f: |
| 100 | for i, line in enumerate(iter(f.readline, b"")): |
| 101 | self.check_file_line(filepath, line, i + 1) |
| 102 | |
| 103 | class PermissionIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 104 | """Track files with bad permissions. |
| 105 | |
| 106 | Files that are not executable scripts must not be executable.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 107 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 108 | heading = "Incorrect permissions:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 109 | |
| 110 | def check_file_for_issue(self, filepath): |
Gilles Peskine | 23e64f2 | 2019-02-25 21:24:27 +0100 | [diff] [blame] | 111 | is_executable = os.access(filepath, os.X_OK) |
| 112 | should_be_executable = filepath.endswith((".sh", ".pl", ".py")) |
| 113 | if is_executable != should_be_executable: |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 114 | self.files_with_issues[filepath] = None |
| 115 | |
| 116 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 117 | class EndOfFileNewlineIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 118 | """Track files that end with an incomplete line |
| 119 | (no newline character at the end of the last line).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 120 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 121 | heading = "Missing newline at end of file:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 122 | |
| 123 | def check_file_for_issue(self, filepath): |
| 124 | with open(filepath, "rb") as f: |
| 125 | if not f.read().endswith(b"\n"): |
| 126 | self.files_with_issues[filepath] = None |
| 127 | |
| 128 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 129 | class Utf8BomIssueTracker(FileIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 130 | """Track files that start with a UTF-8 BOM. |
| 131 | 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] | 132 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 133 | heading = "UTF-8 BOM present:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 134 | |
| 135 | def check_file_for_issue(self, filepath): |
| 136 | with open(filepath, "rb") as f: |
| 137 | if f.read().startswith(codecs.BOM_UTF8): |
| 138 | self.files_with_issues[filepath] = None |
| 139 | |
| 140 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 141 | class LineEndingIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 142 | """Track files with non-Unix line endings (i.e. files with CR).""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 143 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 144 | heading = "Non Unix line endings:" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 145 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 146 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 147 | return b"\r" in line |
| 148 | |
| 149 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 150 | class TrailingWhitespaceIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 151 | """Track lines with trailing whitespace.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 152 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 153 | heading = "Trailing whitespace:" |
| 154 | files_exemptions = frozenset(".md") |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 155 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 156 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 157 | return line.rstrip(b"\r\n") != line.rstrip() |
| 158 | |
| 159 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 160 | class TabIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 161 | """Track lines with tabs.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 162 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 163 | heading = "Tabs present:" |
| 164 | files_exemptions = frozenset([ |
| 165 | "Makefile", |
Christoph M. Wintersteiger | 37eb906 | 2019-02-15 12:39:21 +0000 | [diff] [blame] | 166 | "Makefile.inc", |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 167 | "generate_visualc_files.pl", |
| 168 | ]) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 169 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 170 | def issue_with_line(self, line, _filepath): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 171 | return b"\t" in line |
| 172 | |
| 173 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 174 | class MergeArtifactIssueTracker(LineIssueTracker): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 175 | """Track lines with merge artifacts. |
| 176 | These are leftovers from a ``git merge`` that wasn't fully edited.""" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 177 | |
Gilles Peskine | 1e9698a | 2019-02-25 21:10:04 +0100 | [diff] [blame] | 178 | heading = "Merge artifact:" |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 179 | |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 180 | def issue_with_line(self, line, _filepath): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 181 | # Detect leftover git conflict markers. |
| 182 | if line.startswith(b'<<<<<<< ') or line.startswith(b'>>>>>>> '): |
| 183 | return True |
| 184 | if line.startswith(b'||||||| '): # from merge.conflictStyle=diff3 |
| 185 | return True |
| 186 | if line.rstrip(b'\r\n') == b'=======' and \ |
Gilles Peskine | 6ee576e | 2019-02-25 20:59:05 +0100 | [diff] [blame] | 187 | not _filepath.endswith('.md'): |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 188 | return True |
| 189 | return False |
| 190 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 191 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame^] | 192 | class IntegrityChecker: |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 193 | """Sanity-check files under the current directory.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 194 | |
| 195 | def __init__(self, log_file): |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 196 | """Instantiate the sanity checker. |
| 197 | Check files under the current directory. |
| 198 | Write a report of issues to log_file.""" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 199 | self.check_repo_path() |
| 200 | self.logger = None |
| 201 | self.setup_logger(log_file) |
| 202 | self.files_to_check = ( |
| 203 | ".c", ".h", ".sh", ".pl", ".py", ".md", ".function", ".data", |
Christoph M. Wintersteiger | 37eb906 | 2019-02-15 12:39:21 +0000 | [diff] [blame] | 204 | "Makefile", "Makefile.inc", "CMakeLists.txt", "ChangeLog" |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 205 | ) |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 206 | self.excluded_directories = ['.git', 'mbed-os'] |
| 207 | self.excluded_paths = list(map(os.path.normpath, [ |
| 208 | 'cov-int', |
| 209 | 'examples', |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 210 | ])) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 211 | self.issues_to_check = [ |
| 212 | PermissionIssueTracker(), |
| 213 | EndOfFileNewlineIssueTracker(), |
| 214 | Utf8BomIssueTracker(), |
| 215 | LineEndingIssueTracker(), |
| 216 | TrailingWhitespaceIssueTracker(), |
| 217 | TabIssueTracker(), |
Gilles Peskine | c117d59 | 2018-11-23 21:11:52 +0100 | [diff] [blame] | 218 | MergeArtifactIssueTracker(), |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 219 | ] |
| 220 | |
Gilles Peskine | 0d060ef | 2019-02-25 20:35:31 +0100 | [diff] [blame] | 221 | @staticmethod |
| 222 | def check_repo_path(): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 223 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
| 224 | raise Exception("Must be run from Mbed TLS root") |
| 225 | |
| 226 | def setup_logger(self, log_file, level=logging.INFO): |
| 227 | self.logger = logging.getLogger() |
| 228 | self.logger.setLevel(level) |
| 229 | if log_file: |
| 230 | handler = logging.FileHandler(log_file) |
| 231 | self.logger.addHandler(handler) |
| 232 | else: |
| 233 | console = logging.StreamHandler() |
| 234 | self.logger.addHandler(console) |
| 235 | |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 236 | def prune_branch(self, root, d): |
| 237 | if d in self.excluded_directories: |
| 238 | return True |
| 239 | if os.path.normpath(os.path.join(root, d)) in self.excluded_paths: |
| 240 | return True |
| 241 | return False |
| 242 | |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 243 | def check_files(self): |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 244 | for root, dirs, files in os.walk("."): |
| 245 | dirs[:] = sorted(d for d in dirs if not self.prune_branch(root, d)) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 246 | for filename in sorted(files): |
| 247 | filepath = os.path.join(root, filename) |
Gilles Peskine | 95c5575 | 2018-09-28 11:48:10 +0200 | [diff] [blame] | 248 | if not filepath.endswith(self.files_to_check): |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 249 | continue |
| 250 | for issue_to_check in self.issues_to_check: |
| 251 | if issue_to_check.should_check_file(filepath): |
| 252 | issue_to_check.check_file_for_issue(filepath) |
| 253 | |
| 254 | def output_issues(self): |
| 255 | integrity_return_code = 0 |
| 256 | for issue_to_check in self.issues_to_check: |
| 257 | if issue_to_check.files_with_issues: |
| 258 | integrity_return_code = 1 |
| 259 | issue_to_check.output_file_issues(self.logger) |
| 260 | return integrity_return_code |
| 261 | |
| 262 | |
| 263 | def run_main(): |
Gilles Peskine | 7dfcfce | 2019-07-04 19:31:02 +0200 | [diff] [blame] | 264 | parser = argparse.ArgumentParser(description=__doc__) |
Darryl Green | 10d9ce3 | 2018-02-28 10:02:55 +0000 | [diff] [blame] | 265 | parser.add_argument( |
| 266 | "-l", "--log_file", type=str, help="path to optional output log", |
| 267 | ) |
| 268 | check_args = parser.parse_args() |
| 269 | integrity_check = IntegrityChecker(check_args.log_file) |
| 270 | integrity_check.check_files() |
| 271 | return_code = integrity_check.output_issues() |
| 272 | sys.exit(return_code) |
| 273 | |
| 274 | |
| 275 | if __name__ == "__main__": |
| 276 | run_main() |