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