Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 6 | import re |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame^] | 7 | import sys |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 8 | import subprocess |
| 9 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 10 | |
| 11 | # General Helpers |
| 12 | |
| 13 | COMMON_INCLUDED_PATHS = [ |
| 14 | # C++ and friends |
| 15 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 16 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 17 | # Scripts |
| 18 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 19 | # No extension at all, note that ALL CAPS files are black listed in |
| 20 | # COMMON_EXCLUDED_LIST below. |
| 21 | r"(^|.*?[\\\/])[^.]+$", |
| 22 | # Other |
| 23 | r".*\.java$", r".*\.mk$", r".*\.am$", |
| 24 | ] |
| 25 | |
| 26 | COMMON_EXCLUDED_PATHS = [ |
| 27 | # avoid doing source file checks for kernel |
| 28 | r"/src/third_party/kernel/", |
| 29 | r"/src/third_party/kernel-next/", |
| 30 | r".*\bexperimental[\\\/].*", |
| 31 | r".*\b[A-Z0-9_]{2,}$", |
| 32 | r".*[\\\/]debian[\\\/]rules$", |
| 33 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 34 | |
| 35 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 36 | """Returns the absolute path to the repohooks directory.""" |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 37 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
| 38 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip() |
| 39 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 40 | def _match_regex_list(subject, expressions): |
| 41 | """Try to match a list of regular expressions to a string. |
| 42 | |
| 43 | Args: |
| 44 | subject: The string to match regexes on |
| 45 | expressions: A list of regular expressions to check for matches with. |
| 46 | |
| 47 | Returns: |
| 48 | Whether the passed in subject matches any of the passed in regexes. |
| 49 | """ |
| 50 | for expr in expressions: |
| 51 | if (re.search(expr, subject)): |
| 52 | return True |
| 53 | return False |
| 54 | |
| 55 | def _filter_files(files, include_list, exclude_list=[]): |
| 56 | """Filter out files based on the conditions passed in. |
| 57 | |
| 58 | Args: |
| 59 | files: list of filepaths to filter |
| 60 | include_list: list of regex that when matched with a file path will cause it |
| 61 | to be added to the output list unless the file is also matched with a |
| 62 | regex in the exclude_list. |
| 63 | exclude_list: list of regex that when matched with a file will prevent it |
| 64 | from being added to the output list, even if it is also matched with a |
| 65 | regex in the include_list. |
| 66 | |
| 67 | Returns: |
| 68 | A list of filepaths that contain files matched in the include_list and not |
| 69 | in the exclude_list. |
| 70 | """ |
| 71 | filtered = [] |
| 72 | for f in files: |
| 73 | if (_match_regex_list(f, include_list) and |
| 74 | not _match_regex_list(f, exclude_list)): |
| 75 | filtered.append(f) |
| 76 | return filtered |
| 77 | |
| 78 | def _report_error(msg, items=None): |
| 79 | """Raises an exception with the passed in error message. |
| 80 | |
| 81 | If extra error detail is passed in, it will be appended to the error message. |
| 82 | |
| 83 | Args: |
| 84 | msg: Error message header. |
| 85 | items: A list of lines that follow the header that give extra error |
| 86 | information. |
| 87 | """ |
| 88 | if items: |
| 89 | msg += '\n' + '\n'.join(items) |
| 90 | raise Exception(msg) |
| 91 | |
| 92 | |
| 93 | # Git Helpers |
| 94 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 95 | def _get_diff(commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 96 | """Returns the diff for this commit.""" |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 97 | cmd = ['git', 'show', commit] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 98 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 99 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 100 | def _get_file_diff(file, commit): |
| 101 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
| 102 | cmd = ['git', 'show', '-p', '--no-ext-diff', commit, file] |
| 103 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 104 | |
| 105 | new_lines = [] |
| 106 | line_num = 0 |
| 107 | for line in output.splitlines(): |
| 108 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 109 | if m: |
| 110 | line_num = int(m.groups(1)[0]) |
| 111 | continue |
| 112 | if line.startswith('+') and not line.startswith('++'): |
| 113 | new_lines.append((line_num, line[1:])) |
| 114 | if not line.startswith('-'): |
| 115 | line_num += 1 |
| 116 | return new_lines |
| 117 | |
| 118 | def _get_affected_files(commit): |
| 119 | """Returns list of absolute filepaths that were modified/added.""" |
| 120 | cmd = ['git', 'diff', '--name-status', commit + '^!'] |
| 121 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 122 | files = [] |
| 123 | for statusline in output.splitlines(): |
| 124 | m = re.match('^(\w)+\t(.+)$', statusline.rstrip()) |
| 125 | # Ignore deleted files, and return absolute paths of files |
| 126 | if (m.group(1)[0] != 'D'): |
| 127 | pwd = os.getcwd() |
| 128 | files.append(os.path.join(pwd, m.group(2))) |
| 129 | return files |
| 130 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 131 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 132 | """Returns a list of commits for this review.""" |
| 133 | cmd = ['git', 'log', 'm/master..', '--format=%H'] |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 134 | commits = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 135 | return commits.split() |
| 136 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 137 | def _get_commit_desc(commit): |
| 138 | """Returns the full commit message of a commit.""" |
| 139 | cmd = ['git', 'log', '--format=%B', commit + '^!'] |
| 140 | description = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 141 | return description.splitlines() |
| 142 | |
| 143 | |
| 144 | # Common Hooks |
| 145 | |
| 146 | def _check_no_long_lines(project, commit): |
| 147 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 148 | the text files to be submitted. |
| 149 | """ |
| 150 | MAX_LEN = 80 |
| 151 | |
| 152 | errors = [] |
| 153 | files = _filter_files(_get_affected_files(commit), |
| 154 | COMMON_INCLUDED_PATHS, |
| 155 | COMMON_EXCLUDED_PATHS) |
| 156 | |
| 157 | for afile in files: |
| 158 | for line_num, line in _get_file_diff(afile, commit): |
| 159 | # Allow certain lines to exceed the maxlen rule. |
| 160 | if (len(line) > MAX_LEN and |
| 161 | not 'http://' in line and |
| 162 | not 'https://' in line and |
| 163 | not line.startswith('#define') and |
| 164 | not line.startswith('#include') and |
| 165 | not line.startswith('#import') and |
| 166 | not line.startswith('#pragma') and |
| 167 | not line.startswith('#if') and |
| 168 | not line.startswith('#endif')): |
| 169 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 170 | if len(errors) == 5: # Just show the first 5 errors. |
| 171 | break |
| 172 | |
| 173 | if errors: |
| 174 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
| 175 | _report_error(msg, errors) |
| 176 | |
| 177 | def _check_no_stray_whitespace(project, commit): |
| 178 | """Checks that there is no stray whitespace at source lines end.""" |
| 179 | errors = [] |
| 180 | files = _filter_files(_get_affected_files(commit), |
| 181 | COMMON_INCLUDED_PATHS, |
| 182 | COMMON_EXCLUDED_PATHS) |
| 183 | |
| 184 | for afile in files: |
| 185 | for line_num, line in _get_file_diff(afile, commit): |
| 186 | if line.rstrip() != line: |
| 187 | errors.append('%s, line %s' % (afile, line_num)) |
| 188 | if errors: |
| 189 | _report_error('Found line ending with white space in:', errors) |
| 190 | |
| 191 | def _check_no_tabs(project, commit): |
| 192 | """Checks there are no unexpanded tabs.""" |
| 193 | TAB_OK_PATHS = [ |
| 194 | r"/src/third_party/u-boot/", |
| 195 | r"/src/third_party/u-boot-next/", |
| 196 | r".*\.ebuild$", |
| 197 | r".*\.eclass$", |
| 198 | r".*/[M|m]akefile$" |
| 199 | ] |
| 200 | |
| 201 | errors = [] |
| 202 | files = _filter_files(_get_affected_files(commit), |
| 203 | COMMON_INCLUDED_PATHS, |
| 204 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
| 205 | |
| 206 | for afile in files: |
| 207 | for line_num, line in _get_file_diff(afile, commit): |
| 208 | if '\t' in line: |
| 209 | errors.append('%s, line %s' % (afile, line_num)) |
| 210 | if errors: |
| 211 | _report_error('Found a tab character in:', errors) |
| 212 | |
| 213 | def _check_change_has_test_field(project, commit): |
| 214 | """Check for a non-empty 'TEST=' field in the commit message.""" |
| 215 | TEST_RE = r'^\s*TEST\s*=\s*\S+.*$' |
| 216 | |
| 217 | found_field = False |
| 218 | for line in _get_commit_desc(commit): |
| 219 | |
| 220 | if re.match(TEST_RE, line): |
| 221 | found_field = True |
| 222 | break |
| 223 | |
| 224 | if not found_field: |
| 225 | _report_error('Changelist description needs TEST field') |
| 226 | |
| 227 | def _check_change_has_bug_field(project, commit): |
| 228 | """Check for a non-empty 'BUG=' field in the commit message.""" |
| 229 | BUG_RE = r'^\s*BUG\s*=\s*\S+.*$' |
| 230 | |
| 231 | found_field = False |
| 232 | for line in _get_commit_desc(commit): |
| 233 | if re.match(BUG_RE, line): |
| 234 | found_field = True |
| 235 | break |
| 236 | |
| 237 | if not found_field: |
| 238 | _report_error('Changelist description needs BUG field') |
| 239 | |
| 240 | def _check_license(project, commit): |
| 241 | """Verifies the license header.""" |
| 242 | LICENSE_HEADER = ( |
| 243 | r".*? Copyright \(c\) 20[-0-9]{2,7} The Chromium OS Authors\. All rights " |
| 244 | r"reserved\." "\n" |
| 245 | r".*? Use of this source code is governed by a BSD-style license that can " |
| 246 | "be\n" |
| 247 | r".*? found in the LICENSE file\." |
| 248 | "\n" |
| 249 | ) |
| 250 | |
| 251 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 252 | bad_files = [] |
| 253 | files = _filter_files(_get_affected_files(commit), |
| 254 | COMMON_INCLUDED_PATHS, |
| 255 | COMMON_EXCLUDED_PATHS) |
| 256 | |
| 257 | for f in files: |
| 258 | contents = open(f).read() |
| 259 | if len(contents) == 0: continue # Ignore empty files |
| 260 | if not license_re.search(contents): |
| 261 | bad_files.append(f) |
| 262 | if bad_files: |
| 263 | _report_error('License must match:\n%s\n' % license_re.pattern + |
| 264 | 'Found a bad license header in these files:', |
| 265 | bad_files) |
| 266 | |
| 267 | |
| 268 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 269 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 270 | def _run_checkpatch(project, commit): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 271 | """Runs checkpatch.pl on the given project""" |
| 272 | hooks_dir = _get_hooks_dir() |
| 273 | cmd = ['%s/checkpatch.pl' % hooks_dir, '-'] |
| 274 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 275 | output = p.communicate(_get_diff(commit))[0] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 276 | if p.returncode: |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 277 | _report_error('checkpatch.pl errors/warnings\n\n' + output) |
| 278 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 279 | |
| 280 | # Base |
| 281 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 282 | COMMON_HOOKS = [_check_no_long_lines, |
| 283 | _check_no_stray_whitespace, |
| 284 | _check_no_tabs, |
| 285 | _check_change_has_test_field, |
| 286 | _check_change_has_bug_field, |
| 287 | _check_license] |
| 288 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 289 | def _setup_project_hooks(): |
| 290 | """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]""" |
| 291 | return { |
Doug Anderson | 830216f | 2011-05-02 10:08:37 -0700 | [diff] [blame] | 292 | "chromiumos/third_party/kernel": [_run_checkpatch], |
| 293 | "chromiumos/third_party/kernel-next": [_run_checkpatch], |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | def _run_project_hooks(project, hooks): |
| 297 | """For each project run its project specific hook from the hooks dictionary""" |
| 298 | cmd = ['repo', 'forall', project, '-c', 'pwd'] |
| 299 | proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 300 | proj_dir = proj_dir.strip() |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 301 | pwd = os.getcwd() |
| 302 | # hooks assume they are run from the root of the project |
| 303 | os.chdir(proj_dir) |
| 304 | |
| 305 | project_specific_hooks = [] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 306 | if project in hooks: |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 307 | project_specific_hooks = hooks[project] |
| 308 | |
| 309 | for commit in _get_commits(): |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame^] | 310 | try: |
| 311 | for hook in COMMON_HOOKS + project_specific_hooks: |
| 312 | hook(project, commit) |
| 313 | except: |
| 314 | msg = 'ERROR: pre-upload failed: commit=%s, project=%s' % (commit[:8], |
| 315 | project) |
| 316 | print >> sys.stderr, msg |
| 317 | raise |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 318 | os.chdir(pwd) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 319 | |
| 320 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 321 | |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 322 | def main(project_list, **kwargs): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 323 | hooks = _setup_project_hooks() |
| 324 | for project in project_list: |
| 325 | _run_project_hooks(project, hooks) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 326 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 327 | if __name__ == '__main__': |
| 328 | main() |