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 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 5 | import ConfigParser |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 6 | import json |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 7 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 8 | import re |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 9 | import sys |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 10 | import subprocess |
| 11 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 12 | from errors import (VerifyException, HookFailure, PrintErrorForProject, |
| 13 | PrintErrorsForCommit) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 14 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 15 | |
| 16 | COMMON_INCLUDED_PATHS = [ |
| 17 | # C++ and friends |
| 18 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 19 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 20 | # Scripts |
| 21 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 22 | # No extension at all, note that ALL CAPS files are black listed in |
| 23 | # COMMON_EXCLUDED_LIST below. |
| 24 | r"(^|.*?[\\\/])[^.]+$", |
| 25 | # Other |
| 26 | r".*\.java$", r".*\.mk$", r".*\.am$", |
| 27 | ] |
| 28 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 29 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 30 | COMMON_EXCLUDED_PATHS = [ |
Ryan Cui | 31e0c17 | 2011-05-04 21:00:45 -0700 | [diff] [blame] | 31 | # avoid doing source file checks for kernel |
| 32 | r"/src/third_party/kernel/", |
| 33 | r"/src/third_party/kernel-next/", |
Paul Taysom | f8b6e01 | 2011-05-09 14:32:42 -0700 | [diff] [blame] | 34 | r"/src/third_party/ktop/", |
| 35 | r"/src/third_party/punybench/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 36 | r".*\bexperimental[\\\/].*", |
| 37 | r".*\b[A-Z0-9_]{2,}$", |
| 38 | r".*[\\\/]debian[\\\/]rules$", |
| 39 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 40 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 41 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 42 | _CONFIG_FILE = 'PRESUBMIT.cfg' |
| 43 | |
| 44 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 45 | # General Helpers |
| 46 | |
Sean Paul | ba01d40 | 2011-05-05 11:36:23 -0400 | [diff] [blame] | 47 | |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 48 | def _run_command(cmd): |
| 49 | """Executes the passed in command and returns raw stdout output.""" |
| 50 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 51 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 52 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 53 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 54 | """Returns the absolute path to the repohooks directory.""" |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 55 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 56 | return _run_command(cmd).strip() |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 57 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 58 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 59 | def _match_regex_list(subject, expressions): |
| 60 | """Try to match a list of regular expressions to a string. |
| 61 | |
| 62 | Args: |
| 63 | subject: The string to match regexes on |
| 64 | expressions: A list of regular expressions to check for matches with. |
| 65 | |
| 66 | Returns: |
| 67 | Whether the passed in subject matches any of the passed in regexes. |
| 68 | """ |
| 69 | for expr in expressions: |
| 70 | if (re.search(expr, subject)): |
| 71 | return True |
| 72 | return False |
| 73 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 74 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 75 | def _filter_files(files, include_list, exclude_list=[]): |
| 76 | """Filter out files based on the conditions passed in. |
| 77 | |
| 78 | Args: |
| 79 | files: list of filepaths to filter |
| 80 | include_list: list of regex that when matched with a file path will cause it |
| 81 | to be added to the output list unless the file is also matched with a |
| 82 | regex in the exclude_list. |
| 83 | exclude_list: list of regex that when matched with a file will prevent it |
| 84 | from being added to the output list, even if it is also matched with a |
| 85 | regex in the include_list. |
| 86 | |
| 87 | Returns: |
| 88 | A list of filepaths that contain files matched in the include_list and not |
| 89 | in the exclude_list. |
| 90 | """ |
| 91 | filtered = [] |
| 92 | for f in files: |
| 93 | if (_match_regex_list(f, include_list) and |
| 94 | not _match_regex_list(f, exclude_list)): |
| 95 | filtered.append(f) |
| 96 | return filtered |
| 97 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 98 | |
| 99 | # Git Helpers |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 100 | |
| 101 | |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 102 | def _get_upstream_branch(): |
| 103 | """Returns the upstream tracking branch of the current branch. |
| 104 | |
| 105 | Raises: |
| 106 | Error if there is no tracking branch |
| 107 | """ |
| 108 | current_branch = _run_command(['git', 'symbolic-ref', 'HEAD']).strip() |
| 109 | current_branch = current_branch.replace('refs/heads/', '') |
| 110 | if not current_branch: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 111 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 112 | |
| 113 | cfg_option = 'branch.' + current_branch + '.%s' |
| 114 | full_upstream = _run_command(['git', 'config', cfg_option % 'merge']).strip() |
| 115 | remote = _run_command(['git', 'config', cfg_option % 'remote']).strip() |
| 116 | if not remote or not full_upstream: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 117 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 118 | |
| 119 | return full_upstream.replace('heads', 'remotes/' + remote) |
| 120 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 121 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 122 | def _get_diff(commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 123 | """Returns the diff for this commit.""" |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 124 | return _run_command(['git', 'show', commit]) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 125 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 126 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 127 | def _get_file_diff(file, commit): |
| 128 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 129 | output = _run_command(['git', 'show', '-p', '--no-ext-diff', commit, file]) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 130 | |
| 131 | new_lines = [] |
| 132 | line_num = 0 |
| 133 | for line in output.splitlines(): |
| 134 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 135 | if m: |
| 136 | line_num = int(m.groups(1)[0]) |
| 137 | continue |
| 138 | if line.startswith('+') and not line.startswith('++'): |
| 139 | new_lines.append((line_num, line[1:])) |
| 140 | if not line.startswith('-'): |
| 141 | line_num += 1 |
| 142 | return new_lines |
| 143 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 144 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 145 | def _get_affected_files(commit): |
| 146 | """Returns list of absolute filepaths that were modified/added.""" |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 147 | output = _run_command(['git', 'diff', '--name-status', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 148 | files = [] |
| 149 | for statusline in output.splitlines(): |
| 150 | m = re.match('^(\w)+\t(.+)$', statusline.rstrip()) |
| 151 | # Ignore deleted files, and return absolute paths of files |
| 152 | if (m.group(1)[0] != 'D'): |
| 153 | pwd = os.getcwd() |
| 154 | files.append(os.path.join(pwd, m.group(2))) |
| 155 | return files |
| 156 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 157 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 158 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 159 | """Returns a list of commits for this review.""" |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 160 | cmd = ['git', 'log', '%s..' % _get_upstream_branch(), '--format=%H'] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 161 | return _run_command(cmd).split() |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 162 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 163 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 164 | def _get_commit_desc(commit): |
| 165 | """Returns the full commit message of a commit.""" |
Sean Paul | 23a2c58 | 2011-05-06 13:10:44 -0400 | [diff] [blame] | 166 | return _run_command(['git', 'log', '--format=%s%n%n%b', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | # Common Hooks |
| 170 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 171 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 172 | def _check_no_long_lines(project, commit): |
| 173 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 174 | the text files to be submitted. |
| 175 | """ |
| 176 | MAX_LEN = 80 |
| 177 | |
| 178 | errors = [] |
| 179 | files = _filter_files(_get_affected_files(commit), |
| 180 | COMMON_INCLUDED_PATHS, |
| 181 | COMMON_EXCLUDED_PATHS) |
| 182 | |
| 183 | for afile in files: |
| 184 | for line_num, line in _get_file_diff(afile, commit): |
| 185 | # Allow certain lines to exceed the maxlen rule. |
| 186 | if (len(line) > MAX_LEN and |
| 187 | not 'http://' in line and |
| 188 | not 'https://' in line and |
| 189 | not line.startswith('#define') and |
| 190 | not line.startswith('#include') and |
| 191 | not line.startswith('#import') and |
| 192 | not line.startswith('#pragma') and |
| 193 | not line.startswith('#if') and |
| 194 | not line.startswith('#endif')): |
| 195 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 196 | if len(errors) == 5: # Just show the first 5 errors. |
| 197 | break |
| 198 | |
| 199 | if errors: |
| 200 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 201 | return HookFailure(msg, errors) |
| 202 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 203 | |
| 204 | def _check_no_stray_whitespace(project, commit): |
| 205 | """Checks that there is no stray whitespace at source lines end.""" |
| 206 | errors = [] |
| 207 | files = _filter_files(_get_affected_files(commit), |
| 208 | COMMON_INCLUDED_PATHS, |
| 209 | COMMON_EXCLUDED_PATHS) |
| 210 | |
| 211 | for afile in files: |
| 212 | for line_num, line in _get_file_diff(afile, commit): |
| 213 | if line.rstrip() != line: |
| 214 | errors.append('%s, line %s' % (afile, line_num)) |
| 215 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 216 | return HookFailure('Found line ending with white space in:', errors) |
| 217 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 218 | |
| 219 | def _check_no_tabs(project, commit): |
| 220 | """Checks there are no unexpanded tabs.""" |
| 221 | TAB_OK_PATHS = [ |
Doug Anderson | 0f91cbf | 2011-05-09 15:56:25 -0700 | [diff] [blame] | 222 | r"/src/platform/u-boot-config/", |
Ryan Cui | 31e0c17 | 2011-05-04 21:00:45 -0700 | [diff] [blame] | 223 | r"/src/third_party/u-boot/", |
| 224 | r"/src/third_party/u-boot-next/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 225 | r".*\.ebuild$", |
| 226 | r".*\.eclass$", |
| 227 | r".*/[M|m]akefile$" |
| 228 | ] |
| 229 | |
| 230 | errors = [] |
| 231 | files = _filter_files(_get_affected_files(commit), |
| 232 | COMMON_INCLUDED_PATHS, |
| 233 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
| 234 | |
| 235 | for afile in files: |
| 236 | for line_num, line in _get_file_diff(afile, commit): |
| 237 | if '\t' in line: |
| 238 | errors.append('%s, line %s' % (afile, line_num)) |
| 239 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 240 | return HookFailure('Found a tab character in:', errors) |
| 241 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 242 | |
| 243 | def _check_change_has_test_field(project, commit): |
| 244 | """Check for a non-empty 'TEST=' field in the commit message.""" |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 245 | TEST_RE = r'\n\s*TEST\s*=[^\n]*\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 246 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 247 | if not re.search(TEST_RE, _get_commit_desc(commit)): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 248 | msg = 'Changelist description needs TEST field (after first line)' |
| 249 | return HookFailure(msg) |
| 250 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 251 | |
| 252 | def _check_change_has_bug_field(project, commit): |
| 253 | """Check for a non-empty 'BUG=' field in the commit message.""" |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 254 | BUG_RE = r'\n\s*BUG\s*=[^\n]*\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 255 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 256 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 257 | msg = 'Changelist description needs BUG field (after first line)' |
| 258 | return HookFailure(msg) |
| 259 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 260 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 261 | def _check_change_has_proper_changeid(project, commit): |
| 262 | """Verify that Change-ID is present in last paragraph of commit message.""" |
| 263 | desc = _get_commit_desc(commit) |
| 264 | loc = desc.rfind('\nChange-Id:') |
| 265 | if loc == -1 or re.search('\n\s*\n\s*\S+', desc[loc:]): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 266 | return HookFailure('Change-Id must be in last paragraph of description.') |
| 267 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 268 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 269 | def _check_license(project, commit): |
| 270 | """Verifies the license header.""" |
| 271 | LICENSE_HEADER = ( |
| 272 | r".*? Copyright \(c\) 20[-0-9]{2,7} The Chromium OS Authors\. All rights " |
| 273 | r"reserved\." "\n" |
| 274 | r".*? Use of this source code is governed by a BSD-style license that can " |
| 275 | "be\n" |
| 276 | r".*? found in the LICENSE file\." |
| 277 | "\n" |
| 278 | ) |
| 279 | |
| 280 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 281 | bad_files = [] |
| 282 | files = _filter_files(_get_affected_files(commit), |
| 283 | COMMON_INCLUDED_PATHS, |
| 284 | COMMON_EXCLUDED_PATHS) |
| 285 | |
| 286 | for f in files: |
| 287 | contents = open(f).read() |
| 288 | if len(contents) == 0: continue # Ignore empty files |
| 289 | if not license_re.search(contents): |
| 290 | bad_files.append(f) |
| 291 | if bad_files: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 292 | return HookFailure('License must match:\n%s\n' % license_re.pattern + |
| 293 | 'Found a bad license header in these files:', |
| 294 | bad_files) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 295 | |
| 296 | |
| 297 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 298 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 299 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 300 | def _run_checkpatch(project, commit): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 301 | """Runs checkpatch.pl on the given project""" |
| 302 | hooks_dir = _get_hooks_dir() |
| 303 | cmd = ['%s/checkpatch.pl' % hooks_dir, '-'] |
| 304 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 305 | output = p.communicate(_get_diff(commit))[0] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 306 | if p.returncode: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 307 | return HookFailure('checkpatch.pl errors/warnings\n\n' + output) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 308 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 309 | |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 310 | def _run_json_check(project, commit): |
| 311 | """Checks that all JSON files are syntactically valid.""" |
Dale Curtis | a039cfd | 2011-05-04 12:01:05 -0700 | [diff] [blame] | 312 | for f in _filter_files(_get_affected_files(commit), [r'.*\.json']): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 313 | try: |
| 314 | json.load(open(f)) |
| 315 | except Exception, e: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 316 | return HookFailure('Invalid JSON in %s: %s' % (f, e)) |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 317 | |
| 318 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 319 | # Base |
| 320 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 321 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 322 | # A list of hooks that are not project-specific |
| 323 | _COMMON_HOOKS = [ |
| 324 | _check_change_has_bug_field, |
| 325 | _check_change_has_test_field, |
| 326 | _check_change_has_proper_changeid, |
| 327 | _check_no_stray_whitespace, |
| 328 | _check_no_long_lines, |
| 329 | _check_license, |
| 330 | _check_no_tabs, |
| 331 | ] |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 332 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 333 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 334 | # A dictionary of project-specific hooks(callbacks), indexed by project name. |
| 335 | # dict[project] = [callback1, callback2] |
| 336 | _PROJECT_SPECIFIC_HOOKS = { |
Doug Anderson | 830216f | 2011-05-02 10:08:37 -0700 | [diff] [blame] | 337 | "chromiumos/third_party/kernel": [_run_checkpatch], |
| 338 | "chromiumos/third_party/kernel-next": [_run_checkpatch], |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 339 | "chromeos/autotest-tools": [_run_json_check], |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 340 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 341 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 342 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 343 | # A dictionary of flags (keys) that can appear in the config file, and the hook |
| 344 | # that the flag disables (value) |
| 345 | _DISABLE_FLAGS = { |
| 346 | 'stray_whitespace_check': _check_no_stray_whitespace, |
| 347 | 'long_line_check': _check_no_long_lines, |
| 348 | 'cros_license_check': _check_license, |
| 349 | 'tab_check': _check_no_tabs, |
| 350 | } |
| 351 | |
| 352 | |
| 353 | def _get_disabled_hooks(): |
| 354 | """Returns a set of hooks disabled by the current project's config file. |
| 355 | |
| 356 | Expects to be called within the project root. |
| 357 | """ |
| 358 | SECTION = 'Hook Overrides' |
| 359 | config = ConfigParser.RawConfigParser() |
| 360 | try: |
| 361 | config.read(_CONFIG_FILE) |
| 362 | flags = config.options(SECTION) |
| 363 | except ConfigParser.Error: |
| 364 | return set([]) |
| 365 | |
| 366 | disable_flags = [] |
| 367 | for flag in flags: |
| 368 | try: |
| 369 | if not config.getboolean(SECTION, flag): disable_flags.append(flag) |
| 370 | except ValueError as e: |
| 371 | msg = "Error parsing flag \'%s\' in %s file - " % (flag, _CONFIG_FILE) |
| 372 | print msg + str(e) |
| 373 | |
| 374 | disabled_keys = set(_DISABLE_FLAGS.iterkeys()).intersection(disable_flags) |
| 375 | return set([_DISABLE_FLAGS[key] for key in disabled_keys]) |
| 376 | |
| 377 | |
| 378 | def _get_project_hooks(project): |
| 379 | """Returns a list of hooks that need to be run for a project. |
| 380 | |
| 381 | Expects to be called from within the project root. |
| 382 | """ |
| 383 | disabled_hooks = _get_disabled_hooks() |
| 384 | hooks = [hook for hook in _COMMON_HOOKS if hook not in disabled_hooks] |
| 385 | |
| 386 | if project in _PROJECT_SPECIFIC_HOOKS: |
| 387 | hooks.extend(_PROJECT_SPECIFIC_HOOKS[project]) |
| 388 | |
| 389 | return hooks |
| 390 | |
| 391 | |
| 392 | def _run_project_hooks(project): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 393 | """For each project run its project specific hook from the hooks dictionary. |
| 394 | |
| 395 | Args: |
| 396 | project: name of project to run hooks for. |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 397 | |
| 398 | Returns: |
| 399 | Boolean value of whether any errors were ecountered while running the hooks. |
| 400 | """ |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 401 | proj_dir = _run_command(['repo', 'forall', project, '-c', 'pwd']).strip() |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 402 | pwd = os.getcwd() |
| 403 | # hooks assume they are run from the root of the project |
| 404 | os.chdir(proj_dir) |
| 405 | |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 406 | try: |
| 407 | commit_list = _get_commits() |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 408 | except VerifyException as e: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 409 | PrintErrorForProject(project, HookFailure(str(e))) |
| 410 | os.chdir(pwd) |
| 411 | return True |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 412 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 413 | hooks = _get_project_hooks(project) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 414 | error_found = False |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 415 | for commit in commit_list: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 416 | error_list = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 417 | for hook in hooks: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 418 | hook_error = hook(project, commit) |
| 419 | if hook_error: |
| 420 | error_list.append(hook_error) |
| 421 | error_found = True |
| 422 | if error_list: |
| 423 | PrintErrorsForCommit(project, commit, _get_commit_desc(commit), |
| 424 | error_list) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 425 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 426 | os.chdir(pwd) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 427 | return error_found |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 428 | |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 429 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 430 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 431 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 432 | |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 433 | def main(project_list, **kwargs): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 434 | found_error = False |
| 435 | for project in project_list: |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 436 | if _run_project_hooks(project): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 437 | found_error = True |
| 438 | |
| 439 | if (found_error): |
| 440 | msg = ('Preupload failed due to errors in project(s). HINTS:\n' |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 441 | '- To disable some source style checks, and for other hints, see ' |
| 442 | '<checkout_dir>/src/repohooks/README\n' |
| 443 | '- To upload only current project, run \'repo upload .\'') |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 444 | print >> sys.stderr, msg |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 445 | sys.exit(1) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 446 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 447 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 448 | if __name__ == '__main__': |
| 449 | main() |