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