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