Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 6 | """Presubmit checks to run when doing `repo upload`. |
| 7 | |
| 8 | You can add new checks by adding a functions to the HOOKS constants. |
| 9 | """ |
| 10 | |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 11 | from __future__ import print_function |
| 12 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 13 | import ConfigParser |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 14 | import functools |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 15 | import json |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 16 | import optparse |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 17 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 18 | import re |
David Hendricks | 4c018e7 | 2013-02-06 13:46:38 -0800 | [diff] [blame] | 19 | import socket |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 20 | import sys |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 21 | import subprocess |
| 22 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 23 | from errors import (VerifyException, HookFailure, PrintErrorForProject, |
| 24 | PrintErrorsForCommit) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 25 | |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 26 | # If repo imports us, the __name__ will be __builtin__, and the wrapper will |
| 27 | # be in $CHROMEOS_CHECKOUT/.repo/repo/main.py, so we need to go two directories |
| 28 | # up. The same logic also happens to work if we're executed directly. |
| 29 | if __name__ in ('__builtin__', '__main__'): |
| 30 | sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', '..')) |
| 31 | |
| 32 | from chromite.lib import patch |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 33 | from chromite.licensing import licenses |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 34 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 35 | |
| 36 | COMMON_INCLUDED_PATHS = [ |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 37 | # C++ and friends |
| 38 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 39 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 40 | # Scripts |
| 41 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 42 | # No extension at all, note that ALL CAPS files are black listed in |
| 43 | # COMMON_EXCLUDED_LIST below. |
| 44 | r"(^|.*[\\\/])[^.]+$", |
| 45 | # Other |
| 46 | r".*\.java$", r".*\.mk$", r".*\.am$", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 47 | ] |
| 48 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 49 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 50 | COMMON_EXCLUDED_PATHS = [ |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 51 | # avoid doing source file checks for kernel |
| 52 | r"/src/third_party/kernel/", |
| 53 | r"/src/third_party/kernel-next/", |
| 54 | r"/src/third_party/ktop/", |
| 55 | r"/src/third_party/punybench/", |
| 56 | r".*\bexperimental[\\\/].*", |
| 57 | r".*\b[A-Z0-9_]{2,}$", |
| 58 | r".*[\\\/]debian[\\\/]rules$", |
| 59 | # for ebuild trees, ignore any caches and manifest data |
| 60 | r".*/Manifest$", |
| 61 | r".*/metadata/[^/]*cache[^/]*/[^/]+/[^/]+$", |
Doug Anderson | 5bfb679 | 2011-10-25 16:45:41 -0700 | [diff] [blame] | 62 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 63 | # ignore profiles data (like overlay-tegra2/profiles) |
| 64 | r".*/overlay-.*/profiles/.*", |
| 65 | # ignore minified js and jquery |
| 66 | r".*\.min\.js", |
| 67 | r".*jquery.*\.js", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 68 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 69 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 70 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 71 | _CONFIG_FILE = 'PRESUBMIT.cfg' |
| 72 | |
| 73 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 74 | # Exceptions |
| 75 | |
| 76 | |
| 77 | class BadInvocation(Exception): |
| 78 | """An Exception indicating a bad invocation of the program.""" |
| 79 | pass |
| 80 | |
| 81 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 82 | # General Helpers |
| 83 | |
Sean Paul | ba01d40 | 2011-05-05 11:36:23 -0400 | [diff] [blame] | 84 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 85 | def _run_command(cmd, cwd=None, stderr=None): |
| 86 | """Executes the passed in command and returns raw stdout output. |
| 87 | |
| 88 | Args: |
| 89 | cmd: The command to run; should be a list of strings. |
| 90 | cwd: The directory to switch to for running the command. |
| 91 | stderr: Can be one of None (print stderr to console), subprocess.STDOUT |
| 92 | (combine stderr with stdout), or subprocess.PIPE (ignore stderr). |
| 93 | |
| 94 | Returns: |
| 95 | The standard out from the process. |
| 96 | """ |
| 97 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=stderr, cwd=cwd) |
| 98 | return p.communicate()[0] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 99 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 100 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 101 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 102 | """Returns the absolute path to the repohooks directory.""" |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 103 | if __name__ == '__main__': |
| 104 | # Works when file is run on its own (__file__ is defined)... |
| 105 | return os.path.abspath(os.path.dirname(__file__)) |
| 106 | else: |
| 107 | # We need to do this when we're run through repo. Since repo executes |
| 108 | # us with execfile(), we don't get __file__ defined. |
| 109 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
| 110 | return _run_command(cmd).strip() |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 111 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 112 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 113 | def _match_regex_list(subject, expressions): |
| 114 | """Try to match a list of regular expressions to a string. |
| 115 | |
| 116 | Args: |
| 117 | subject: The string to match regexes on |
| 118 | expressions: A list of regular expressions to check for matches with. |
| 119 | |
| 120 | Returns: |
| 121 | Whether the passed in subject matches any of the passed in regexes. |
| 122 | """ |
| 123 | for expr in expressions: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 124 | if re.search(expr, subject): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 125 | return True |
| 126 | return False |
| 127 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 128 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 129 | def _filter_files(files, include_list, exclude_list=()): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 130 | """Filter out files based on the conditions passed in. |
| 131 | |
| 132 | Args: |
| 133 | files: list of filepaths to filter |
| 134 | include_list: list of regex that when matched with a file path will cause it |
| 135 | to be added to the output list unless the file is also matched with a |
| 136 | regex in the exclude_list. |
| 137 | exclude_list: list of regex that when matched with a file will prevent it |
| 138 | from being added to the output list, even if it is also matched with a |
| 139 | regex in the include_list. |
| 140 | |
| 141 | Returns: |
| 142 | A list of filepaths that contain files matched in the include_list and not |
| 143 | in the exclude_list. |
| 144 | """ |
| 145 | filtered = [] |
| 146 | for f in files: |
| 147 | if (_match_regex_list(f, include_list) and |
| 148 | not _match_regex_list(f, exclude_list)): |
| 149 | filtered.append(f) |
| 150 | return filtered |
| 151 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 152 | |
David Hendricks | 35030d0 | 2013-02-04 17:49:16 -0800 | [diff] [blame] | 153 | def _verify_header_content(commit, content, fail_msg): |
| 154 | """Verify that file headers contain specified content. |
| 155 | |
| 156 | Args: |
| 157 | commit: the affected commit. |
| 158 | content: the content of the header to be verified. |
| 159 | fail_msg: the first message to display in case of failure. |
| 160 | |
| 161 | Returns: |
| 162 | The return value of HookFailure(). |
| 163 | """ |
| 164 | license_re = re.compile(content, re.MULTILINE) |
| 165 | bad_files = [] |
| 166 | files = _filter_files(_get_affected_files(commit), |
| 167 | COMMON_INCLUDED_PATHS, |
| 168 | COMMON_EXCLUDED_PATHS) |
| 169 | |
| 170 | for f in files: |
Tom Wai-Hong Tam | 667db5d | 2014-02-27 06:28:14 +0800 | [diff] [blame] | 171 | # Ignore non-existant files and symlinks |
| 172 | if os.path.exists(f) and not os.path.islink(f): |
Gabe Black | cf3c32c | 2013-02-27 00:26:13 -0800 | [diff] [blame] | 173 | contents = open(f).read() |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 174 | if not contents: |
| 175 | # Ignore empty files |
| 176 | continue |
Gabe Black | cf3c32c | 2013-02-27 00:26:13 -0800 | [diff] [blame] | 177 | if not license_re.search(contents): |
| 178 | bad_files.append(f) |
David Hendricks | 35030d0 | 2013-02-04 17:49:16 -0800 | [diff] [blame] | 179 | if bad_files: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 180 | msg = "%s:\n%s\n%s" % (fail_msg, license_re.pattern, |
| 181 | "Found a bad header in these files:") |
| 182 | return HookFailure(msg, bad_files) |
David Hendricks | 35030d0 | 2013-02-04 17:49:16 -0800 | [diff] [blame] | 183 | |
| 184 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 185 | # Git Helpers |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 186 | |
| 187 | |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 188 | def _get_upstream_branch(): |
| 189 | """Returns the upstream tracking branch of the current branch. |
| 190 | |
| 191 | Raises: |
| 192 | Error if there is no tracking branch |
| 193 | """ |
| 194 | current_branch = _run_command(['git', 'symbolic-ref', 'HEAD']).strip() |
| 195 | current_branch = current_branch.replace('refs/heads/', '') |
| 196 | if not current_branch: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 197 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 198 | |
| 199 | cfg_option = 'branch.' + current_branch + '.%s' |
| 200 | full_upstream = _run_command(['git', 'config', cfg_option % 'merge']).strip() |
| 201 | remote = _run_command(['git', 'config', cfg_option % 'remote']).strip() |
| 202 | if not remote or not full_upstream: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 203 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 204 | |
| 205 | return full_upstream.replace('heads', 'remotes/' + remote) |
| 206 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 207 | |
Che-Liang Chiou | 5ce2d7b | 2013-03-22 18:47:55 -0700 | [diff] [blame] | 208 | def _get_patch(commit): |
| 209 | """Returns the patch for this commit.""" |
| 210 | return _run_command(['git', 'format-patch', '--stdout', '-1', commit]) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 211 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 212 | |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 213 | def _try_utf8_decode(data): |
| 214 | """Attempts to decode a string as UTF-8. |
| 215 | |
| 216 | Returns: |
| 217 | The decoded Unicode object, or the original string if parsing fails. |
| 218 | """ |
| 219 | try: |
| 220 | return unicode(data, 'utf-8', 'strict') |
| 221 | except UnicodeDecodeError: |
| 222 | return data |
| 223 | |
| 224 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 225 | def _get_file_content(path, commit): |
| 226 | """Returns the content of a file at a specific commit. |
| 227 | |
| 228 | We can't rely on the file as it exists in the filesystem as people might be |
| 229 | uploading a series of changes which modifies the file multiple times. |
| 230 | |
| 231 | Note: The "content" of a symlink is just the target. So if you're expecting |
| 232 | a full file, you should check that first. One way to detect is that the |
| 233 | content will not have any newlines. |
| 234 | """ |
| 235 | return _run_command(['git', 'show', '%s:%s' % (commit, path)]) |
| 236 | |
| 237 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 238 | def _get_file_diff(path, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 239 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
Mike Frysinger | 5122a1f | 2014-02-01 02:47:35 -0500 | [diff] [blame] | 240 | output = _run_command(['git', 'show', '-p', '--pretty=format:', |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 241 | '--no-ext-diff', commit, path]) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 242 | |
| 243 | new_lines = [] |
| 244 | line_num = 0 |
| 245 | for line in output.splitlines(): |
| 246 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 247 | if m: |
| 248 | line_num = int(m.groups(1)[0]) |
| 249 | continue |
| 250 | if line.startswith('+') and not line.startswith('++'): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 251 | new_lines.append((line_num, _try_utf8_decode(line[1:]))) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 252 | if not line.startswith('-'): |
| 253 | line_num += 1 |
| 254 | return new_lines |
| 255 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 256 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 257 | def _get_affected_files(commit, include_deletes=False, relative=False): |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 258 | """Returns list of absolute filepaths that were modified/added. |
| 259 | |
| 260 | Args: |
| 261 | commit: The commit |
| 262 | include_deletes: If true we'll include delete in the list. |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 263 | relative: Whether to return full paths to files. |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 264 | |
| 265 | Returns: |
| 266 | A list of modified/added (and perhaps deleted) files |
| 267 | """ |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 268 | output = _run_command(['git', 'diff', '--name-status', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 269 | files = [] |
| 270 | for statusline in output.splitlines(): |
| 271 | m = re.match('^(\w)+\t(.+)$', statusline.rstrip()) |
| 272 | # Ignore deleted files, and return absolute paths of files |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 273 | if include_deletes or m.group(1)[0] != 'D': |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 274 | f = m.group(2) |
| 275 | if not relative: |
| 276 | pwd = os.getcwd() |
| 277 | f = os.path.join(pwd, f) |
| 278 | files.append(f) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 279 | return files |
| 280 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 281 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 282 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 283 | """Returns a list of commits for this review.""" |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 284 | cmd = ['git', 'log', '%s..' % _get_upstream_branch(), '--format=%H'] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 285 | return _run_command(cmd).split() |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 286 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 287 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 288 | def _get_commit_desc(commit): |
| 289 | """Returns the full commit message of a commit.""" |
Sean Paul | 23a2c58 | 2011-05-06 13:10:44 -0400 | [diff] [blame] | 290 | return _run_command(['git', 'log', '--format=%s%n%n%b', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 291 | |
| 292 | |
| 293 | # Common Hooks |
| 294 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 295 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 296 | def _check_no_long_lines(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 297 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 298 | the text files to be submitted. |
| 299 | """ |
| 300 | MAX_LEN = 80 |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 301 | SKIP_REGEXP = re.compile('|'.join([ |
| 302 | r'https?://', |
| 303 | r'^#\s*(define|include|import|pragma|if|endif)\b'])) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 304 | |
| 305 | errors = [] |
| 306 | files = _filter_files(_get_affected_files(commit), |
| 307 | COMMON_INCLUDED_PATHS, |
| 308 | COMMON_EXCLUDED_PATHS) |
| 309 | |
| 310 | for afile in files: |
| 311 | for line_num, line in _get_file_diff(afile, commit): |
| 312 | # Allow certain lines to exceed the maxlen rule. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 313 | if len(line) <= MAX_LEN or SKIP_REGEXP.search(line): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 314 | continue |
| 315 | |
| 316 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 317 | if len(errors) == 5: # Just show the first 5 errors. |
| 318 | break |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 319 | |
| 320 | if errors: |
| 321 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 322 | return HookFailure(msg, errors) |
| 323 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 324 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 325 | def _check_no_stray_whitespace(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 326 | """Checks that there is no stray whitespace at source lines end.""" |
| 327 | errors = [] |
| 328 | files = _filter_files(_get_affected_files(commit), |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 329 | COMMON_INCLUDED_PATHS, |
| 330 | COMMON_EXCLUDED_PATHS) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 331 | |
| 332 | for afile in files: |
| 333 | for line_num, line in _get_file_diff(afile, commit): |
| 334 | if line.rstrip() != line: |
| 335 | errors.append('%s, line %s' % (afile, line_num)) |
| 336 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 337 | return HookFailure('Found line ending with white space in:', errors) |
| 338 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 339 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 340 | def _check_no_tabs(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 341 | """Checks there are no unexpanded tabs.""" |
| 342 | TAB_OK_PATHS = [ |
Ryan Cui | 31e0c17 | 2011-05-04 21:00:45 -0700 | [diff] [blame] | 343 | r"/src/third_party/u-boot/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 344 | r".*\.ebuild$", |
| 345 | r".*\.eclass$", |
Elly Jones | 5ab3419 | 2011-11-15 14:57:06 -0500 | [diff] [blame] | 346 | r".*/[M|m]akefile$", |
| 347 | r".*\.mk$" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 348 | ] |
| 349 | |
| 350 | errors = [] |
| 351 | files = _filter_files(_get_affected_files(commit), |
| 352 | COMMON_INCLUDED_PATHS, |
| 353 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
| 354 | |
| 355 | for afile in files: |
| 356 | for line_num, line in _get_file_diff(afile, commit): |
| 357 | if '\t' in line: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 358 | errors.append('%s, line %s' % (afile, line_num)) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 359 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 360 | return HookFailure('Found a tab character in:', errors) |
| 361 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 362 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 363 | def _check_change_has_test_field(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 364 | """Check for a non-empty 'TEST=' field in the commit message.""" |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 365 | TEST_RE = r'\nTEST=\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 366 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 367 | if not re.search(TEST_RE, _get_commit_desc(commit)): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 368 | msg = 'Changelist description needs TEST field (after first line)' |
| 369 | return HookFailure(msg) |
| 370 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 371 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 372 | def _check_change_has_valid_cq_depend(_project, commit): |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 373 | """Check for a correctly formatted CQ-DEPEND field in the commit message.""" |
| 374 | msg = 'Changelist has invalid CQ-DEPEND target.' |
| 375 | example = 'Example: CQ-DEPEND=CL:1234, CL:2345' |
| 376 | try: |
| 377 | patch.GetPaladinDeps(_get_commit_desc(commit)) |
| 378 | except ValueError as ex: |
| 379 | return HookFailure(msg, [example, str(ex)]) |
| 380 | |
| 381 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 382 | def _check_change_has_bug_field(_project, commit): |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 383 | """Check for a correctly formatted 'BUG=' field in the commit message.""" |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 384 | OLD_BUG_RE = r'\nBUG=.*chromium-os' |
| 385 | if re.search(OLD_BUG_RE, _get_commit_desc(commit)): |
| 386 | msg = ('The chromium-os bug tracker is now deprecated. Please use\n' |
| 387 | 'the chromium tracker in your BUG= line now.') |
| 388 | return HookFailure(msg) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 389 | |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 390 | BUG_RE = r'\nBUG=([Nn]one|(chrome-os-partner|chromium):\d+)' |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 391 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 392 | msg = ('Changelist description needs BUG field (after first line):\n' |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 393 | 'BUG=chromium:9999 (for public tracker)\n' |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 394 | 'BUG=chrome-os-partner:9999 (for partner tracker)\n' |
| 395 | 'BUG=None') |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 396 | return HookFailure(msg) |
| 397 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 398 | |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 399 | def _check_for_uprev(project, commit): |
| 400 | """Check that we're not missing a revbump of an ebuild in the given commit. |
| 401 | |
| 402 | If the given commit touches files in a directory that has ebuilds somewhere |
| 403 | up the directory hierarchy, it's very likely that we need an ebuild revbump |
| 404 | in order for those changes to take effect. |
| 405 | |
| 406 | It's not totally trivial to detect a revbump, so at least detect that an |
| 407 | ebuild with a revision number in it was touched. This should handle the |
| 408 | common case where we use a symlink to do the revbump. |
| 409 | |
| 410 | TODO: it would be nice to enhance this hook to: |
| 411 | * Handle cases where people revbump with a slightly different syntax. I see |
| 412 | one ebuild (puppy) that revbumps with _pN. This is a false positive. |
| 413 | * Catches cases where people aren't using symlinks for revbumps. If they |
| 414 | edit a revisioned file directly (and are expected to rename it for revbump) |
| 415 | we'll miss that. Perhaps we could detect that the file touched is a |
| 416 | symlink? |
| 417 | |
| 418 | If a project doesn't use symlinks we'll potentially miss a revbump, but we're |
| 419 | still better off than without this check. |
| 420 | |
| 421 | Args: |
| 422 | project: The project to look at |
| 423 | commit: The commit to look at |
| 424 | |
| 425 | Returns: |
| 426 | A HookFailure or None. |
| 427 | """ |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 428 | # If this is the portage-stable overlay, then ignore the check. It's rare |
| 429 | # that we're doing anything other than importing files from upstream, so |
| 430 | # forcing a rev bump makes no sense. |
| 431 | whitelist = ( |
| 432 | 'chromiumos/overlays/portage-stable', |
| 433 | ) |
| 434 | if project in whitelist: |
| 435 | return None |
| 436 | |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 437 | affected_paths = _get_affected_files(commit, include_deletes=True) |
| 438 | |
| 439 | # Don't yell about changes to whitelisted files... |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 440 | whitelist = ('ChangeLog', 'Manifest', 'metadata.xml') |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 441 | affected_paths = [path for path in affected_paths |
| 442 | if os.path.basename(path) not in whitelist] |
| 443 | if not affected_paths: |
| 444 | return None |
| 445 | |
| 446 | # If we've touched any file named with a -rN.ebuild then we'll say we're |
| 447 | # OK right away. See TODO above about enhancing this. |
| 448 | touched_revved_ebuild = any(re.search(r'-r\d*\.ebuild$', path) |
| 449 | for path in affected_paths) |
| 450 | if touched_revved_ebuild: |
| 451 | return None |
| 452 | |
| 453 | # We want to examine the current contents of all directories that are parents |
| 454 | # of files that were touched (up to the top of the project). |
| 455 | # |
| 456 | # ...note: we use the current directory contents even though it may have |
| 457 | # changed since the commit we're looking at. This is just a heuristic after |
| 458 | # all. Worst case we don't flag a missing revbump. |
| 459 | project_top = os.getcwd() |
| 460 | dirs_to_check = set([project_top]) |
| 461 | for path in affected_paths: |
| 462 | path = os.path.dirname(path) |
| 463 | while os.path.exists(path) and not os.path.samefile(path, project_top): |
| 464 | dirs_to_check.add(path) |
| 465 | path = os.path.dirname(path) |
| 466 | |
| 467 | # Look through each directory. If it's got an ebuild in it then we'll |
| 468 | # consider this as a case when we need a revbump. |
| 469 | for dir_path in dirs_to_check: |
| 470 | contents = os.listdir(dir_path) |
| 471 | ebuilds = [os.path.join(dir_path, path) |
| 472 | for path in contents if path.endswith('.ebuild')] |
| 473 | ebuilds_9999 = [path for path in ebuilds if path.endswith('-9999.ebuild')] |
| 474 | |
| 475 | # If the -9999.ebuild file was touched the bot will uprev for us. |
| 476 | # ...we'll use a simple intersection here as a heuristic... |
| 477 | if set(ebuilds_9999) & set(affected_paths): |
| 478 | continue |
| 479 | |
| 480 | if ebuilds: |
| 481 | return HookFailure('Changelist probably needs a revbump of an ebuild\n' |
| 482 | 'or a -r1.ebuild symlink if this is a new ebuild') |
| 483 | |
| 484 | return None |
| 485 | |
| 486 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 487 | def _check_ebuild_eapi(project, commit): |
| 488 | """Make sure we have people use EAPI=4 or newer with custom ebuilds. |
| 489 | |
| 490 | We want to get away from older EAPI's as it makes life confusing and they |
| 491 | have less builtin error checking. |
| 492 | |
| 493 | Args: |
| 494 | project: The project to look at |
| 495 | commit: The commit to look at |
| 496 | |
| 497 | Returns: |
| 498 | A HookFailure or None. |
| 499 | """ |
| 500 | # If this is the portage-stable overlay, then ignore the check. It's rare |
Mike Frysinger | cd6adfc | 2014-02-06 01:03:56 -0500 | [diff] [blame] | 501 | # that we're doing anything other than importing files from upstream, and |
| 502 | # we shouldn't be rewriting things fundamentally anyways. |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 503 | whitelist = ( |
| 504 | 'chromiumos/overlays/portage-stable', |
| 505 | ) |
| 506 | if project in whitelist: |
| 507 | return None |
| 508 | |
| 509 | BAD_EAPIS = ('0', '1', '2', '3') |
| 510 | |
| 511 | get_eapi = re.compile(r'^\s*EAPI=[\'"]?([^\'"]+)') |
| 512 | |
| 513 | ebuilds_re = [r'\.ebuild$'] |
| 514 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 515 | ebuilds_re) |
| 516 | bad_ebuilds = [] |
| 517 | |
| 518 | for ebuild in ebuilds: |
| 519 | # If the ebuild does not specify an EAPI, it defaults to 0. |
| 520 | eapi = '0' |
| 521 | |
| 522 | lines = _get_file_content(ebuild, commit).splitlines() |
| 523 | if len(lines) == 1: |
| 524 | # This is most likely a symlink, so skip it entirely. |
| 525 | continue |
| 526 | |
| 527 | for line in lines: |
| 528 | m = get_eapi.match(line) |
| 529 | if m: |
| 530 | # Once we hit the first EAPI line in this ebuild, stop processing. |
| 531 | # The spec requires that there only be one and it be first, so |
| 532 | # checking all possible values is pointless. We also assume that |
| 533 | # it's "the" EAPI line and not something in the middle of a heredoc. |
| 534 | eapi = m.group(1) |
| 535 | break |
| 536 | |
| 537 | if eapi in BAD_EAPIS: |
| 538 | bad_ebuilds.append((ebuild, eapi)) |
| 539 | |
| 540 | if bad_ebuilds: |
| 541 | # pylint: disable=C0301 |
| 542 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/upgrade-ebuild-eapis' |
| 543 | # pylint: enable=C0301 |
| 544 | return HookFailure( |
Mike Frysinger | cd6adfc | 2014-02-06 01:03:56 -0500 | [diff] [blame] | 545 | 'These ebuilds are using old EAPIs. If these are imported from\n' |
| 546 | 'Gentoo, then you may ignore and upload once with the --no-verify\n' |
| 547 | 'flag. Otherwise, please update to 4 or newer.\n' |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 548 | '\t%s\n' |
| 549 | 'See this guide for more details:\n%s\n' % |
| 550 | ('\n\t'.join(['%s: EAPI=%s' % x for x in bad_ebuilds]), url)) |
| 551 | |
| 552 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 553 | def _check_ebuild_keywords(_project, commit): |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 554 | """Make sure we use the new style KEYWORDS when possible in ebuilds. |
| 555 | |
| 556 | If an ebuild generally does not care about the arch it is running on, then |
| 557 | ebuilds should flag it with one of: |
| 558 | KEYWORDS="*" # A stable ebuild. |
| 559 | KEYWORDS="~*" # An unstable ebuild. |
| 560 | KEYWORDS="-* ..." # Is known to only work on specific arches. |
| 561 | |
| 562 | Args: |
| 563 | project: The project to look at |
| 564 | commit: The commit to look at |
| 565 | |
| 566 | Returns: |
| 567 | A HookFailure or None. |
| 568 | """ |
| 569 | WHITELIST = set(('*', '-*', '~*')) |
| 570 | |
| 571 | get_keywords = re.compile(r'^\s*KEYWORDS="(.*)"') |
| 572 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 573 | ebuilds_re = [r'\.ebuild$'] |
| 574 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 575 | ebuilds_re) |
| 576 | |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 577 | for ebuild in ebuilds: |
| 578 | for _, line in _get_file_diff(ebuild, commit): |
| 579 | m = get_keywords.match(line) |
| 580 | if m: |
| 581 | keywords = set(m.group(1).split()) |
| 582 | if not keywords or WHITELIST - keywords != WHITELIST: |
| 583 | continue |
| 584 | |
| 585 | return HookFailure( |
| 586 | 'Please update KEYWORDS to use a glob:\n' |
| 587 | 'If the ebuild should be marked stable (normal for non-9999 ' |
| 588 | 'ebuilds):\n' |
| 589 | ' KEYWORDS="*"\n' |
| 590 | 'If the ebuild should be marked unstable (normal for ' |
| 591 | 'cros-workon / 9999 ebuilds):\n' |
| 592 | ' KEYWORDS="~*"\n' |
| 593 | 'If the ebuild needs to be marked for only specific arches,' |
| 594 | 'then use -* like so:\n' |
| 595 | ' KEYWORDS="-* arm ..."\n') |
| 596 | |
| 597 | |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 598 | def _check_ebuild_licenses(_project, commit): |
| 599 | """Check if the LICENSE field in the ebuild is correct.""" |
| 600 | affected_paths = _get_affected_files(commit) |
| 601 | touched_ebuilds = [x for x in affected_paths if x.endswith('.ebuild')] |
| 602 | |
| 603 | # A list of licenses to ignore for now. |
Yu-Ju Hong | c0963fa | 2014-03-03 12:36:52 -0800 | [diff] [blame^] | 604 | LICENSES_IGNORE = ['||', '(', ')'] |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 605 | |
| 606 | for ebuild in touched_ebuilds: |
| 607 | # Skip virutal packages. |
| 608 | if ebuild.split('/')[-3] == 'virtual': |
| 609 | continue |
| 610 | |
| 611 | try: |
| 612 | license_types = licenses.GetLicenseTypesFromEbuild(ebuild) |
| 613 | except ValueError as e: |
| 614 | return HookFailure(e.message, [ebuild]) |
| 615 | |
| 616 | # Also ignore licenses ending with '?' |
| 617 | for license_type in [x for x in license_types |
| 618 | if x not in LICENSES_IGNORE and not x.endswith('?')]: |
| 619 | try: |
| 620 | licenses.Licensing.FindLicenseType(license_type) |
| 621 | except AssertionError as e: |
| 622 | return HookFailure(e.message, [ebuild]) |
| 623 | |
| 624 | |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 625 | def _check_ebuild_virtual_pv(project, commit): |
| 626 | """Enforce the virtual PV policies.""" |
| 627 | # If this is the portage-stable overlay, then ignore the check. |
| 628 | # We want to import virtuals as-is from upstream Gentoo. |
| 629 | whitelist = ( |
| 630 | 'chromiumos/overlays/portage-stable', |
| 631 | ) |
| 632 | if project in whitelist: |
| 633 | return None |
| 634 | |
| 635 | # We assume the repo name is the same as the dir name on disk. |
| 636 | # It would be dumb to not have them match though. |
| 637 | project = os.path.basename(project) |
| 638 | |
| 639 | is_variant = lambda x: x.startswith('overlay-variant-') |
| 640 | is_board = lambda x: x.startswith('overlay-') |
| 641 | is_private = lambda x: x.endswith('-private') |
| 642 | |
| 643 | get_pv = re.compile(r'(.*?)virtual/([^/]+)/\2-([^/]*)\.ebuild$') |
| 644 | |
| 645 | ebuilds_re = [r'\.ebuild$'] |
| 646 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 647 | ebuilds_re) |
| 648 | bad_ebuilds = [] |
| 649 | |
| 650 | for ebuild in ebuilds: |
| 651 | m = get_pv.match(ebuild) |
| 652 | if m: |
| 653 | overlay = m.group(1) |
| 654 | if not overlay or not is_board(overlay): |
| 655 | overlay = project |
| 656 | |
| 657 | pv = m.group(3).split('-', 1)[0] |
| 658 | |
| 659 | if is_private(overlay): |
| 660 | want_pv = '3.5' if is_variant(overlay) else '3' |
| 661 | elif is_board(overlay): |
| 662 | want_pv = '2.5' if is_variant(overlay) else '2' |
| 663 | else: |
| 664 | want_pv = '1' |
| 665 | |
| 666 | if pv != want_pv: |
| 667 | bad_ebuilds.append((ebuild, pv, want_pv)) |
| 668 | |
| 669 | if bad_ebuilds: |
| 670 | # pylint: disable=C0301 |
| 671 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/portage-build-faq#TOC-Virtuals-and-central-management' |
| 672 | # pylint: enable=C0301 |
| 673 | return HookFailure( |
| 674 | 'These virtuals have incorrect package versions (PVs). Please adjust:\n' |
| 675 | '\t%s\n' |
| 676 | 'If this is an upstream Gentoo virtual, then you may ignore this\n' |
| 677 | 'check (and re-run w/--no-verify). Otherwise, please see this\n' |
| 678 | 'page for more details:\n%s\n' % |
| 679 | ('\n\t'.join(['%s:\n\t\tPV is %s but should be %s' % x |
| 680 | for x in bad_ebuilds]), url)) |
| 681 | |
| 682 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 683 | def _check_change_has_proper_changeid(_project, commit): |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 684 | """Verify that Change-ID is present in last paragraph of commit message.""" |
| 685 | desc = _get_commit_desc(commit) |
| 686 | loc = desc.rfind('\nChange-Id:') |
| 687 | 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] | 688 | return HookFailure('Change-Id must be in last paragraph of description.') |
| 689 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 690 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 691 | def _check_license(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 692 | """Verifies the license header.""" |
| 693 | LICENSE_HEADER = ( |
Chris Sosa | ed7a3fa | 2014-02-26 12:18:31 -0800 | [diff] [blame] | 694 | r".* Copyright( \(c\))? 20[-0-9]{2,7} The Chromium OS Authors\. " |
| 695 | "All rights reserved\." "\n" |
| 696 | r".* Use of this source code is governed by a BSD-style license that can " |
| 697 | "be\n" |
| 698 | r".* found in the LICENSE file\." |
| 699 | "\n" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 700 | ) |
David Hendricks | 35030d0 | 2013-02-04 17:49:16 -0800 | [diff] [blame] | 701 | FAIL_MSG = "License must match" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 702 | |
David Hendricks | 35030d0 | 2013-02-04 17:49:16 -0800 | [diff] [blame] | 703 | return _verify_header_content(commit, LICENSE_HEADER, FAIL_MSG) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 704 | |
| 705 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 706 | def _check_google_copyright(_project, commit): |
David Hendricks | a0e310d | 2013-02-04 17:51:55 -0800 | [diff] [blame] | 707 | """Verifies Google Inc. as copyright holder.""" |
| 708 | LICENSE_HEADER = ( |
| 709 | r".* Copyright 20[-0-9]{2,7} Google Inc\." |
| 710 | ) |
| 711 | FAIL_MSG = "Copyright must match" |
| 712 | |
David Hendricks | 4c018e7 | 2013-02-06 13:46:38 -0800 | [diff] [blame] | 713 | # Avoid blocking partners and external contributors. |
| 714 | fqdn = socket.getfqdn() |
| 715 | if not fqdn.endswith(".corp.google.com"): |
| 716 | return None |
| 717 | |
David Hendricks | a0e310d | 2013-02-04 17:51:55 -0800 | [diff] [blame] | 718 | return _verify_header_content(commit, LICENSE_HEADER, FAIL_MSG) |
| 719 | |
| 720 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 721 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 722 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 723 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 724 | def _run_checkpatch(_project, commit, options=()): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 725 | """Runs checkpatch.pl on the given project""" |
| 726 | hooks_dir = _get_hooks_dir() |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 727 | cmd = ['%s/checkpatch.pl' % hooks_dir] + list(options) + ['-'] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 728 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Che-Liang Chiou | 5ce2d7b | 2013-03-22 18:47:55 -0700 | [diff] [blame] | 729 | output = p.communicate(_get_patch(commit))[0] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 730 | if p.returncode: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 731 | return HookFailure('checkpatch.pl errors/warnings\n\n' + output) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 732 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 733 | |
Anton Staaf | 815d685 | 2011-08-22 10:08:45 -0700 | [diff] [blame] | 734 | def _run_checkpatch_no_tree(project, commit): |
| 735 | return _run_checkpatch(project, commit, ['--no-tree']) |
| 736 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 737 | |
Randall Spangler | 7318fd6 | 2013-11-21 12:16:58 -0800 | [diff] [blame] | 738 | def _run_checkpatch_ec(project, commit): |
| 739 | """Runs checkpatch with options for Chromium EC projects.""" |
| 740 | return _run_checkpatch(project, commit, ['--no-tree', |
| 741 | '--ignore=MSLEEP,VOLATILE']) |
| 742 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 743 | |
| 744 | def _kernel_configcheck(_project, commit): |
Olof Johansson | a96810f | 2012-09-04 16:20:03 -0700 | [diff] [blame] | 745 | """Makes sure kernel config changes are not mixed with code changes""" |
| 746 | files = _get_affected_files(commit) |
| 747 | if not len(_filter_files(files, [r'chromeos/config'])) in [0, len(files)]: |
| 748 | return HookFailure('Changes to chromeos/config/ and regular files must ' |
| 749 | 'be in separate commits:\n%s' % '\n'.join(files)) |
Anton Staaf | 815d685 | 2011-08-22 10:08:45 -0700 | [diff] [blame] | 750 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 751 | |
| 752 | def _run_json_check(_project, commit): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 753 | """Checks that all JSON files are syntactically valid.""" |
Dale Curtis | a039cfd | 2011-05-04 12:01:05 -0700 | [diff] [blame] | 754 | for f in _filter_files(_get_affected_files(commit), [r'.*\.json']): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 755 | try: |
| 756 | json.load(open(f)) |
| 757 | except Exception, e: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 758 | return HookFailure('Invalid JSON in %s: %s' % (f, e)) |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 759 | |
| 760 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 761 | def _check_manifests(_project, commit): |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 762 | """Make sure Manifest files only have DIST lines""" |
| 763 | paths = [] |
| 764 | |
| 765 | for path in _get_affected_files(commit): |
| 766 | if os.path.basename(path) != 'Manifest': |
| 767 | continue |
| 768 | if not os.path.exists(path): |
| 769 | continue |
| 770 | |
| 771 | with open(path, 'r') as f: |
| 772 | for line in f.readlines(): |
| 773 | if not line.startswith('DIST '): |
| 774 | paths.append(path) |
| 775 | break |
| 776 | |
| 777 | if paths: |
| 778 | return HookFailure('Please remove lines that do not start with DIST:\n%s' % |
| 779 | ('\n'.join(paths),)) |
| 780 | |
| 781 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 782 | def _check_change_has_branch_field(_project, commit): |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 783 | """Check for a non-empty 'BRANCH=' field in the commit message.""" |
| 784 | BRANCH_RE = r'\nBRANCH=\S+' |
| 785 | |
| 786 | if not re.search(BRANCH_RE, _get_commit_desc(commit)): |
| 787 | msg = ('Changelist description needs BRANCH field (after first line)\n' |
| 788 | 'E.g. BRANCH=none or BRANCH=link,snow') |
| 789 | return HookFailure(msg) |
| 790 | |
| 791 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 792 | def _check_change_has_signoff_field(_project, commit): |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 793 | """Check for a non-empty 'Signed-off-by:' field in the commit message.""" |
| 794 | SIGNOFF_RE = r'\nSigned-off-by: \S+' |
| 795 | |
| 796 | if not re.search(SIGNOFF_RE, _get_commit_desc(commit)): |
| 797 | msg = ('Changelist description needs Signed-off-by: field\n' |
| 798 | 'E.g. Signed-off-by: My Name <me@chromium.org>') |
| 799 | return HookFailure(msg) |
| 800 | |
| 801 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 802 | def _run_project_hook_script(script, project, commit): |
| 803 | """Runs a project hook script. |
| 804 | |
| 805 | The script is run with the following environment variables set: |
| 806 | PRESUBMIT_PROJECT: The affected project |
| 807 | PRESUBMIT_COMMIT: The affected commit |
| 808 | PRESUBMIT_FILES: A newline-separated list of affected files |
| 809 | |
| 810 | The script is considered to fail if the exit code is non-zero. It should |
| 811 | write an error message to stdout. |
| 812 | """ |
| 813 | env = dict(os.environ) |
| 814 | env['PRESUBMIT_PROJECT'] = project |
| 815 | env['PRESUBMIT_COMMIT'] = commit |
| 816 | |
| 817 | # Put affected files in an environment variable |
| 818 | files = _get_affected_files(commit) |
| 819 | env['PRESUBMIT_FILES'] = '\n'.join(files) |
| 820 | |
| 821 | process = subprocess.Popen(script, env=env, shell=True, |
| 822 | stdin=open(os.devnull), |
Jon Salz | 7b618af | 2012-08-31 06:03:16 +0800 | [diff] [blame] | 823 | stdout=subprocess.PIPE, |
| 824 | stderr=subprocess.STDOUT) |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 825 | stdout, _ = process.communicate() |
| 826 | if process.wait(): |
Jon Salz | 7b618af | 2012-08-31 06:03:16 +0800 | [diff] [blame] | 827 | if stdout: |
| 828 | stdout = re.sub('(?m)^', ' ', stdout) |
| 829 | return HookFailure('Hook script "%s" failed with code %d%s' % |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 830 | (script, process.returncode, |
| 831 | ':\n' + stdout if stdout else '')) |
| 832 | |
| 833 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 834 | # Base |
| 835 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 836 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 837 | # A list of hooks that are not project-specific |
| 838 | _COMMON_HOOKS = [ |
| 839 | _check_change_has_bug_field, |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 840 | _check_change_has_valid_cq_depend, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 841 | _check_change_has_test_field, |
| 842 | _check_change_has_proper_changeid, |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 843 | _check_ebuild_eapi, |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 844 | _check_ebuild_keywords, |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 845 | _check_ebuild_licenses, |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 846 | _check_ebuild_virtual_pv, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 847 | _check_no_stray_whitespace, |
| 848 | _check_no_long_lines, |
| 849 | _check_license, |
| 850 | _check_no_tabs, |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 851 | _check_for_uprev, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 852 | ] |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 853 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 854 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 855 | # A dictionary of project-specific hooks(callbacks), indexed by project name. |
| 856 | # dict[project] = [callback1, callback2] |
| 857 | _PROJECT_SPECIFIC_HOOKS = { |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 858 | "chromeos/autotest-tools": [_run_json_check], |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 859 | "chromeos/overlays/chromeos-overlay": [_check_manifests], |
| 860 | "chromeos/overlays/chromeos-partner-overlay": [_check_manifests], |
Randall Spangler | 7318fd6 | 2013-11-21 12:16:58 -0800 | [diff] [blame] | 861 | "chromeos/platform/ec-private": [_run_checkpatch_ec, |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 862 | _check_change_has_branch_field], |
Puneet Kumar | 57b9c09 | 2012-08-14 18:58:29 -0700 | [diff] [blame] | 863 | "chromeos/third_party/intel-framework": [_check_change_has_branch_field], |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 864 | "chromeos/vendor/kernel-exynos-staging": [_run_checkpatch, |
| 865 | _kernel_configcheck], |
| 866 | "chromeos/vendor/u-boot-exynos": [_run_checkpatch_no_tree], |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 867 | "chromiumos/overlays/board-overlays": [_check_manifests], |
| 868 | "chromiumos/overlays/chromiumos-overlay": [_check_manifests], |
| 869 | "chromiumos/overlays/portage-stable": [_check_manifests], |
Randall Spangler | 7318fd6 | 2013-11-21 12:16:58 -0800 | [diff] [blame] | 870 | "chromiumos/platform/ec": [_run_checkpatch_ec, |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 871 | _check_change_has_branch_field], |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 872 | "chromiumos/platform/mosys": [_check_change_has_branch_field], |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 873 | "chromiumos/platform/vboot_reference": [_check_change_has_branch_field], |
Shawn Nematbakhsh | b6ac17a | 2014-01-28 16:47:13 -0800 | [diff] [blame] | 874 | "chromiumos/third_party/coreboot": [_check_change_has_branch_field, |
| 875 | _check_change_has_signoff_field, |
| 876 | _check_google_copyright], |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 877 | "chromiumos/third_party/flashrom": [_check_change_has_branch_field], |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 878 | "chromiumos/third_party/kernel": [_run_checkpatch, _kernel_configcheck], |
| 879 | "chromiumos/third_party/kernel-next": [_run_checkpatch, |
| 880 | _kernel_configcheck], |
| 881 | "chromiumos/third_party/u-boot": [_run_checkpatch_no_tree, |
| 882 | _check_change_has_branch_field], |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 883 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 884 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 885 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 886 | # A dictionary of flags (keys) that can appear in the config file, and the hook |
| 887 | # that the flag disables (value) |
| 888 | _DISABLE_FLAGS = { |
| 889 | 'stray_whitespace_check': _check_no_stray_whitespace, |
| 890 | 'long_line_check': _check_no_long_lines, |
| 891 | 'cros_license_check': _check_license, |
| 892 | 'tab_check': _check_no_tabs, |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 893 | 'branch_check': _check_change_has_branch_field, |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 894 | 'signoff_check': _check_change_has_signoff_field, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 898 | def _get_disabled_hooks(config): |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 899 | """Returns a set of hooks disabled by the current project's config file. |
| 900 | |
| 901 | Expects to be called within the project root. |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 902 | |
| 903 | Args: |
| 904 | config: A ConfigParser for the project's config file. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 905 | """ |
| 906 | SECTION = 'Hook Overrides' |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 907 | if not config.has_section(SECTION): |
| 908 | return set() |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 909 | |
| 910 | disable_flags = [] |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 911 | for flag in config.options(SECTION): |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 912 | try: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 913 | if not config.getboolean(SECTION, flag): |
| 914 | disable_flags.append(flag) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 915 | except ValueError as e: |
| 916 | msg = "Error parsing flag \'%s\' in %s file - " % (flag, _CONFIG_FILE) |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 917 | print(msg + str(e)) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 918 | |
| 919 | disabled_keys = set(_DISABLE_FLAGS.iterkeys()).intersection(disable_flags) |
| 920 | return set([_DISABLE_FLAGS[key] for key in disabled_keys]) |
| 921 | |
| 922 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 923 | def _get_project_hook_scripts(config): |
| 924 | """Returns a list of project-specific hook scripts. |
| 925 | |
| 926 | Args: |
| 927 | config: A ConfigParser for the project's config file. |
| 928 | """ |
| 929 | SECTION = 'Hook Scripts' |
| 930 | if not config.has_section(SECTION): |
| 931 | return [] |
| 932 | |
| 933 | hook_names_values = config.items(SECTION) |
| 934 | hook_names_values.sort(key=lambda x: x[0]) |
| 935 | return [x[1] for x in hook_names_values] |
| 936 | |
| 937 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 938 | def _get_project_hooks(project): |
| 939 | """Returns a list of hooks that need to be run for a project. |
| 940 | |
| 941 | Expects to be called from within the project root. |
| 942 | """ |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 943 | config = ConfigParser.RawConfigParser() |
| 944 | try: |
| 945 | config.read(_CONFIG_FILE) |
| 946 | except ConfigParser.Error: |
| 947 | # Just use an empty config file |
| 948 | config = ConfigParser.RawConfigParser() |
| 949 | |
| 950 | disabled_hooks = _get_disabled_hooks(config) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 951 | hooks = [hook for hook in _COMMON_HOOKS if hook not in disabled_hooks] |
| 952 | |
| 953 | if project in _PROJECT_SPECIFIC_HOOKS: |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 954 | hooks.extend(hook for hook in _PROJECT_SPECIFIC_HOOKS[project] |
| 955 | if hook not in disabled_hooks) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 956 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 957 | for script in _get_project_hook_scripts(config): |
| 958 | hooks.append(functools.partial(_run_project_hook_script, script)) |
| 959 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 960 | return hooks |
| 961 | |
| 962 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 963 | def _run_project_hooks(project, proj_dir=None, commit_list=None): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 964 | """For each project run its project specific hook from the hooks dictionary. |
| 965 | |
| 966 | Args: |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 967 | project: The name of project to run hooks for. |
| 968 | proj_dir: If non-None, this is the directory the project is in. If None, |
| 969 | we'll ask repo. |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 970 | commit_list: A list of commits to run hooks against. If None or empty list |
| 971 | then we'll automatically get the list of commits that would be uploaded. |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 972 | |
| 973 | Returns: |
| 974 | Boolean value of whether any errors were ecountered while running the hooks. |
| 975 | """ |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 976 | if proj_dir is None: |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 977 | proj_dirs = _run_command(['repo', 'forall', project, '-c', 'pwd']).split() |
| 978 | if len(proj_dirs) == 0: |
| 979 | print('%s cannot be found.' % project, file=sys.stderr) |
| 980 | print('Please specify a valid project.', file=sys.stderr) |
| 981 | return True |
| 982 | if len(proj_dirs) > 1: |
| 983 | print('%s is associated with multiple directories.' % project, |
| 984 | file=sys.stderr) |
| 985 | print('Please specify a directory to help disambiguate.', file=sys.stderr) |
| 986 | return True |
| 987 | proj_dir = proj_dirs[0] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 988 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 989 | pwd = os.getcwd() |
| 990 | # hooks assume they are run from the root of the project |
| 991 | os.chdir(proj_dir) |
| 992 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 993 | if not commit_list: |
| 994 | try: |
| 995 | commit_list = _get_commits() |
| 996 | except VerifyException as e: |
| 997 | PrintErrorForProject(project, HookFailure(str(e))) |
| 998 | os.chdir(pwd) |
| 999 | return True |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1000 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1001 | hooks = _get_project_hooks(project) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1002 | error_found = False |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1003 | for commit in commit_list: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1004 | error_list = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1005 | for hook in hooks: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1006 | hook_error = hook(project, commit) |
| 1007 | if hook_error: |
| 1008 | error_list.append(hook_error) |
| 1009 | error_found = True |
| 1010 | if error_list: |
| 1011 | PrintErrorsForCommit(project, commit, _get_commit_desc(commit), |
| 1012 | error_list) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1013 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1014 | os.chdir(pwd) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1015 | return error_found |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1016 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1017 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1018 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1019 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1020 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1021 | def main(project_list, worktree_list=None, **_kwargs): |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1022 | """Main function invoked directly by repo. |
| 1023 | |
| 1024 | This function will exit directly upon error so that repo doesn't print some |
| 1025 | obscure error message. |
| 1026 | |
| 1027 | Args: |
| 1028 | project_list: List of projects to run on. |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1029 | worktree_list: A list of directories. It should be the same length as |
| 1030 | project_list, so that each entry in project_list matches with a directory |
| 1031 | in worktree_list. If None, we will attempt to calculate the directories |
| 1032 | automatically. |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1033 | kwargs: Leave this here for forward-compatibility. |
| 1034 | """ |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1035 | found_error = False |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1036 | if not worktree_list: |
| 1037 | worktree_list = [None] * len(project_list) |
| 1038 | for project, worktree in zip(project_list, worktree_list): |
| 1039 | if _run_project_hooks(project, proj_dir=worktree): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1040 | found_error = True |
| 1041 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1042 | if found_error: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1043 | msg = ('Preupload failed due to errors in project(s). HINTS:\n' |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1044 | '- To disable some source style checks, and for other hints, see ' |
| 1045 | '<checkout_dir>/src/repohooks/README\n' |
| 1046 | '- To upload only current project, run \'repo upload .\'') |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 1047 | print(msg, file=sys.stderr) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1048 | sys.exit(1) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 1049 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1050 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1051 | def _identify_project(path): |
| 1052 | """Identify the repo project associated with the given path. |
| 1053 | |
| 1054 | Returns: |
| 1055 | A string indicating what project is associated with the path passed in or |
| 1056 | a blank string upon failure. |
| 1057 | """ |
| 1058 | return _run_command(['repo', 'forall', '.', '-c', 'echo ${REPO_PROJECT}'], |
| 1059 | stderr=subprocess.PIPE, cwd=path).strip() |
| 1060 | |
| 1061 | |
| 1062 | def direct_main(args, verbose=False): |
| 1063 | """Run hooks directly (outside of the context of repo). |
| 1064 | |
| 1065 | # Setup for doctests below. |
| 1066 | # ...note that some tests assume that running pre-upload on this CWD is fine. |
| 1067 | # TODO: Use mock and actually mock out _run_project_hooks() for tests. |
| 1068 | >>> mydir = os.path.dirname(os.path.abspath(__file__)) |
| 1069 | >>> olddir = os.getcwd() |
| 1070 | |
| 1071 | # OK to run w/ no arugments; will run with CWD. |
| 1072 | >>> os.chdir(mydir) |
| 1073 | >>> direct_main(['prog_name'], verbose=True) |
| 1074 | Running hooks on chromiumos/repohooks |
| 1075 | 0 |
| 1076 | >>> os.chdir(olddir) |
| 1077 | |
| 1078 | # Run specifying a dir |
| 1079 | >>> direct_main(['prog_name', '--dir=%s' % mydir], verbose=True) |
| 1080 | Running hooks on chromiumos/repohooks |
| 1081 | 0 |
| 1082 | |
| 1083 | # Not a problem to use a bogus project; we'll just get default settings. |
| 1084 | >>> direct_main(['prog_name', '--dir=%s' % mydir, '--project=X'],verbose=True) |
| 1085 | Running hooks on X |
| 1086 | 0 |
| 1087 | |
| 1088 | # Run with project but no dir |
| 1089 | >>> os.chdir(mydir) |
| 1090 | >>> direct_main(['prog_name', '--project=X'], verbose=True) |
| 1091 | Running hooks on X |
| 1092 | 0 |
| 1093 | >>> os.chdir(olddir) |
| 1094 | |
| 1095 | # Try with a non-git CWD |
| 1096 | >>> os.chdir('/tmp') |
| 1097 | >>> direct_main(['prog_name']) |
| 1098 | Traceback (most recent call last): |
| 1099 | ... |
| 1100 | BadInvocation: The current directory is not part of a git project. |
| 1101 | |
| 1102 | # Check various bad arguments... |
| 1103 | >>> direct_main(['prog_name', 'bogus']) |
| 1104 | Traceback (most recent call last): |
| 1105 | ... |
| 1106 | BadInvocation: Unexpected arguments: bogus |
| 1107 | >>> direct_main(['prog_name', '--project=bogus', '--dir=bogusdir']) |
| 1108 | Traceback (most recent call last): |
| 1109 | ... |
| 1110 | BadInvocation: Invalid dir: bogusdir |
| 1111 | >>> direct_main(['prog_name', '--project=bogus', '--dir=/tmp']) |
| 1112 | Traceback (most recent call last): |
| 1113 | ... |
| 1114 | BadInvocation: Not a git directory: /tmp |
| 1115 | |
| 1116 | Args: |
| 1117 | args: The value of sys.argv |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1118 | verbose: Log verbose info while running |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1119 | |
| 1120 | Returns: |
| 1121 | 0 if no pre-upload failures, 1 if failures. |
| 1122 | |
| 1123 | Raises: |
| 1124 | BadInvocation: On some types of invocation errors. |
| 1125 | """ |
| 1126 | desc = 'Run Chromium OS pre-upload hooks on changes compared to upstream.' |
| 1127 | parser = optparse.OptionParser(description=desc) |
| 1128 | |
| 1129 | parser.add_option('--dir', default=None, |
| 1130 | help='The directory that the project lives in. If not ' |
| 1131 | 'specified, use the git project root based on the cwd.') |
| 1132 | parser.add_option('--project', default=None, |
| 1133 | help='The project repo path; this can affect how the hooks ' |
| 1134 | 'get run, since some hooks are project-specific. For ' |
| 1135 | 'chromite this is chromiumos/chromite. If not specified, ' |
| 1136 | 'the repo tool will be used to figure this out based on ' |
| 1137 | 'the dir.') |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1138 | parser.add_option('--rerun-since', default=None, |
| 1139 | help='Rerun hooks on old commits since the given date. ' |
| 1140 | 'The date should match git log\'s concept of a date. ' |
| 1141 | 'e.g. 2012-06-20') |
| 1142 | |
| 1143 | parser.usage = "pre-upload.py [options] [commits]" |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1144 | |
| 1145 | opts, args = parser.parse_args(args[1:]) |
| 1146 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1147 | if opts.rerun_since: |
| 1148 | if args: |
| 1149 | raise BadInvocation('Can\'t pass commits and use rerun-since: %s' % |
| 1150 | ' '.join(args)) |
| 1151 | |
| 1152 | cmd = ['git', 'log', '--since="%s"' % opts.rerun_since, '--pretty=%H'] |
| 1153 | all_commits = _run_command(cmd).splitlines() |
| 1154 | bot_commits = _run_command(cmd + ['--author=chrome-bot']).splitlines() |
| 1155 | |
| 1156 | # Eliminate chrome-bot commits but keep ordering the same... |
| 1157 | bot_commits = set(bot_commits) |
| 1158 | args = [c for c in all_commits if c not in bot_commits] |
| 1159 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1160 | |
| 1161 | # Check/normlaize git dir; if unspecified, we'll use the root of the git |
| 1162 | # project from CWD |
| 1163 | if opts.dir is None: |
| 1164 | git_dir = _run_command(['git', 'rev-parse', '--git-dir'], |
| 1165 | stderr=subprocess.PIPE).strip() |
| 1166 | if not git_dir: |
| 1167 | raise BadInvocation('The current directory is not part of a git project.') |
| 1168 | opts.dir = os.path.dirname(os.path.abspath(git_dir)) |
| 1169 | elif not os.path.isdir(opts.dir): |
| 1170 | raise BadInvocation('Invalid dir: %s' % opts.dir) |
| 1171 | elif not os.path.isdir(os.path.join(opts.dir, '.git')): |
| 1172 | raise BadInvocation('Not a git directory: %s' % opts.dir) |
| 1173 | |
| 1174 | # Identify the project if it wasn't specified; this _requires_ the repo |
| 1175 | # tool to be installed and for the project to be part of a repo checkout. |
| 1176 | if not opts.project: |
| 1177 | opts.project = _identify_project(opts.dir) |
| 1178 | if not opts.project: |
| 1179 | raise BadInvocation("Repo couldn't identify the project of %s" % opts.dir) |
| 1180 | |
| 1181 | if verbose: |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 1182 | print("Running hooks on %s" % (opts.project)) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1183 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1184 | found_error = _run_project_hooks(opts.project, proj_dir=opts.dir, |
| 1185 | commit_list=args) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1186 | if found_error: |
| 1187 | return 1 |
| 1188 | return 0 |
| 1189 | |
| 1190 | |
| 1191 | def _test(): |
| 1192 | """Run any built-in tests.""" |
| 1193 | import doctest |
| 1194 | doctest.testmod() |
| 1195 | |
| 1196 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1197 | if __name__ == '__main__': |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1198 | if sys.argv[1:2] == ["--test"]: |
| 1199 | _test() |
| 1200 | exit_code = 0 |
| 1201 | else: |
| 1202 | prog_name = os.path.basename(sys.argv[0]) |
| 1203 | try: |
| 1204 | exit_code = direct_main(sys.argv) |
| 1205 | except BadInvocation, e: |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 1206 | print("%s: %s" % (prog_name, str(e)), file=sys.stderr) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1207 | exit_code = 1 |
| 1208 | sys.exit(exit_code) |