blob: 00fd0edfb793df167fcd7bc0c51ab1ac77d680ce [file] [log] [blame]
Darryl Green10d9ce32018-02-28 10:02:55 +00001#!/usr/bin/env python3
2"""
3This file is part of Mbed TLS (https://tls.mbed.org)
4
5Copyright (c) 2018, Arm Limited, All Rights Reserved
6
7Purpose
8
9This script checks the current state of the source code for minor issues,
10including incorrect file permissions, presence of tabs, non-Unix line endings,
11trailing whitespace, presence of UTF-8 BOM, and TODO comments.
12Note: requires python 3, must be run from Mbed TLS root.
13"""
14
15import os
16import argparse
17import logging
18import codecs
19import sys
20
21
Gilles Peskine6ee576e2019-02-25 20:59:05 +010022class 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 Peskine1e9698a2019-02-25 21:10:04 +010026 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 Peskine6ee576e2019-02-25 20:59:05 +010032 """
Darryl Green10d9ce32018-02-28 10:02:55 +000033
Gilles Peskine1e9698a2019-02-25 21:10:04 +010034 files_exemptions = frozenset()
35 # heading must be defined in derived classes.
36 # pylint: disable=no-member
37
Darryl Green10d9ce32018-02-28 10:02:55 +000038 def __init__(self):
Darryl Green10d9ce32018-02-28 10:02:55 +000039 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 Green10d9ce32018-02-28 10:02:55 +000047 def check_file_for_issue(self, filepath):
Gilles Peskine6ee576e2019-02-25 20:59:05 +010048 raise NotImplementedError
Darryl Green10d9ce32018-02-28 10:02:55 +000049
Gilles Peskine04398052018-11-23 21:11:30 +010050 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 Green10d9ce32018-02-28 10:02:55 +000055 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 Peskine6ee576e2019-02-25 20:59:05 +010067class LineIssueTracker(FileIssueTracker):
68 """Base class for line-by-line issue tracking.
Darryl Green10d9ce32018-02-28 10:02:55 +000069
Gilles Peskine6ee576e2019-02-25 20:59:05 +010070 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
86class PermissionIssueTracker(FileIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +010087 """Track files with bad permissions.
88
89 Files that are not executable scripts must not be executable."""
Darryl Green10d9ce32018-02-28 10:02:55 +000090
Gilles Peskine1e9698a2019-02-25 21:10:04 +010091 heading = "Incorrect permissions:"
Darryl Green10d9ce32018-02-28 10:02:55 +000092
93 def check_file_for_issue(self, filepath):
Gilles Peskine23e64f22019-02-25 21:24:27 +010094 is_executable = os.access(filepath, os.X_OK)
95 should_be_executable = filepath.endswith((".sh", ".pl", ".py"))
96 if is_executable != should_be_executable:
Darryl Green10d9ce32018-02-28 10:02:55 +000097 self.files_with_issues[filepath] = None
98
99
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100100class EndOfFileNewlineIssueTracker(FileIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100101 """Track files that end with an incomplete line
102 (no newline character at the end of the last line)."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000103
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100104 heading = "Missing newline at end of file:"
Darryl Green10d9ce32018-02-28 10:02:55 +0000105
106 def check_file_for_issue(self, filepath):
107 with open(filepath, "rb") as f:
108 if not f.read().endswith(b"\n"):
109 self.files_with_issues[filepath] = None
110
111
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100112class Utf8BomIssueTracker(FileIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100113 """Track files that start with a UTF-8 BOM.
114 Files should be ASCII or UTF-8. Valid UTF-8 does not start with a BOM."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000115
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100116 heading = "UTF-8 BOM present:"
Darryl Green10d9ce32018-02-28 10:02:55 +0000117
118 def check_file_for_issue(self, filepath):
119 with open(filepath, "rb") as f:
120 if f.read().startswith(codecs.BOM_UTF8):
121 self.files_with_issues[filepath] = None
122
123
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100124class LineEndingIssueTracker(LineIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100125 """Track files with non-Unix line endings (i.e. files with CR)."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000126
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100127 heading = "Non Unix line endings:"
Darryl Green10d9ce32018-02-28 10:02:55 +0000128
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100129 def issue_with_line(self, line, _filepath):
Darryl Green10d9ce32018-02-28 10:02:55 +0000130 return b"\r" in line
131
132
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100133class TrailingWhitespaceIssueTracker(LineIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100134 """Track lines with trailing whitespace."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000135
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100136 heading = "Trailing whitespace:"
137 files_exemptions = frozenset(".md")
Darryl Green10d9ce32018-02-28 10:02:55 +0000138
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100139 def issue_with_line(self, line, _filepath):
Darryl Green10d9ce32018-02-28 10:02:55 +0000140 return line.rstrip(b"\r\n") != line.rstrip()
141
142
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100143class TabIssueTracker(LineIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100144 """Track lines with tabs."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000145
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100146 heading = "Tabs present:"
147 files_exemptions = frozenset([
148 "Makefile",
149 "generate_visualc_files.pl",
150 ])
Darryl Green10d9ce32018-02-28 10:02:55 +0000151
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100152 def issue_with_line(self, line, _filepath):
Darryl Green10d9ce32018-02-28 10:02:55 +0000153 return b"\t" in line
154
155
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100156class MergeArtifactIssueTracker(LineIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100157 """Track lines with merge artifacts.
158 These are leftovers from a ``git merge`` that wasn't fully edited."""
Gilles Peskinec117d592018-11-23 21:11:52 +0100159
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100160 heading = "Merge artifact:"
Gilles Peskinec117d592018-11-23 21:11:52 +0100161
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100162 def issue_with_line(self, line, _filepath):
Gilles Peskinec117d592018-11-23 21:11:52 +0100163 # Detect leftover git conflict markers.
164 if line.startswith(b'<<<<<<< ') or line.startswith(b'>>>>>>> '):
165 return True
166 if line.startswith(b'||||||| '): # from merge.conflictStyle=diff3
167 return True
168 if line.rstrip(b'\r\n') == b'=======' and \
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100169 not _filepath.endswith('.md'):
Gilles Peskinec117d592018-11-23 21:11:52 +0100170 return True
171 return False
172
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100173class TodoIssueTracker(LineIssueTracker):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100174 """Track lines containing ``TODO``."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000175
Gilles Peskine1e9698a2019-02-25 21:10:04 +0100176 heading = "TODO present:"
177 files_exemptions = frozenset([
178 os.path.basename(__file__),
179 "benchmark.c",
180 "pull_request_template.md",
181 ])
Darryl Green10d9ce32018-02-28 10:02:55 +0000182
Gilles Peskine6ee576e2019-02-25 20:59:05 +0100183 def issue_with_line(self, line, _filepath):
Darryl Green10d9ce32018-02-28 10:02:55 +0000184 return b"todo" in line.lower()
185
186
187class IntegrityChecker(object):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100188 """Sanity-check files under the current directory."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000189
190 def __init__(self, log_file):
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100191 """Instantiate the sanity checker.
192 Check files under the current directory.
193 Write a report of issues to log_file."""
Darryl Green10d9ce32018-02-28 10:02:55 +0000194 self.check_repo_path()
195 self.logger = None
196 self.setup_logger(log_file)
197 self.files_to_check = (
198 ".c", ".h", ".sh", ".pl", ".py", ".md", ".function", ".data",
199 "Makefile", "CMakeLists.txt", "ChangeLog"
200 )
Gilles Peskine95c55752018-09-28 11:48:10 +0200201 self.excluded_directories = ['.git', 'mbed-os']
202 self.excluded_paths = list(map(os.path.normpath, [
203 'cov-int',
204 'examples',
Gilles Peskine95c55752018-09-28 11:48:10 +0200205 ]))
Darryl Green10d9ce32018-02-28 10:02:55 +0000206 self.issues_to_check = [
207 PermissionIssueTracker(),
208 EndOfFileNewlineIssueTracker(),
209 Utf8BomIssueTracker(),
210 LineEndingIssueTracker(),
211 TrailingWhitespaceIssueTracker(),
212 TabIssueTracker(),
Gilles Peskinec117d592018-11-23 21:11:52 +0100213 MergeArtifactIssueTracker(),
Darryl Green10d9ce32018-02-28 10:02:55 +0000214 TodoIssueTracker(),
215 ]
216
Gilles Peskine0d060ef2019-02-25 20:35:31 +0100217 @staticmethod
218 def check_repo_path():
Darryl Green10d9ce32018-02-28 10:02:55 +0000219 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
220 raise Exception("Must be run from Mbed TLS root")
221
222 def setup_logger(self, log_file, level=logging.INFO):
223 self.logger = logging.getLogger()
224 self.logger.setLevel(level)
225 if log_file:
226 handler = logging.FileHandler(log_file)
227 self.logger.addHandler(handler)
228 else:
229 console = logging.StreamHandler()
230 self.logger.addHandler(console)
231
Gilles Peskine95c55752018-09-28 11:48:10 +0200232 def prune_branch(self, root, d):
233 if d in self.excluded_directories:
234 return True
235 if os.path.normpath(os.path.join(root, d)) in self.excluded_paths:
236 return True
237 return False
238
Darryl Green10d9ce32018-02-28 10:02:55 +0000239 def check_files(self):
Gilles Peskine95c55752018-09-28 11:48:10 +0200240 for root, dirs, files in os.walk("."):
241 dirs[:] = sorted(d for d in dirs if not self.prune_branch(root, d))
Darryl Green10d9ce32018-02-28 10:02:55 +0000242 for filename in sorted(files):
243 filepath = os.path.join(root, filename)
Gilles Peskine95c55752018-09-28 11:48:10 +0200244 if not filepath.endswith(self.files_to_check):
Darryl Green10d9ce32018-02-28 10:02:55 +0000245 continue
246 for issue_to_check in self.issues_to_check:
247 if issue_to_check.should_check_file(filepath):
248 issue_to_check.check_file_for_issue(filepath)
249
250 def output_issues(self):
251 integrity_return_code = 0
252 for issue_to_check in self.issues_to_check:
253 if issue_to_check.files_with_issues:
254 integrity_return_code = 1
255 issue_to_check.output_file_issues(self.logger)
256 return integrity_return_code
257
258
259def run_main():
260 parser = argparse.ArgumentParser(
261 description=(
262 "This script checks the current state of the source code for "
263 "minor issues, including incorrect file permissions, "
264 "presence of tabs, non-Unix line endings, trailing whitespace, "
265 "presence of UTF-8 BOM, and TODO comments. "
266 "Note: requires python 3, must be run from Mbed TLS root."
267 )
268 )
269 parser.add_argument(
270 "-l", "--log_file", type=str, help="path to optional output log",
271 )
272 check_args = parser.parse_args()
273 integrity_check = IntegrityChecker(check_args.log_file)
274 integrity_check.check_files()
275 return_code = integrity_check.output_issues()
276 sys.exit(return_code)
277
278
279if __name__ == "__main__":
280 run_main()