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