Prathmesh Prabhu | c525465 | 2016-12-22 12:58:05 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
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 | |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 13 | import argparse |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 14 | import collections |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 15 | import ConfigParser |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 16 | import fnmatch |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 17 | import functools |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 18 | import json |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 19 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 20 | import re |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 21 | import sys |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 22 | import stat |
Aviv Keshet | 5ac5952 | 2017-01-31 14:28:27 -0800 | [diff] [blame] | 23 | import StringIO |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 24 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 25 | from errors import (VerifyException, HookFailure, PrintErrorForProject, |
| 26 | PrintErrorsForCommit) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 27 | |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 28 | # If repo imports us, the __name__ will be __builtin__, and the wrapper will |
| 29 | # be in $CHROMEOS_CHECKOUT/.repo/repo/main.py, so we need to go two directories |
| 30 | # up. The same logic also happens to work if we're executed directly. |
| 31 | if __name__ in ('__builtin__', '__main__'): |
| 32 | sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', '..')) |
| 33 | |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 34 | from chromite.lib import commandline |
Aviv Keshet | 5ac5952 | 2017-01-31 14:28:27 -0800 | [diff] [blame] | 35 | from chromite.lib import constants |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 36 | from chromite.lib import cros_build_lib |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 37 | from chromite.lib import git |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 38 | from chromite.lib import osutils |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 39 | from chromite.lib import patch |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 40 | from chromite.licensing import licenses_lib |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 41 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 42 | PRE_SUBMIT = 'pre-submit' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 43 | |
| 44 | COMMON_INCLUDED_PATHS = [ |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 45 | # C++ and friends |
| 46 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 47 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 48 | # Scripts |
| 49 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 50 | # No extension at all, note that ALL CAPS files are black listed in |
| 51 | # COMMON_EXCLUDED_LIST below. |
| 52 | r"(^|.*[\\\/])[^.]+$", |
| 53 | # Other |
| 54 | r".*\.java$", r".*\.mk$", r".*\.am$", |
Brian Norris | fdbac8e | 2016-06-16 11:09:05 -0700 | [diff] [blame] | 55 | r".*\.policy$", r".*\.conf$", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 56 | ] |
| 57 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 58 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 59 | COMMON_EXCLUDED_PATHS = [ |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 60 | # Avoid doing source file checks for kernel. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 61 | r"/src/third_party/kernel/", |
| 62 | r"/src/third_party/kernel-next/", |
| 63 | r"/src/third_party/ktop/", |
| 64 | r"/src/third_party/punybench/", |
| 65 | r".*\bexperimental[\\\/].*", |
| 66 | r".*\b[A-Z0-9_]{2,}$", |
| 67 | r".*[\\\/]debian[\\\/]rules$", |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 68 | |
| 69 | # For ebuild trees, ignore any caches and manifest data. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 70 | r".*/Manifest$", |
| 71 | r".*/metadata/[^/]*cache[^/]*/[^/]+/[^/]+$", |
Doug Anderson | 5bfb679 | 2011-10-25 16:45:41 -0700 | [diff] [blame] | 72 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 73 | # Ignore profiles data (like overlay-tegra2/profiles). |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 74 | r"(^|.*/)overlay-.*/profiles/.*", |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 75 | r"^profiles/.*$", |
| 76 | |
C Shapiro | 8f90e9b | 2017-06-28 09:54:50 -0600 | [diff] [blame^] | 77 | # Ignore config files in ebuild setup. |
| 78 | r"(^|.*/)overlay-.*/chromeos-base/chromeos-bsp.*/files/.*", |
| 79 | r"^chromeos-base/chromeos-bsp.*/files/.*", |
| 80 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 81 | # Ignore minified js and jquery. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 82 | r".*\.min\.js", |
| 83 | r".*jquery.*\.js", |
Mike Frysinger | 33a458d | 2014-03-03 17:00:51 -0500 | [diff] [blame] | 84 | |
| 85 | # Ignore license files as the content is often taken verbatim. |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 86 | r".*/licenses/.*", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 87 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 88 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 89 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 90 | _CONFIG_FILE = 'PRESUBMIT.cfg' |
| 91 | |
| 92 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 93 | # File containing wildcards, one per line, matching files that should be |
| 94 | # excluded from presubmit checks. Lines beginning with '#' are ignored. |
| 95 | _IGNORE_FILE = '.presubmitignore' |
| 96 | |
| 97 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 98 | # Exceptions |
| 99 | |
| 100 | |
| 101 | class BadInvocation(Exception): |
| 102 | """An Exception indicating a bad invocation of the program.""" |
| 103 | pass |
| 104 | |
| 105 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 106 | # General Helpers |
| 107 | |
Sean Paul | ba01d40 | 2011-05-05 11:36:23 -0400 | [diff] [blame] | 108 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 109 | Project = collections.namedtuple('Project', ['name', 'dir', 'remote']) |
| 110 | |
| 111 | |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 112 | # pylint: disable=redefined-builtin |
| 113 | def _run_command(cmd, cwd=None, input=None, |
| 114 | redirect_stderr=False, combine_stdout_stderr=False): |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 115 | """Executes the passed in command and returns raw stdout output. |
| 116 | |
| 117 | Args: |
| 118 | cmd: The command to run; should be a list of strings. |
| 119 | cwd: The directory to switch to for running the command. |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 120 | input: The data to pipe into this command through stdin. If a file object |
| 121 | or file descriptor, stdin will be connected directly to that. |
| 122 | redirect_stderr: Redirect stderr away from console. |
| 123 | combine_stdout_stderr: Combines stdout and stderr streams into stdout. |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 124 | |
| 125 | Returns: |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 126 | The stdout from the process (discards stderr and returncode). |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 127 | """ |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 128 | return cros_build_lib.RunCommand(cmd=cmd, |
| 129 | cwd=cwd, |
| 130 | print_cmd=False, |
| 131 | input=input, |
| 132 | stdout_to_pipe=True, |
| 133 | redirect_stderr=redirect_stderr, |
| 134 | combine_stdout_stderr=combine_stdout_stderr, |
| 135 | error_code_ok=True).output |
| 136 | # pylint: enable=redefined-builtin |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 137 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 138 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 139 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 140 | """Returns the absolute path to the repohooks directory.""" |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 141 | if __name__ == '__main__': |
| 142 | # Works when file is run on its own (__file__ is defined)... |
| 143 | return os.path.abspath(os.path.dirname(__file__)) |
| 144 | else: |
| 145 | # We need to do this when we're run through repo. Since repo executes |
| 146 | # us with execfile(), we don't get __file__ defined. |
| 147 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
| 148 | return _run_command(cmd).strip() |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 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 _match_regex_list(subject, expressions): |
| 152 | """Try to match a list of regular expressions to a string. |
| 153 | |
| 154 | Args: |
| 155 | subject: The string to match regexes on |
| 156 | expressions: A list of regular expressions to check for matches with. |
| 157 | |
| 158 | Returns: |
| 159 | Whether the passed in subject matches any of the passed in regexes. |
| 160 | """ |
| 161 | for expr in expressions: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 162 | if re.search(expr, subject): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 163 | return True |
| 164 | return False |
| 165 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 166 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 167 | def _filter_files(files, include_list, exclude_list=()): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 168 | """Filter out files based on the conditions passed in. |
| 169 | |
| 170 | Args: |
| 171 | files: list of filepaths to filter |
| 172 | include_list: list of regex that when matched with a file path will cause it |
| 173 | to be added to the output list unless the file is also matched with a |
| 174 | regex in the exclude_list. |
| 175 | exclude_list: list of regex that when matched with a file will prevent it |
| 176 | from being added to the output list, even if it is also matched with a |
| 177 | regex in the include_list. |
| 178 | |
| 179 | Returns: |
| 180 | A list of filepaths that contain files matched in the include_list and not |
| 181 | in the exclude_list. |
| 182 | """ |
| 183 | filtered = [] |
| 184 | for f in files: |
| 185 | if (_match_regex_list(f, include_list) and |
| 186 | not _match_regex_list(f, exclude_list)): |
| 187 | filtered.append(f) |
| 188 | return filtered |
| 189 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 190 | |
| 191 | # Git Helpers |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 192 | |
| 193 | |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 194 | def _get_upstream_branch(): |
| 195 | """Returns the upstream tracking branch of the current branch. |
| 196 | |
| 197 | Raises: |
| 198 | Error if there is no tracking branch |
| 199 | """ |
| 200 | current_branch = _run_command(['git', 'symbolic-ref', 'HEAD']).strip() |
| 201 | current_branch = current_branch.replace('refs/heads/', '') |
| 202 | if not current_branch: |
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 | cfg_option = 'branch.' + current_branch + '.%s' |
| 206 | full_upstream = _run_command(['git', 'config', cfg_option % 'merge']).strip() |
| 207 | remote = _run_command(['git', 'config', cfg_option % 'remote']).strip() |
| 208 | if not remote or not full_upstream: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 209 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 210 | |
| 211 | return full_upstream.replace('heads', 'remotes/' + remote) |
| 212 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 213 | |
Che-Liang Chiou | 5ce2d7b | 2013-03-22 18:47:55 -0700 | [diff] [blame] | 214 | def _get_patch(commit): |
| 215 | """Returns the patch for this commit.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 216 | if commit == PRE_SUBMIT: |
| 217 | return _run_command(['git', 'diff', '--cached', 'HEAD']) |
| 218 | else: |
| 219 | return _run_command(['git', 'format-patch', '--stdout', '-1', commit]) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 220 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 221 | |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 222 | def _try_utf8_decode(data): |
| 223 | """Attempts to decode a string as UTF-8. |
| 224 | |
| 225 | Returns: |
| 226 | The decoded Unicode object, or the original string if parsing fails. |
| 227 | """ |
| 228 | try: |
| 229 | return unicode(data, 'utf-8', 'strict') |
| 230 | except UnicodeDecodeError: |
| 231 | return data |
| 232 | |
| 233 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 234 | def _get_file_content(path, commit): |
| 235 | """Returns the content of a file at a specific commit. |
| 236 | |
| 237 | We can't rely on the file as it exists in the filesystem as people might be |
| 238 | uploading a series of changes which modifies the file multiple times. |
| 239 | |
| 240 | Note: The "content" of a symlink is just the target. So if you're expecting |
| 241 | a full file, you should check that first. One way to detect is that the |
| 242 | content will not have any newlines. |
| 243 | """ |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 244 | if commit == PRE_SUBMIT: |
| 245 | return _run_command(['git', 'diff', 'HEAD', path]) |
| 246 | else: |
| 247 | return _run_command(['git', 'show', '%s:%s' % (commit, path)]) |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 248 | |
| 249 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 250 | def _get_file_diff(path, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 251 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 252 | if commit == PRE_SUBMIT: |
Prathmesh Prabhu | a9de172 | 2016-12-22 14:56:40 -0800 | [diff] [blame] | 253 | command = ['git', 'diff', '-p', '--pretty=format:', '--no-ext-diff', 'HEAD', |
| 254 | path] |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 255 | else: |
Prathmesh Prabhu | a9de172 | 2016-12-22 14:56:40 -0800 | [diff] [blame] | 256 | command = ['git', 'show', '-p', '--pretty=format:', '--no-ext-diff', commit, |
| 257 | path] |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 258 | output = _run_command(command) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 259 | |
| 260 | new_lines = [] |
| 261 | line_num = 0 |
| 262 | for line in output.splitlines(): |
| 263 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 264 | if m: |
| 265 | line_num = int(m.groups(1)[0]) |
| 266 | continue |
| 267 | if line.startswith('+') and not line.startswith('++'): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 268 | new_lines.append((line_num, _try_utf8_decode(line[1:]))) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 269 | if not line.startswith('-'): |
| 270 | line_num += 1 |
| 271 | return new_lines |
| 272 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 273 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 274 | def _get_ignore_wildcards(directory, cache): |
| 275 | """Get wildcards listed in a directory's _IGNORE_FILE. |
| 276 | |
| 277 | Args: |
| 278 | directory: A string containing a directory path. |
| 279 | cache: A dictionary (opaque to caller) caching previously-read wildcards. |
| 280 | |
| 281 | Returns: |
| 282 | A list of wildcards from _IGNORE_FILE or an empty list if _IGNORE_FILE |
| 283 | wasn't present. |
| 284 | """ |
| 285 | # In the cache, keys are directories and values are lists of wildcards from |
| 286 | # _IGNORE_FILE within those directories (and empty if no file was present). |
| 287 | if directory not in cache: |
| 288 | wildcards = [] |
| 289 | dotfile_path = os.path.join(directory, _IGNORE_FILE) |
| 290 | if os.path.exists(dotfile_path): |
| 291 | # TODO(derat): Consider using _get_file_content() to get the file as of |
| 292 | # this commit instead of the on-disk version. This may have a noticeable |
| 293 | # performance impact, as each call to _get_file_content() runs git. |
| 294 | with open(dotfile_path, 'r') as dotfile: |
| 295 | for line in dotfile.readlines(): |
| 296 | line = line.strip() |
| 297 | if line.startswith('#'): |
| 298 | continue |
| 299 | if line.endswith('/'): |
| 300 | line += '*' |
| 301 | wildcards.append(line) |
| 302 | cache[directory] = wildcards |
| 303 | |
| 304 | return cache[directory] |
| 305 | |
| 306 | |
| 307 | def _path_is_ignored(path, cache): |
| 308 | """Check whether a path is ignored by _IGNORE_FILE. |
| 309 | |
| 310 | Args: |
| 311 | path: A string containing a path. |
| 312 | cache: A dictionary (opaque to caller) caching previously-read wildcards. |
| 313 | |
| 314 | Returns: |
| 315 | True if a file named _IGNORE_FILE in one of the passed-in path's parent |
| 316 | directories contains a wildcard matching the path. |
| 317 | """ |
| 318 | # Skip ignore files. |
| 319 | if os.path.basename(path) == _IGNORE_FILE: |
| 320 | return True |
| 321 | |
| 322 | path = os.path.abspath(path) |
| 323 | base = os.getcwd() |
| 324 | |
| 325 | prefix = os.path.dirname(path) |
| 326 | while prefix.startswith(base): |
| 327 | rel_path = path[len(prefix) + 1:] |
| 328 | for wildcard in _get_ignore_wildcards(prefix, cache): |
| 329 | if fnmatch.fnmatch(rel_path, wildcard): |
| 330 | return True |
| 331 | prefix = os.path.dirname(prefix) |
| 332 | |
| 333 | return False |
| 334 | |
| 335 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 336 | def _get_affected_files(commit, include_deletes=False, relative=False, |
| 337 | include_symlinks=False, include_adds=True, |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 338 | full_details=False, use_ignore_files=True): |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 339 | """Returns list of file paths that were modified/added, excluding symlinks. |
| 340 | |
| 341 | Args: |
| 342 | commit: The commit |
| 343 | include_deletes: If true, we'll include deleted files in the result |
| 344 | relative: Whether to return relative or full paths to files |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 345 | include_symlinks: If true, we'll include symlinks in the result |
| 346 | include_adds: If true, we'll include new files in the result |
| 347 | full_details: If False, return filenames, else return structured results. |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 348 | use_ignore_files: Whether we ignore files matched by _IGNORE_FILE files. |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 349 | |
| 350 | Returns: |
| 351 | A list of modified/added (and perhaps deleted) files |
| 352 | """ |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 353 | if not relative and full_details: |
| 354 | raise ValueError('full_details only supports relative paths currently') |
| 355 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 356 | if commit == PRE_SUBMIT: |
| 357 | return _run_command(['git', 'diff-index', '--cached', |
| 358 | '--name-only', 'HEAD']).split() |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 359 | |
| 360 | path = os.getcwd() |
| 361 | files = git.RawDiff(path, '%s^!' % commit) |
| 362 | |
| 363 | # Filter out symlinks. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 364 | if not include_symlinks: |
| 365 | files = [x for x in files if not stat.S_ISLNK(int(x.dst_mode, 8))] |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 366 | |
| 367 | if not include_deletes: |
| 368 | files = [x for x in files if x.status != 'D'] |
| 369 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 370 | if not include_adds: |
| 371 | files = [x for x in files if x.status != 'A'] |
| 372 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 373 | if use_ignore_files: |
| 374 | cache = {} |
| 375 | is_ignored = lambda x: _path_is_ignored(x.dst_file or x.src_file, cache) |
| 376 | files = [x for x in files if not is_ignored(x)] |
| 377 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 378 | if full_details: |
| 379 | # Caller wants the raw objects to parse status/etc... themselves. |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 380 | return files |
| 381 | else: |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 382 | # Caller only cares about filenames. |
| 383 | files = [x.dst_file if x.dst_file else x.src_file for x in files] |
| 384 | if relative: |
| 385 | return files |
| 386 | else: |
| 387 | return [os.path.join(path, x) for x in files] |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 388 | |
| 389 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 390 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 391 | """Returns a list of commits for this review.""" |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 392 | cmd = ['git', 'log', '%s..' % _get_upstream_branch(), '--format=%H'] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 393 | return _run_command(cmd).split() |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 394 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 395 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 396 | def _get_commit_desc(commit): |
| 397 | """Returns the full commit message of a commit.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 398 | if commit == PRE_SUBMIT: |
| 399 | return '' |
Sean Paul | 23a2c58 | 2011-05-06 13:10:44 -0400 | [diff] [blame] | 400 | return _run_command(['git', 'log', '--format=%s%n%n%b', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 401 | |
| 402 | |
Prathmesh Prabhu | c525465 | 2016-12-22 12:58:05 -0800 | [diff] [blame] | 403 | def _check_lines_in_diff(commit, files, check_callable, error_description): |
| 404 | """Checks given file for errors via the given check. |
| 405 | |
| 406 | This is a convenience function for common per-line checks. It goes through all |
| 407 | files and returns a HookFailure with the error description listing all the |
| 408 | failures. |
| 409 | |
| 410 | Args: |
| 411 | commit: The commit we're working on. |
| 412 | files: The files to check. |
| 413 | check_callable: A callable that takes a line and returns True if this line |
| 414 | _fails_ the check. |
| 415 | error_description: A string describing the error. |
| 416 | """ |
| 417 | errors = [] |
| 418 | for afile in files: |
| 419 | for line_num, line in _get_file_diff(afile, commit): |
| 420 | if check_callable(line): |
| 421 | errors.append('%s, line %s' % (afile, line_num)) |
| 422 | if errors: |
| 423 | return HookFailure(error_description, errors) |
| 424 | |
| 425 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 426 | # Common Hooks |
| 427 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 428 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 429 | def _check_no_long_lines(_project, commit): |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 430 | """Checks there are no lines longer than MAX_LEN in any of the text files.""" |
| 431 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 432 | MAX_LEN = 80 |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 433 | SKIP_REGEXP = re.compile('|'.join([ |
| 434 | r'https?://', |
| 435 | r'^#\s*(define|include|import|pragma|if|endif)\b'])) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 436 | |
| 437 | errors = [] |
| 438 | files = _filter_files(_get_affected_files(commit), |
| 439 | COMMON_INCLUDED_PATHS, |
| 440 | COMMON_EXCLUDED_PATHS) |
| 441 | |
| 442 | for afile in files: |
| 443 | for line_num, line in _get_file_diff(afile, commit): |
| 444 | # Allow certain lines to exceed the maxlen rule. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 445 | if len(line) <= MAX_LEN or SKIP_REGEXP.search(line): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 446 | continue |
| 447 | |
| 448 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 449 | if len(errors) == 5: # Just show the first 5 errors. |
| 450 | break |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 451 | |
| 452 | if errors: |
| 453 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 454 | return HookFailure(msg, errors) |
| 455 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 456 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 457 | def _check_no_stray_whitespace(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 458 | """Checks that there is no stray whitespace at source lines end.""" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 459 | files = _filter_files(_get_affected_files(commit), |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 460 | COMMON_INCLUDED_PATHS, |
| 461 | COMMON_EXCLUDED_PATHS) |
Prathmesh Prabhu | c525465 | 2016-12-22 12:58:05 -0800 | [diff] [blame] | 462 | return _check_lines_in_diff(commit, files, |
| 463 | lambda line: line.rstrip() != line, |
| 464 | 'Found line ending with white space in:') |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 465 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 466 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 467 | def _check_no_tabs(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 468 | """Checks there are no unexpanded tabs.""" |
| 469 | TAB_OK_PATHS = [ |
Ryan Cui | 31e0c17 | 2011-05-04 21:00:45 -0700 | [diff] [blame] | 470 | r"/src/third_party/u-boot/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 471 | r".*\.ebuild$", |
| 472 | r".*\.eclass$", |
Elly Jones | 5ab3419 | 2011-11-15 14:57:06 -0500 | [diff] [blame] | 473 | r".*/[M|m]akefile$", |
| 474 | r".*\.mk$" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 475 | ] |
| 476 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 477 | files = _filter_files(_get_affected_files(commit), |
| 478 | COMMON_INCLUDED_PATHS, |
| 479 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
Prathmesh Prabhu | c525465 | 2016-12-22 12:58:05 -0800 | [diff] [blame] | 480 | return _check_lines_in_diff(commit, files, |
| 481 | lambda line: '\t' in line, |
| 482 | 'Found a tab character in:') |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 483 | |
Prathmesh Prabhu | c525465 | 2016-12-22 12:58:05 -0800 | [diff] [blame] | 484 | |
| 485 | def _check_tabbed_indents(_project, commit): |
| 486 | """Checks that indents use tabs only.""" |
| 487 | TABS_REQUIRED_PATHS = [ |
| 488 | r".*\.ebuild$", |
| 489 | r".*\.eclass$", |
| 490 | ] |
| 491 | LEADING_SPACE_RE = re.compile('[\t]* ') |
| 492 | |
| 493 | files = _filter_files(_get_affected_files(commit), |
| 494 | TABS_REQUIRED_PATHS, |
| 495 | COMMON_EXCLUDED_PATHS) |
| 496 | return _check_lines_in_diff( |
| 497 | commit, files, |
| 498 | lambda line: LEADING_SPACE_RE.match(line) is not None, |
| 499 | 'Found a space in indentation (must be all tabs):') |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 500 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 501 | |
Rahul Chaudhry | 09f6137 | 2015-07-31 17:14:26 -0700 | [diff] [blame] | 502 | def _check_gofmt(_project, commit): |
| 503 | """Checks that Go files are formatted with gofmt.""" |
| 504 | errors = [] |
| 505 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 506 | [r'\.go$']) |
| 507 | |
| 508 | for gofile in files: |
| 509 | contents = _get_file_content(gofile, commit) |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 510 | output = _run_command(cmd=['gofmt', '-l'], input=contents, |
| 511 | combine_stdout_stderr=True) |
Rahul Chaudhry | 09f6137 | 2015-07-31 17:14:26 -0700 | [diff] [blame] | 512 | if output: |
| 513 | errors.append(gofile) |
| 514 | if errors: |
| 515 | return HookFailure('Files not formatted with gofmt:', errors) |
| 516 | |
| 517 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 518 | def _check_change_has_test_field(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 519 | """Check for a non-empty 'TEST=' field in the commit message.""" |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 520 | TEST_RE = r'\nTEST=\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 521 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 522 | if not re.search(TEST_RE, _get_commit_desc(commit)): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 523 | msg = 'Changelist description needs TEST field (after first line)' |
| 524 | return HookFailure(msg) |
| 525 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 526 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 527 | def _check_change_has_valid_cq_depend(_project, commit): |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 528 | """Check for a correctly formatted CQ-DEPEND field in the commit message.""" |
| 529 | msg = 'Changelist has invalid CQ-DEPEND target.' |
| 530 | example = 'Example: CQ-DEPEND=CL:1234, CL:2345' |
| 531 | try: |
| 532 | patch.GetPaladinDeps(_get_commit_desc(commit)) |
| 533 | except ValueError as ex: |
| 534 | return HookFailure(msg, [example, str(ex)]) |
| 535 | |
| 536 | |
Bernie Thompson | f8fea99 | 2016-01-14 10:27:18 -0800 | [diff] [blame] | 537 | def _check_change_is_contribution(_project, commit): |
| 538 | """Check that the change is a contribution.""" |
| 539 | NO_CONTRIB = 'not a contribution' |
| 540 | if NO_CONTRIB in _get_commit_desc(commit).lower(): |
| 541 | msg = ('Changelist is not a contribution, this cannot be accepted.\n' |
| 542 | 'Please remove the "%s" text from the commit message.') % NO_CONTRIB |
| 543 | return HookFailure(msg) |
| 544 | |
| 545 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 546 | def _check_change_has_bug_field(project, commit): |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 547 | """Check for a correctly formatted 'BUG=' field in the commit message.""" |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 548 | OLD_BUG_RE = r'\nBUG=.*chromium-os' |
| 549 | if re.search(OLD_BUG_RE, _get_commit_desc(commit)): |
| 550 | msg = ('The chromium-os bug tracker is now deprecated. Please use\n' |
| 551 | 'the chromium tracker in your BUG= line now.') |
| 552 | return HookFailure(msg) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 553 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 554 | # Android internal and external projects use "Bug: " to track bugs in |
| 555 | # buganizer. |
| 556 | BUG_COLON_REMOTES = ( |
| 557 | 'aosp', |
| 558 | 'goog', |
| 559 | ) |
| 560 | if project.remote in BUG_COLON_REMOTES: |
| 561 | BUG_RE = r'\nBug: ?([Nn]one|\d+)' |
| 562 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
| 563 | msg = ('Changelist description needs BUG field (after first line):\n' |
| 564 | 'Bug: 9999 (for buganizer)\n' |
| 565 | 'BUG=None') |
| 566 | return HookFailure(msg) |
| 567 | else: |
| 568 | BUG_RE = r'\nBUG=([Nn]one|(chrome-os-partner|chromium|brillo|b):\d+)' |
| 569 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
| 570 | msg = ('Changelist description needs BUG field (after first line):\n' |
| 571 | 'BUG=brillo:9999 (for Brillo tracker)\n' |
| 572 | 'BUG=chromium:9999 (for public tracker)\n' |
| 573 | 'BUG=chrome-os-partner:9999 (for partner tracker)\n' |
| 574 | 'BUG=b:9999 (for buganizer)\n' |
| 575 | 'BUG=None') |
| 576 | return HookFailure(msg) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 577 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 578 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 579 | def _check_for_uprev(project, commit, project_top=None): |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 580 | """Check that we're not missing a revbump of an ebuild in the given commit. |
| 581 | |
| 582 | If the given commit touches files in a directory that has ebuilds somewhere |
| 583 | up the directory hierarchy, it's very likely that we need an ebuild revbump |
| 584 | in order for those changes to take effect. |
| 585 | |
| 586 | It's not totally trivial to detect a revbump, so at least detect that an |
| 587 | ebuild with a revision number in it was touched. This should handle the |
| 588 | common case where we use a symlink to do the revbump. |
| 589 | |
| 590 | TODO: it would be nice to enhance this hook to: |
| 591 | * Handle cases where people revbump with a slightly different syntax. I see |
| 592 | one ebuild (puppy) that revbumps with _pN. This is a false positive. |
| 593 | * Catches cases where people aren't using symlinks for revbumps. If they |
| 594 | edit a revisioned file directly (and are expected to rename it for revbump) |
| 595 | we'll miss that. Perhaps we could detect that the file touched is a |
| 596 | symlink? |
| 597 | |
| 598 | If a project doesn't use symlinks we'll potentially miss a revbump, but we're |
| 599 | still better off than without this check. |
| 600 | |
| 601 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 602 | project: The Project to look at |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 603 | commit: The commit to look at |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 604 | project_top: Top dir to process commits in |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 605 | |
| 606 | Returns: |
| 607 | A HookFailure or None. |
| 608 | """ |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 609 | # If this is the portage-stable overlay, then ignore the check. It's rare |
| 610 | # that we're doing anything other than importing files from upstream, so |
| 611 | # forcing a rev bump makes no sense. |
| 612 | whitelist = ( |
| 613 | 'chromiumos/overlays/portage-stable', |
| 614 | ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 615 | if project.name in whitelist: |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 616 | return None |
| 617 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 618 | def FinalName(obj): |
| 619 | # If the file is being deleted, then the dst_file is not set. |
| 620 | if obj.dst_file is None: |
| 621 | return obj.src_file |
| 622 | else: |
| 623 | return obj.dst_file |
| 624 | |
| 625 | affected_path_objs = _get_affected_files( |
| 626 | commit, include_deletes=True, include_symlinks=True, relative=True, |
| 627 | full_details=True) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 628 | |
| 629 | # Don't yell about changes to whitelisted files... |
Aviv Keshet | 272f2e5 | 2016-04-25 14:49:44 -0700 | [diff] [blame] | 630 | whitelist = ('ChangeLog', 'Manifest', 'metadata.xml', 'COMMIT-QUEUE.ini') |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 631 | affected_path_objs = [x for x in affected_path_objs |
| 632 | if os.path.basename(FinalName(x)) not in whitelist] |
| 633 | if not affected_path_objs: |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 634 | return None |
| 635 | |
| 636 | # If we've touched any file named with a -rN.ebuild then we'll say we're |
| 637 | # OK right away. See TODO above about enhancing this. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 638 | touched_revved_ebuild = any(re.search(r'-r\d*\.ebuild$', FinalName(x)) |
| 639 | for x in affected_path_objs) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 640 | if touched_revved_ebuild: |
| 641 | return None |
| 642 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 643 | # If we're creating new ebuilds from scratch, then we don't need an uprev. |
| 644 | # Find all the dirs that new ebuilds and ignore their files/. |
| 645 | ebuild_dirs = [os.path.dirname(FinalName(x)) + '/' for x in affected_path_objs |
| 646 | if FinalName(x).endswith('.ebuild') and x.status == 'A'] |
| 647 | affected_path_objs = [obj for obj in affected_path_objs |
| 648 | if not any(FinalName(obj).startswith(x) |
| 649 | for x in ebuild_dirs)] |
| 650 | if not affected_path_objs: |
| 651 | return |
| 652 | |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 653 | # We want to examine the current contents of all directories that are parents |
| 654 | # of files that were touched (up to the top of the project). |
| 655 | # |
| 656 | # ...note: we use the current directory contents even though it may have |
| 657 | # changed since the commit we're looking at. This is just a heuristic after |
| 658 | # all. Worst case we don't flag a missing revbump. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 659 | if project_top is None: |
| 660 | project_top = os.getcwd() |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 661 | dirs_to_check = set([project_top]) |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 662 | for obj in affected_path_objs: |
| 663 | path = os.path.join(project_top, os.path.dirname(FinalName(obj))) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 664 | while os.path.exists(path) and not os.path.samefile(path, project_top): |
| 665 | dirs_to_check.add(path) |
| 666 | path = os.path.dirname(path) |
| 667 | |
| 668 | # Look through each directory. If it's got an ebuild in it then we'll |
| 669 | # consider this as a case when we need a revbump. |
Gwendal Grignou | a3086c3 | 2014-12-09 11:17:22 -0800 | [diff] [blame] | 670 | affected_paths = set(os.path.join(project_top, FinalName(x)) |
| 671 | for x in affected_path_objs) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 672 | for dir_path in dirs_to_check: |
| 673 | contents = os.listdir(dir_path) |
| 674 | ebuilds = [os.path.join(dir_path, path) |
| 675 | for path in contents if path.endswith('.ebuild')] |
| 676 | ebuilds_9999 = [path for path in ebuilds if path.endswith('-9999.ebuild')] |
| 677 | |
| 678 | # If the -9999.ebuild file was touched the bot will uprev for us. |
| 679 | # ...we'll use a simple intersection here as a heuristic... |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 680 | if set(ebuilds_9999) & affected_paths: |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 681 | continue |
| 682 | |
| 683 | if ebuilds: |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 684 | return HookFailure('Changelist probably needs a revbump of an ebuild, ' |
| 685 | 'or a -r1.ebuild symlink if this is a new ebuild:\n' |
| 686 | '%s' % dir_path) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 687 | |
| 688 | return None |
| 689 | |
| 690 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 691 | def _check_ebuild_eapi(project, commit): |
| 692 | """Make sure we have people use EAPI=4 or newer with custom ebuilds. |
| 693 | |
| 694 | We want to get away from older EAPI's as it makes life confusing and they |
| 695 | have less builtin error checking. |
| 696 | |
| 697 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 698 | project: The Project to look at |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 699 | commit: The commit to look at |
| 700 | |
| 701 | Returns: |
| 702 | A HookFailure or None. |
| 703 | """ |
| 704 | # 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] | 705 | # that we're doing anything other than importing files from upstream, and |
| 706 | # we shouldn't be rewriting things fundamentally anyways. |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 707 | whitelist = ( |
| 708 | 'chromiumos/overlays/portage-stable', |
| 709 | ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 710 | if project.name in whitelist: |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 711 | return None |
| 712 | |
| 713 | BAD_EAPIS = ('0', '1', '2', '3') |
| 714 | |
| 715 | get_eapi = re.compile(r'^\s*EAPI=[\'"]?([^\'"]+)') |
| 716 | |
| 717 | ebuilds_re = [r'\.ebuild$'] |
| 718 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 719 | ebuilds_re) |
| 720 | bad_ebuilds = [] |
| 721 | |
| 722 | for ebuild in ebuilds: |
| 723 | # If the ebuild does not specify an EAPI, it defaults to 0. |
| 724 | eapi = '0' |
| 725 | |
| 726 | lines = _get_file_content(ebuild, commit).splitlines() |
| 727 | if len(lines) == 1: |
| 728 | # This is most likely a symlink, so skip it entirely. |
| 729 | continue |
| 730 | |
| 731 | for line in lines: |
| 732 | m = get_eapi.match(line) |
| 733 | if m: |
| 734 | # Once we hit the first EAPI line in this ebuild, stop processing. |
| 735 | # The spec requires that there only be one and it be first, so |
| 736 | # checking all possible values is pointless. We also assume that |
| 737 | # it's "the" EAPI line and not something in the middle of a heredoc. |
| 738 | eapi = m.group(1) |
| 739 | break |
| 740 | |
| 741 | if eapi in BAD_EAPIS: |
| 742 | bad_ebuilds.append((ebuild, eapi)) |
| 743 | |
| 744 | if bad_ebuilds: |
| 745 | # pylint: disable=C0301 |
| 746 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/upgrade-ebuild-eapis' |
| 747 | # pylint: enable=C0301 |
| 748 | return HookFailure( |
Mike Frysinger | cd6adfc | 2014-02-06 01:03:56 -0500 | [diff] [blame] | 749 | 'These ebuilds are using old EAPIs. If these are imported from\n' |
| 750 | 'Gentoo, then you may ignore and upload once with the --no-verify\n' |
| 751 | 'flag. Otherwise, please update to 4 or newer.\n' |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 752 | '\t%s\n' |
| 753 | 'See this guide for more details:\n%s\n' % |
| 754 | ('\n\t'.join(['%s: EAPI=%s' % x for x in bad_ebuilds]), url)) |
| 755 | |
| 756 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 757 | def _check_ebuild_keywords(_project, commit): |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 758 | """Make sure we use the new style KEYWORDS when possible in ebuilds. |
| 759 | |
| 760 | If an ebuild generally does not care about the arch it is running on, then |
| 761 | ebuilds should flag it with one of: |
| 762 | KEYWORDS="*" # A stable ebuild. |
| 763 | KEYWORDS="~*" # An unstable ebuild. |
| 764 | KEYWORDS="-* ..." # Is known to only work on specific arches. |
| 765 | |
| 766 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 767 | project: The Project to look at |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 768 | commit: The commit to look at |
| 769 | |
| 770 | Returns: |
| 771 | A HookFailure or None. |
| 772 | """ |
| 773 | WHITELIST = set(('*', '-*', '~*')) |
| 774 | |
| 775 | get_keywords = re.compile(r'^\s*KEYWORDS="(.*)"') |
| 776 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 777 | ebuilds_re = [r'\.ebuild$'] |
| 778 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 779 | ebuilds_re) |
| 780 | |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 781 | bad_ebuilds = [] |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 782 | for ebuild in ebuilds: |
Mike Frysinger | 5c9e58d | 2014-09-09 03:32:50 -0400 | [diff] [blame] | 783 | # We get the full content rather than a diff as the latter does not work |
| 784 | # on new files (like when adding new ebuilds). |
| 785 | lines = _get_file_content(ebuild, commit).splitlines() |
| 786 | for line in lines: |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 787 | m = get_keywords.match(line) |
| 788 | if m: |
| 789 | keywords = set(m.group(1).split()) |
| 790 | if not keywords or WHITELIST - keywords != WHITELIST: |
| 791 | continue |
| 792 | |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 793 | bad_ebuilds.append(ebuild) |
| 794 | |
| 795 | if bad_ebuilds: |
| 796 | return HookFailure( |
| 797 | '%s\n' |
| 798 | 'Please update KEYWORDS to use a glob:\n' |
| 799 | 'If the ebuild should be marked stable (normal for non-9999 ebuilds):\n' |
| 800 | ' KEYWORDS="*"\n' |
| 801 | 'If the ebuild should be marked unstable (normal for ' |
| 802 | 'cros-workon / 9999 ebuilds):\n' |
| 803 | ' KEYWORDS="~*"\n' |
Mike Frysinger | de8efea | 2015-05-17 03:42:26 -0400 | [diff] [blame] | 804 | 'If the ebuild needs to be marked for only specific arches, ' |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 805 | 'then use -* like so:\n' |
| 806 | ' KEYWORDS="-* arm ..."\n' % '\n* '.join(bad_ebuilds)) |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 807 | |
| 808 | |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 809 | def _check_ebuild_licenses(_project, commit): |
| 810 | """Check if the LICENSE field in the ebuild is correct.""" |
Brian Norris | 7a610e8 | 2016-02-17 12:24:54 -0800 | [diff] [blame] | 811 | affected_paths = _get_affected_files(commit, relative=True) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 812 | touched_ebuilds = [x for x in affected_paths if x.endswith('.ebuild')] |
| 813 | |
| 814 | # A list of licenses to ignore for now. |
Yu-Ju Hong | c0963fa | 2014-03-03 12:36:52 -0800 | [diff] [blame] | 815 | LICENSES_IGNORE = ['||', '(', ')'] |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 816 | |
| 817 | for ebuild in touched_ebuilds: |
| 818 | # Skip virutal packages. |
| 819 | if ebuild.split('/')[-3] == 'virtual': |
| 820 | continue |
| 821 | |
| 822 | try: |
Brian Norris | 7a610e8 | 2016-02-17 12:24:54 -0800 | [diff] [blame] | 823 | ebuild_content = _get_file_content(ebuild, commit) |
| 824 | license_types = licenses_lib.GetLicenseTypesFromEbuild(ebuild_content) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 825 | except ValueError as e: |
| 826 | return HookFailure(e.message, [ebuild]) |
| 827 | |
| 828 | # Also ignore licenses ending with '?' |
| 829 | for license_type in [x for x in license_types |
| 830 | if x not in LICENSES_IGNORE and not x.endswith('?')]: |
| 831 | try: |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 832 | licenses_lib.Licensing.FindLicenseType(license_type) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 833 | except AssertionError as e: |
| 834 | return HookFailure(e.message, [ebuild]) |
| 835 | |
| 836 | |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 837 | def _check_ebuild_virtual_pv(project, commit): |
| 838 | """Enforce the virtual PV policies.""" |
| 839 | # If this is the portage-stable overlay, then ignore the check. |
| 840 | # We want to import virtuals as-is from upstream Gentoo. |
| 841 | whitelist = ( |
| 842 | 'chromiumos/overlays/portage-stable', |
| 843 | ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 844 | if project.name in whitelist: |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 845 | return None |
| 846 | |
| 847 | # We assume the repo name is the same as the dir name on disk. |
| 848 | # It would be dumb to not have them match though. |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 849 | project_base = os.path.basename(project.name) |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 850 | |
| 851 | is_variant = lambda x: x.startswith('overlay-variant-') |
| 852 | is_board = lambda x: x.startswith('overlay-') |
| 853 | is_private = lambda x: x.endswith('-private') |
| 854 | |
| 855 | get_pv = re.compile(r'(.*?)virtual/([^/]+)/\2-([^/]*)\.ebuild$') |
| 856 | |
| 857 | ebuilds_re = [r'\.ebuild$'] |
| 858 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 859 | ebuilds_re) |
| 860 | bad_ebuilds = [] |
| 861 | |
| 862 | for ebuild in ebuilds: |
| 863 | m = get_pv.match(ebuild) |
| 864 | if m: |
| 865 | overlay = m.group(1) |
| 866 | if not overlay or not is_board(overlay): |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 867 | overlay = project_base |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 868 | |
| 869 | pv = m.group(3).split('-', 1)[0] |
| 870 | |
Bernie Thompson | e5ee182 | 2016-01-12 14:22:23 -0800 | [diff] [blame] | 871 | # Virtual versions >= 4 are special cases used above the standard |
| 872 | # versioning structure, e.g. if one has a board inheriting a board. |
| 873 | if float(pv) >= 4: |
| 874 | want_pv = pv |
| 875 | elif is_private(overlay): |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 876 | want_pv = '3.5' if is_variant(overlay) else '3' |
| 877 | elif is_board(overlay): |
| 878 | want_pv = '2.5' if is_variant(overlay) else '2' |
| 879 | else: |
| 880 | want_pv = '1' |
| 881 | |
| 882 | if pv != want_pv: |
| 883 | bad_ebuilds.append((ebuild, pv, want_pv)) |
| 884 | |
| 885 | if bad_ebuilds: |
| 886 | # pylint: disable=C0301 |
| 887 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/portage-build-faq#TOC-Virtuals-and-central-management' |
| 888 | # pylint: enable=C0301 |
| 889 | return HookFailure( |
| 890 | 'These virtuals have incorrect package versions (PVs). Please adjust:\n' |
| 891 | '\t%s\n' |
| 892 | 'If this is an upstream Gentoo virtual, then you may ignore this\n' |
| 893 | 'check (and re-run w/--no-verify). Otherwise, please see this\n' |
| 894 | 'page for more details:\n%s\n' % |
| 895 | ('\n\t'.join(['%s:\n\t\tPV is %s but should be %s' % x |
| 896 | for x in bad_ebuilds]), url)) |
| 897 | |
| 898 | |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 899 | def _check_portage_make_use_var(_project, commit): |
| 900 | """Verify that $USE is set correctly in make.conf and make.defaults.""" |
| 901 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 902 | [r'(^|/)make.(conf|defaults)$']) |
| 903 | |
| 904 | errors = [] |
| 905 | for path in files: |
| 906 | basename = os.path.basename(path) |
| 907 | |
| 908 | # Has a USE= line already been encountered in this file? |
| 909 | saw_use = False |
| 910 | |
| 911 | for i, line in enumerate(_get_file_content(path, commit).splitlines(), 1): |
| 912 | if not line.startswith('USE='): |
| 913 | continue |
| 914 | |
| 915 | preserves_use = '${USE}' in line or '$USE' in line |
| 916 | |
| 917 | if (basename == 'make.conf' or |
| 918 | (basename == 'make.defaults' and saw_use)) and not preserves_use: |
| 919 | errors.append('%s:%d: missing ${USE}' % (path, i)) |
| 920 | elif basename == 'make.defaults' and not saw_use and preserves_use: |
| 921 | errors.append('%s:%d: ${USE} referenced in initial declaration' % |
| 922 | (path, i)) |
| 923 | |
| 924 | saw_use = True |
| 925 | |
| 926 | if errors: |
| 927 | return HookFailure( |
| 928 | 'One or more Portage make files appear to set USE incorrectly.\n' |
| 929 | '\n' |
| 930 | 'All USE assignments in make.conf and all assignments after the\n' |
| 931 | 'initial declaration in make.defaults should contain "${USE}" to\n' |
| 932 | 'preserve previously-set flags.\n' |
| 933 | '\n' |
| 934 | 'The initial USE declaration in make.defaults should not contain\n' |
| 935 | '"${USE}".\n', |
| 936 | errors) |
| 937 | |
| 938 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 939 | def _check_change_has_proper_changeid(_project, commit): |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 940 | """Verify that Change-ID is present in last paragraph of commit message.""" |
Mike Frysinger | 4a22bf0 | 2014-10-31 13:53:35 -0400 | [diff] [blame] | 941 | CHANGE_ID_RE = r'\nChange-Id: I[a-f0-9]+\n' |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 942 | desc = _get_commit_desc(commit) |
Mike Frysinger | 4a22bf0 | 2014-10-31 13:53:35 -0400 | [diff] [blame] | 943 | m = re.search(CHANGE_ID_RE, desc) |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 944 | if not m: |
Vadim Bendebury | 20532ba | 2017-05-23 17:26:15 -0700 | [diff] [blame] | 945 | return HookFailure('Last paragraph of description must include Change-Id.') |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 946 | |
Vadim Bendebury | 20532ba | 2017-05-23 17:26:15 -0700 | [diff] [blame] | 947 | # S-o-b tags always allowed to follow Change-ID. |
| 948 | allowed_tags = ['Signed-off-by'] |
| 949 | |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 950 | end = desc[m.end():].strip().splitlines() |
Vadim Bendebury | 20532ba | 2017-05-23 17:26:15 -0700 | [diff] [blame] | 951 | if end and end[-1].startswith('(cherry picked from commit'): |
| 952 | # Cherry picked patches allow more tags in the last paragraph. |
| 953 | allowed_tags += ['Reviewed-on', 'Reviewed-by', 'Commit-Ready', 'Tested-by'] |
| 954 | end = end[:-1] |
| 955 | |
| 956 | tag_search = '^(%s): ' % '|'.join(allowed_tags) |
| 957 | |
| 958 | if [x for x in end if not re.search(tag_search, x)]: |
| 959 | return HookFailure('Only "%s:" tag(s) may follow the Change-Id.' % |
| 960 | ':", "'.join(allowed_tags)) |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 961 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 962 | |
Mike Frysinger | 36b2ebc | 2014-10-31 14:02:03 -0400 | [diff] [blame] | 963 | def _check_commit_message_style(_project, commit): |
| 964 | """Verify that the commit message matches our style. |
| 965 | |
| 966 | We do not check for BUG=/TEST=/etc... lines here as that is handled by other |
| 967 | commit hooks. |
| 968 | """ |
| 969 | desc = _get_commit_desc(commit) |
| 970 | |
| 971 | # The first line should be by itself. |
| 972 | lines = desc.splitlines() |
| 973 | if len(lines) > 1 and lines[1]: |
| 974 | return HookFailure('The second line of the commit message must be blank.') |
| 975 | |
| 976 | # The first line should be one sentence. |
| 977 | if '. ' in lines[0]: |
| 978 | return HookFailure('The first line cannot be more than one sentence.') |
| 979 | |
| 980 | # The first line cannot be too long. |
| 981 | MAX_FIRST_LINE_LEN = 100 |
| 982 | if len(lines[0]) > MAX_FIRST_LINE_LEN: |
| 983 | return HookFailure('The first line must be less than %i chars.' % |
| 984 | MAX_FIRST_LINE_LEN) |
| 985 | |
| 986 | |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 987 | def _check_cros_license(_project, commit, options=()): |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 988 | """Verifies the Chromium OS license/copyright header. |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 989 | |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 990 | Should be following the spec: |
| 991 | http://dev.chromium.org/developers/coding-style#TOC-File-headers |
| 992 | """ |
| 993 | # For older years, be a bit more flexible as our policy says leave them be. |
| 994 | LICENSE_HEADER = ( |
| 995 | r'.* Copyright( \(c\))? 20[-0-9]{2,7} The Chromium OS Authors\. ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 996 | r'All rights reserved\.' r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 997 | r'.* Use of this source code is governed by a BSD-style license that can ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 998 | r'be\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 999 | r'.* found in the LICENSE file\.' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 1000 | r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 1001 | ) |
| 1002 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 1003 | |
| 1004 | # For newer years, be stricter. |
| 1005 | COPYRIGHT_LINE = ( |
| 1006 | r'.* Copyright \(c\) 20(1[5-9]|[2-9][0-9]) The Chromium OS Authors\. ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 1007 | r'All rights reserved\.' r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 1008 | ) |
| 1009 | copyright_re = re.compile(COPYRIGHT_LINE) |
| 1010 | |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 1011 | parser = argparse.ArgumentParser() |
| 1012 | parser.add_argument('--exclude_regex', action='append') |
| 1013 | parser.add_argument('--include_regex', action='append') |
| 1014 | opts = parser.parse_args(options) |
| 1015 | included = opts.include_regex or [] |
| 1016 | excluded = opts.exclude_regex or [] |
| 1017 | |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 1018 | bad_files = [] |
| 1019 | bad_copyright_files = [] |
| 1020 | files = _filter_files(_get_affected_files(commit, relative=True), |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 1021 | included + COMMON_INCLUDED_PATHS, |
| 1022 | excluded + COMMON_EXCLUDED_PATHS) |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 1023 | |
| 1024 | for f in files: |
| 1025 | contents = _get_file_content(f, commit) |
| 1026 | if not contents: |
| 1027 | # Ignore empty files. |
| 1028 | continue |
| 1029 | |
| 1030 | if not license_re.search(contents): |
| 1031 | bad_files.append(f) |
| 1032 | elif copyright_re.search(contents): |
| 1033 | bad_copyright_files.append(f) |
| 1034 | |
| 1035 | if bad_files: |
| 1036 | msg = '%s:\n%s\n%s' % ( |
| 1037 | 'License must match', license_re.pattern, |
| 1038 | 'Found a bad header in these files:') |
| 1039 | return HookFailure(msg, bad_files) |
| 1040 | |
| 1041 | if bad_copyright_files: |
| 1042 | msg = 'Do not use (c) in copyright headers in new files:' |
| 1043 | return HookFailure(msg, bad_copyright_files) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1044 | |
| 1045 | |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 1046 | def _check_aosp_license(_project, commit): |
| 1047 | """Verifies the AOSP license/copyright header. |
| 1048 | |
| 1049 | AOSP uses the Apache2 License: |
| 1050 | https://source.android.com/source/licenses.html |
| 1051 | """ |
| 1052 | LICENSE_HEADER = ( |
| 1053 | r"""^[#/\*]* |
| 1054 | [#/\*]* ?Copyright( \([cC]\))? 20[-0-9]{2,7} The Android Open Source Project |
| 1055 | [#/\*]* ? |
| 1056 | [#/\*]* ?Licensed under the Apache License, Version 2.0 \(the "License"\); |
| 1057 | [#/\*]* ?you may not use this file except in compliance with the License\. |
| 1058 | [#/\*]* ?You may obtain a copy of the License at |
| 1059 | [#/\*]* ? |
| 1060 | [#/\*]* ? http://www\.apache\.org/licenses/LICENSE-2\.0 |
| 1061 | [#/\*]* ? |
| 1062 | [#/\*]* ?Unless required by applicable law or agreed to in writing, software |
| 1063 | [#/\*]* ?distributed under the License is distributed on an "AS IS" BASIS, |
| 1064 | [#/\*]* ?WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or """ |
| 1065 | r"""implied\. |
| 1066 | [#/\*]* ?See the License for the specific language governing permissions and |
| 1067 | [#/\*]* ?limitations under the License\. |
| 1068 | [#/\*]*$ |
| 1069 | """ |
| 1070 | ) |
| 1071 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 1072 | |
| 1073 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 1074 | COMMON_INCLUDED_PATHS, |
| 1075 | COMMON_EXCLUDED_PATHS) |
| 1076 | |
| 1077 | bad_files = [] |
| 1078 | for f in files: |
| 1079 | contents = _get_file_content(f, commit) |
| 1080 | if not contents: |
| 1081 | # Ignore empty files. |
| 1082 | continue |
| 1083 | |
| 1084 | if not license_re.search(contents): |
| 1085 | bad_files.append(f) |
| 1086 | |
| 1087 | if bad_files: |
| 1088 | msg = ('License must match:\n%s\nFound a bad header in these files:' % |
| 1089 | license_re.pattern) |
| 1090 | return HookFailure(msg, bad_files) |
| 1091 | |
| 1092 | |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1093 | def _check_layout_conf(_project, commit): |
| 1094 | """Verifies the metadata/layout.conf file.""" |
| 1095 | repo_name = 'profiles/repo_name' |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1096 | repo_names = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1097 | layout_path = 'metadata/layout.conf' |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1098 | layout_paths = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1099 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1100 | # Handle multiple overlays in a single commit (like the public tree). |
| 1101 | for f in _get_affected_files(commit, relative=True): |
| 1102 | if f.endswith(repo_name): |
| 1103 | repo_names.append(f) |
| 1104 | elif f.endswith(layout_path): |
| 1105 | layout_paths.append(f) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1106 | |
| 1107 | # Disallow new repos with the repo_name file. |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1108 | if repo_names: |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1109 | return HookFailure('%s: use "repo-name" in %s instead' % |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1110 | (repo_names, layout_path)) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1111 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1112 | # Gather all the errors in one pass so we show one full message. |
| 1113 | all_errors = {} |
| 1114 | for layout_path in layout_paths: |
| 1115 | all_errors[layout_path] = errors = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1116 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1117 | # Make sure the config file is sorted. |
| 1118 | data = [x for x in _get_file_content(layout_path, commit).splitlines() |
| 1119 | if x and x[0] != '#'] |
| 1120 | if sorted(data) != data: |
| 1121 | errors += ['keep lines sorted'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1122 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1123 | # Require people to set specific values all the time. |
| 1124 | settings = ( |
| 1125 | # TODO: Enable this for everyone. http://crbug.com/408038 |
| 1126 | #('fast caching', 'cache-format = md5-dict'), |
| 1127 | ('fast manifests', 'thin-manifests = true'), |
Mike Frysinger | d773452 | 2015-02-26 16:12:43 -0500 | [diff] [blame] | 1128 | ('extra features', 'profile-formats = portage-2 profile-default-eapi'), |
| 1129 | ('newer eapi', 'profile_eapi_when_unspecified = 5-progress'), |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1130 | ) |
| 1131 | for reason, line in settings: |
| 1132 | if line not in data: |
| 1133 | errors += ['enable %s with: %s' % (reason, line)] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1134 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1135 | # Require one of these settings. |
Mike Frysinger | 5fae62d | 2015-11-11 20:12:15 -0500 | [diff] [blame] | 1136 | if 'use-manifests = strict' not in data: |
| 1137 | errors += ['enable file checking with: use-manifests = strict'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1138 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1139 | # Require repo-name to be set. |
Mike Frysinger | 324cf68 | 2014-09-22 15:52:50 -0400 | [diff] [blame] | 1140 | for line in data: |
| 1141 | if line.startswith('repo-name = '): |
| 1142 | break |
| 1143 | else: |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1144 | errors += ['set the board name with: repo-name = $BOARD'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1145 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1146 | # Summarize all the errors we saw (if any). |
| 1147 | lines = '' |
| 1148 | for layout_path, errors in all_errors.items(): |
| 1149 | if errors: |
| 1150 | lines += '\n\t- '.join(['\n* %s:' % layout_path] + errors) |
| 1151 | if lines: |
| 1152 | lines = 'See the portage(5) man page for layout.conf details' + lines + '\n' |
| 1153 | return HookFailure(lines) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1154 | |
| 1155 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1156 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1157 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1158 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1159 | def _run_checkpatch(_project, commit, options=()): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1160 | """Runs checkpatch.pl on the given project""" |
| 1161 | hooks_dir = _get_hooks_dir() |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1162 | options = list(options) |
| 1163 | if commit == PRE_SUBMIT: |
| 1164 | # The --ignore option must be present and include 'MISSING_SIGN_OFF' in |
| 1165 | # this case. |
| 1166 | options.append('--ignore=MISSING_SIGN_OFF') |
Filipe Brandenburger | 28d48d6 | 2015-10-07 09:48:54 -0700 | [diff] [blame] | 1167 | # Always ignore the check for the MAINTAINERS file. We do not track that |
| 1168 | # information on that file in our source trees, so let's suppress the |
| 1169 | # warning. |
| 1170 | options.append('--ignore=FILE_PATH_CHANGES') |
Filipe Brandenburger | ce97832 | 2015-10-09 10:04:15 -0700 | [diff] [blame] | 1171 | # Do not complain about the Change-Id: fields, since we use Gerrit. |
| 1172 | # Upstream does not want those lines (since they do not use Gerrit), but |
| 1173 | # we always do, so disable the check globally. |
Daisuke Nojiri | 4844f89 | 2015-10-08 09:54:33 -0700 | [diff] [blame] | 1174 | options.append('--ignore=GERRIT_CHANGE_ID') |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1175 | cmd = ['%s/checkpatch.pl' % hooks_dir] + options + ['-'] |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1176 | cmd_result = cros_build_lib.RunCommand(cmd=cmd, |
| 1177 | print_cmd=False, |
| 1178 | input=_get_patch(commit), |
| 1179 | stdout_to_pipe=True, |
| 1180 | combine_stdout_stderr=True, |
| 1181 | error_code_ok=True) |
| 1182 | if cmd_result.returncode: |
| 1183 | return HookFailure('checkpatch.pl errors/warnings\n\n' + cmd_result.output) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1184 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1185 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1186 | def _kernel_configcheck(_project, commit): |
Olof Johansson | a96810f | 2012-09-04 16:20:03 -0700 | [diff] [blame] | 1187 | """Makes sure kernel config changes are not mixed with code changes""" |
| 1188 | files = _get_affected_files(commit) |
| 1189 | if not len(_filter_files(files, [r'chromeos/config'])) in [0, len(files)]: |
| 1190 | return HookFailure('Changes to chromeos/config/ and regular files must ' |
| 1191 | 'be in separate commits:\n%s' % '\n'.join(files)) |
Anton Staaf | 815d685 | 2011-08-22 10:08:45 -0700 | [diff] [blame] | 1192 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1193 | |
| 1194 | def _run_json_check(_project, commit): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 1195 | """Checks that all JSON files are syntactically valid.""" |
Dale Curtis | a039cfd | 2011-05-04 12:01:05 -0700 | [diff] [blame] | 1196 | for f in _filter_files(_get_affected_files(commit), [r'.*\.json']): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 1197 | try: |
| 1198 | json.load(open(f)) |
| 1199 | except Exception, e: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1200 | return HookFailure('Invalid JSON in %s: %s' % (f, e)) |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 1201 | |
| 1202 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1203 | def _check_manifests(_project, commit): |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 1204 | """Make sure Manifest files only have DIST lines""" |
| 1205 | paths = [] |
| 1206 | |
| 1207 | for path in _get_affected_files(commit): |
| 1208 | if os.path.basename(path) != 'Manifest': |
| 1209 | continue |
| 1210 | if not os.path.exists(path): |
| 1211 | continue |
| 1212 | |
| 1213 | with open(path, 'r') as f: |
| 1214 | for line in f.readlines(): |
| 1215 | if not line.startswith('DIST '): |
| 1216 | paths.append(path) |
| 1217 | break |
| 1218 | |
| 1219 | if paths: |
| 1220 | return HookFailure('Please remove lines that do not start with DIST:\n%s' % |
| 1221 | ('\n'.join(paths),)) |
| 1222 | |
| 1223 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1224 | def _check_change_has_branch_field(_project, commit): |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1225 | """Check for a non-empty 'BRANCH=' field in the commit message.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1226 | if commit == PRE_SUBMIT: |
| 1227 | return |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1228 | BRANCH_RE = r'\nBRANCH=\S+' |
| 1229 | |
| 1230 | if not re.search(BRANCH_RE, _get_commit_desc(commit)): |
| 1231 | msg = ('Changelist description needs BRANCH field (after first line)\n' |
| 1232 | 'E.g. BRANCH=none or BRANCH=link,snow') |
| 1233 | return HookFailure(msg) |
| 1234 | |
| 1235 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1236 | def _check_change_has_signoff_field(_project, commit): |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1237 | """Check for a non-empty 'Signed-off-by:' field in the commit message.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1238 | if commit == PRE_SUBMIT: |
| 1239 | return |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1240 | SIGNOFF_RE = r'\nSigned-off-by: \S+' |
| 1241 | |
| 1242 | if not re.search(SIGNOFF_RE, _get_commit_desc(commit)): |
| 1243 | msg = ('Changelist description needs Signed-off-by: field\n' |
| 1244 | 'E.g. Signed-off-by: My Name <me@chromium.org>') |
| 1245 | return HookFailure(msg) |
| 1246 | |
| 1247 | |
Aviv Keshet | 5ac5952 | 2017-01-31 14:28:27 -0800 | [diff] [blame] | 1248 | def _check_cq_ini_well_formed(_project, commit): |
| 1249 | """Check that any modified COMMIT-QUEUE.ini files are well formed.""" |
| 1250 | pattern = '.*' + constants.CQ_CONFIG_FILENAME |
| 1251 | files = _filter_files(_get_affected_files(commit), (pattern,)) |
| 1252 | |
| 1253 | # TODO(akeshet): Check not only that the file is parseable, but that all the |
| 1254 | # pre-cq configs it requests are existing ones. |
| 1255 | for f in files: |
| 1256 | try: |
| 1257 | parser = ConfigParser.SafeConfigParser() |
| 1258 | # Prior to python3, ConfigParser has no read_string method, so we must |
| 1259 | # pass it either a file path or file like object. And we must use |
| 1260 | # _get_file_content to fetch file contents to ensure we are examining the |
| 1261 | # commit diff, rather than whatever's on disk. |
| 1262 | contents = _get_file_content(f, commit) |
| 1263 | parser.readfp(StringIO.StringIO(contents)) |
| 1264 | except ConfigParser.Error as e: |
| 1265 | msg = ('Unable to parse COMMIT-QUEUE.ini file at %s due to %s.' % |
| 1266 | (f, e)) |
| 1267 | return HookFailure(msg) |
| 1268 | |
| 1269 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1270 | def _run_project_hook_script(script, project, commit): |
| 1271 | """Runs a project hook script. |
| 1272 | |
| 1273 | The script is run with the following environment variables set: |
| 1274 | PRESUBMIT_PROJECT: The affected project |
| 1275 | PRESUBMIT_COMMIT: The affected commit |
| 1276 | PRESUBMIT_FILES: A newline-separated list of affected files |
| 1277 | |
| 1278 | The script is considered to fail if the exit code is non-zero. It should |
| 1279 | write an error message to stdout. |
| 1280 | """ |
| 1281 | env = dict(os.environ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1282 | env['PRESUBMIT_PROJECT'] = project.name |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1283 | env['PRESUBMIT_COMMIT'] = commit |
| 1284 | |
| 1285 | # Put affected files in an environment variable |
| 1286 | files = _get_affected_files(commit) |
| 1287 | env['PRESUBMIT_FILES'] = '\n'.join(files) |
| 1288 | |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1289 | cmd_result = cros_build_lib.RunCommand(cmd=script, |
| 1290 | env=env, |
| 1291 | shell=True, |
| 1292 | print_cmd=False, |
| 1293 | input=os.devnull, |
| 1294 | stdout_to_pipe=True, |
| 1295 | combine_stdout_stderr=True, |
| 1296 | error_code_ok=True) |
| 1297 | if cmd_result.returncode: |
| 1298 | stdout = cmd_result.output |
Jon Salz | 7b618af | 2012-08-31 06:03:16 +0800 | [diff] [blame] | 1299 | if stdout: |
| 1300 | stdout = re.sub('(?m)^', ' ', stdout) |
| 1301 | return HookFailure('Hook script "%s" failed with code %d%s' % |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1302 | (script, cmd_result.returncode, |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1303 | ':\n' + stdout if stdout else '')) |
| 1304 | |
| 1305 | |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1306 | def _check_project_prefix(_project, commit): |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1307 | """Require the commit message have a project specific prefix as needed.""" |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1308 | |
| 1309 | files = _get_affected_files(commit, relative=True) |
| 1310 | prefix = os.path.commonprefix(files) |
| 1311 | prefix = os.path.dirname(prefix) |
| 1312 | |
| 1313 | # If there is no common prefix, the CL span multiple projects. |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 1314 | if not prefix: |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1315 | return |
| 1316 | |
| 1317 | project_name = prefix.split('/')[0] |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 1318 | |
| 1319 | # The common files may all be within a subdirectory of the main project |
| 1320 | # directory, so walk up the tree until we find an alias file. |
| 1321 | # _get_affected_files() should return relative paths, but check against '/' to |
| 1322 | # ensure that this loop terminates even if it receives an absolute path. |
| 1323 | while prefix and prefix != '/': |
| 1324 | alias_file = os.path.join(prefix, '.project_alias') |
| 1325 | |
| 1326 | # If an alias exists, use it. |
| 1327 | if os.path.isfile(alias_file): |
| 1328 | project_name = osutils.ReadFile(alias_file).strip() |
| 1329 | |
| 1330 | prefix = os.path.dirname(prefix) |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1331 | |
| 1332 | if not _get_commit_desc(commit).startswith(project_name + ': '): |
| 1333 | return HookFailure('The commit title for changes affecting only %s' |
| 1334 | ' should start with \"%s: \"' |
| 1335 | % (project_name, project_name)) |
| 1336 | |
| 1337 | |
Mike Frysinger | f9d41b3 | 2017-02-23 15:20:04 -0500 | [diff] [blame] | 1338 | def _check_exec_files(_project, commit): |
| 1339 | """Make +x bits on files.""" |
| 1340 | # List of files that should never be +x. |
| 1341 | NO_EXEC = ( |
| 1342 | 'ChangeLog*', |
| 1343 | 'COPYING', |
| 1344 | 'make.conf', |
| 1345 | 'make.defaults', |
| 1346 | 'Manifest', |
| 1347 | 'OWNERS', |
| 1348 | 'package.use', |
| 1349 | 'package.keywords', |
| 1350 | 'package.mask', |
| 1351 | 'parent', |
| 1352 | 'README', |
| 1353 | 'TODO', |
| 1354 | '.gitignore', |
| 1355 | '*.[achly]', |
| 1356 | '*.[ch]xx', |
| 1357 | '*.boto', |
| 1358 | '*.cc', |
| 1359 | '*.cfg', |
| 1360 | '*.conf', |
| 1361 | '*.config', |
| 1362 | '*.cpp', |
| 1363 | '*.css', |
| 1364 | '*.ebuild', |
| 1365 | '*.eclass', |
| 1366 | '*.gyp', |
| 1367 | '*.gypi', |
| 1368 | '*.htm', |
| 1369 | '*.html', |
| 1370 | '*.ini', |
| 1371 | '*.js', |
| 1372 | '*.json', |
| 1373 | '*.md', |
| 1374 | '*.mk', |
| 1375 | '*.patch', |
| 1376 | '*.policy', |
| 1377 | '*.proto', |
| 1378 | '*.raw', |
| 1379 | '*.rules', |
| 1380 | '*.service', |
| 1381 | '*.target', |
| 1382 | '*.txt', |
| 1383 | '*.xml', |
| 1384 | '*.yaml', |
| 1385 | ) |
| 1386 | |
| 1387 | def FinalName(obj): |
| 1388 | # If the file is being deleted, then the dst_file is not set. |
| 1389 | if obj.dst_file is None: |
| 1390 | return obj.src_file |
| 1391 | else: |
| 1392 | return obj.dst_file |
| 1393 | |
| 1394 | bad_files = [] |
| 1395 | files = _get_affected_files(commit, relative=True, full_details=True) |
| 1396 | for f in files: |
| 1397 | mode = int(f.dst_mode, 8) |
| 1398 | if not mode & 0o111: |
| 1399 | continue |
| 1400 | name = FinalName(f) |
| 1401 | for no_exec in NO_EXEC: |
| 1402 | if fnmatch.fnmatch(name, no_exec): |
| 1403 | bad_files.append(name) |
| 1404 | break |
| 1405 | |
| 1406 | if bad_files: |
| 1407 | return HookFailure('These files should not be executable. ' |
| 1408 | 'Please `chmod -x` them.', bad_files) |
| 1409 | |
| 1410 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1411 | # Base |
| 1412 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1413 | # A list of hooks which are not project specific and check patch description |
| 1414 | # (as opposed to patch body). |
| 1415 | _PATCH_DESCRIPTION_HOOKS = [ |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1416 | _check_change_has_bug_field, |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 1417 | _check_change_has_valid_cq_depend, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1418 | _check_change_has_test_field, |
| 1419 | _check_change_has_proper_changeid, |
Mike Frysinger | 36b2ebc | 2014-10-31 14:02:03 -0400 | [diff] [blame] | 1420 | _check_commit_message_style, |
Bernie Thompson | f8fea99 | 2016-01-14 10:27:18 -0800 | [diff] [blame] | 1421 | _check_change_is_contribution, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1422 | ] |
| 1423 | |
| 1424 | |
| 1425 | # A list of hooks that are not project-specific |
| 1426 | _COMMON_HOOKS = [ |
Aviv Keshet | 5ac5952 | 2017-01-31 14:28:27 -0800 | [diff] [blame] | 1427 | _check_cq_ini_well_formed, |
| 1428 | _check_cros_license, |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 1429 | _check_ebuild_eapi, |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 1430 | _check_ebuild_keywords, |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 1431 | _check_ebuild_licenses, |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 1432 | _check_ebuild_virtual_pv, |
Mike Frysinger | f9d41b3 | 2017-02-23 15:20:04 -0500 | [diff] [blame] | 1433 | _check_exec_files, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1434 | _check_for_uprev, |
Rahul Chaudhry | 09f6137 | 2015-07-31 17:14:26 -0700 | [diff] [blame] | 1435 | _check_gofmt, |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1436 | _check_layout_conf, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1437 | _check_no_long_lines, |
| 1438 | _check_no_stray_whitespace, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1439 | _check_no_tabs, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1440 | _check_portage_make_use_var, |
Aviv Keshet | 5ac5952 | 2017-01-31 14:28:27 -0800 | [diff] [blame] | 1441 | _check_tabbed_indents, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1442 | ] |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1443 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1444 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1445 | # A dictionary of project-specific hooks(callbacks), indexed by project name. |
| 1446 | # dict[project] = [callback1, callback2] |
| 1447 | _PROJECT_SPECIFIC_HOOKS = { |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1448 | "chromiumos/platform2": [_check_project_prefix], |
Mike Frysinger | af89129 | 2015-03-25 19:46:53 -0400 | [diff] [blame] | 1449 | "chromiumos/third_party/kernel": [_kernel_configcheck], |
| 1450 | "chromiumos/third_party/kernel-next": [_kernel_configcheck], |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1451 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1452 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1453 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1454 | # A dictionary of flags (keys) that can appear in the config file, and the hook |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1455 | # that the flag controls (value). |
| 1456 | _HOOK_FLAGS = { |
Mike Frysinger | a7642f5 | 2015-03-25 18:31:42 -0400 | [diff] [blame] | 1457 | 'checkpatch_check': _run_checkpatch, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1458 | 'stray_whitespace_check': _check_no_stray_whitespace, |
Mike Frysinger | ff6c7d6 | 2015-03-24 13:49:46 -0400 | [diff] [blame] | 1459 | 'json_check': _run_json_check, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1460 | 'long_line_check': _check_no_long_lines, |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 1461 | 'cros_license_check': _check_cros_license, |
| 1462 | 'aosp_license_check': _check_aosp_license, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1463 | 'tab_check': _check_no_tabs, |
Prathmesh Prabhu | c525465 | 2016-12-22 12:58:05 -0800 | [diff] [blame] | 1464 | 'tabbed_indent_required_check': _check_tabbed_indents, |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1465 | 'branch_check': _check_change_has_branch_field, |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1466 | 'signoff_check': _check_change_has_signoff_field, |
Josh Triplett | 0e8fc7f | 2014-04-23 16:00:00 -0700 | [diff] [blame] | 1467 | 'bug_field_check': _check_change_has_bug_field, |
| 1468 | 'test_field_check': _check_change_has_test_field, |
Steve Fung | 49ec7e9 | 2015-03-23 16:07:12 -0700 | [diff] [blame] | 1469 | 'manifest_check': _check_manifests, |
Bernie Thompson | f8fea99 | 2016-01-14 10:27:18 -0800 | [diff] [blame] | 1470 | 'contribution_check': _check_change_is_contribution, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1474 | def _get_override_hooks(config): |
| 1475 | """Returns a set of hooks controlled by the current project's config file. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1476 | |
| 1477 | Expects to be called within the project root. |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1478 | |
| 1479 | Args: |
| 1480 | config: A ConfigParser for the project's config file. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1481 | """ |
| 1482 | SECTION = 'Hook Overrides' |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1483 | SECTION_OPTIONS = 'Hook Overrides Options' |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1484 | if not config.has_section(SECTION): |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1485 | return set(), set() |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1486 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1487 | valid_keys = set(_HOOK_FLAGS.iterkeys()) |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1488 | hooks = _HOOK_FLAGS.copy() |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1489 | |
| 1490 | enable_flags = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1491 | disable_flags = [] |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1492 | for flag in config.options(SECTION): |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1493 | if flag not in valid_keys: |
| 1494 | raise ValueError('Error: unknown key "%s" in hook section of "%s"' % |
| 1495 | (flag, _CONFIG_FILE)) |
| 1496 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1497 | try: |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1498 | enabled = config.getboolean(SECTION, flag) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1499 | except ValueError as e: |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1500 | raise ValueError('Error: parsing flag "%s" in "%s" failed: %s' % |
| 1501 | (flag, _CONFIG_FILE, e)) |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1502 | if enabled: |
| 1503 | enable_flags.append(flag) |
| 1504 | else: |
| 1505 | disable_flags.append(flag) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1506 | |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1507 | # See if this hook has custom options. |
| 1508 | if enabled: |
| 1509 | try: |
| 1510 | options = config.get(SECTION_OPTIONS, flag) |
| 1511 | hooks[flag] = functools.partial(hooks[flag], options=options.split()) |
| 1512 | except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): |
| 1513 | pass |
| 1514 | |
| 1515 | enabled_hooks = set(hooks[x] for x in enable_flags) |
| 1516 | disabled_hooks = set(hooks[x] for x in disable_flags) |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1517 | return enabled_hooks, disabled_hooks |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1518 | |
| 1519 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1520 | def _get_project_hook_scripts(config): |
| 1521 | """Returns a list of project-specific hook scripts. |
| 1522 | |
| 1523 | Args: |
| 1524 | config: A ConfigParser for the project's config file. |
| 1525 | """ |
| 1526 | SECTION = 'Hook Scripts' |
| 1527 | if not config.has_section(SECTION): |
| 1528 | return [] |
| 1529 | |
| 1530 | hook_names_values = config.items(SECTION) |
| 1531 | hook_names_values.sort(key=lambda x: x[0]) |
| 1532 | return [x[1] for x in hook_names_values] |
| 1533 | |
| 1534 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1535 | def _get_project_hooks(project, presubmit): |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1536 | """Returns a list of hooks that need to be run for a project. |
| 1537 | |
| 1538 | Expects to be called from within the project root. |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1539 | |
| 1540 | Args: |
| 1541 | project: A string, name of the project. |
| 1542 | presubmit: A Boolean, True if the check is run as a git pre-submit script. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1543 | """ |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1544 | config = ConfigParser.RawConfigParser() |
| 1545 | try: |
| 1546 | config.read(_CONFIG_FILE) |
| 1547 | except ConfigParser.Error: |
| 1548 | # Just use an empty config file |
| 1549 | config = ConfigParser.RawConfigParser() |
| 1550 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1551 | if presubmit: |
Filipe Brandenburger | f70d32c | 2015-10-09 13:35:45 -0700 | [diff] [blame] | 1552 | hooks = _COMMON_HOOKS |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1553 | else: |
Filipe Brandenburger | f70d32c | 2015-10-09 13:35:45 -0700 | [diff] [blame] | 1554 | hooks = _PATCH_DESCRIPTION_HOOKS + _COMMON_HOOKS |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1555 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1556 | enabled_hooks, disabled_hooks = _get_override_hooks(config) |
Filipe Brandenburger | f70d32c | 2015-10-09 13:35:45 -0700 | [diff] [blame] | 1557 | hooks = [hook for hook in hooks if hook not in disabled_hooks] |
| 1558 | |
| 1559 | # If a list is both in _COMMON_HOOKS and also enabled explicitly through an |
| 1560 | # override, keep the override only. Note that the override may end up being |
| 1561 | # a functools.partial, in which case we need to extract the .func to compare |
| 1562 | # it to the common hooks. |
| 1563 | unwrapped_hooks = [getattr(hook, 'func', hook) for hook in enabled_hooks] |
| 1564 | hooks = [hook for hook in hooks if hook not in unwrapped_hooks] |
| 1565 | |
| 1566 | hooks = list(enabled_hooks) + hooks |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1567 | |
| 1568 | if project in _PROJECT_SPECIFIC_HOOKS: |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1569 | hooks.extend(hook for hook in _PROJECT_SPECIFIC_HOOKS[project] |
| 1570 | if hook not in disabled_hooks) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1571 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1572 | for script in _get_project_hook_scripts(config): |
| 1573 | hooks.append(functools.partial(_run_project_hook_script, script)) |
| 1574 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1575 | return hooks |
| 1576 | |
| 1577 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1578 | def _run_project_hooks(project_name, proj_dir=None, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1579 | commit_list=None, presubmit=False): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1580 | """For each project run its project specific hook from the hooks dictionary. |
| 1581 | |
| 1582 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1583 | project_name: The name of project to run hooks for. |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1584 | proj_dir: If non-None, this is the directory the project is in. If None, |
| 1585 | we'll ask repo. |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1586 | commit_list: A list of commits to run hooks against. If None or empty list |
| 1587 | then we'll automatically get the list of commits that would be uploaded. |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1588 | presubmit: A Boolean, True if the check is run as a git pre-submit script. |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1589 | |
| 1590 | Returns: |
| 1591 | Boolean value of whether any errors were ecountered while running the hooks. |
| 1592 | """ |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1593 | if proj_dir is None: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1594 | proj_dirs = _run_command( |
| 1595 | ['repo', 'forall', project_name, '-c', 'pwd']).split() |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1596 | if len(proj_dirs) == 0: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1597 | print('%s cannot be found.' % project_name, file=sys.stderr) |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1598 | print('Please specify a valid project.', file=sys.stderr) |
| 1599 | return True |
| 1600 | if len(proj_dirs) > 1: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1601 | print('%s is associated with multiple directories.' % project_name, |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1602 | file=sys.stderr) |
| 1603 | print('Please specify a directory to help disambiguate.', file=sys.stderr) |
| 1604 | return True |
| 1605 | proj_dir = proj_dirs[0] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1606 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1607 | pwd = os.getcwd() |
| 1608 | # hooks assume they are run from the root of the project |
| 1609 | os.chdir(proj_dir) |
| 1610 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1611 | remote_branch = _run_command(['git', 'rev-parse', '--abbrev-ref', |
| 1612 | '--symbolic-full-name', '@{u}']).strip() |
| 1613 | if not remote_branch: |
| 1614 | print('Your project %s doesn\'t track any remote repo.' % project_name, |
| 1615 | file=sys.stderr) |
| 1616 | remote = None |
| 1617 | else: |
| 1618 | remote, _branch = remote_branch.split('/', 1) |
| 1619 | |
| 1620 | project = Project(name=project_name, dir=proj_dir, remote=remote) |
| 1621 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1622 | if not commit_list: |
| 1623 | try: |
| 1624 | commit_list = _get_commits() |
| 1625 | except VerifyException as e: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1626 | PrintErrorForProject(project.name, HookFailure(str(e))) |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1627 | os.chdir(pwd) |
| 1628 | return True |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1629 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1630 | hooks = _get_project_hooks(project.name, presubmit) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1631 | error_found = False |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1632 | for commit in commit_list: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1633 | error_list = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1634 | for hook in hooks: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1635 | hook_error = hook(project, commit) |
| 1636 | if hook_error: |
| 1637 | error_list.append(hook_error) |
| 1638 | error_found = True |
| 1639 | if error_list: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1640 | PrintErrorsForCommit(project.name, commit, _get_commit_desc(commit), |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1641 | error_list) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1642 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1643 | os.chdir(pwd) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1644 | return error_found |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1645 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1646 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1647 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1648 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1649 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1650 | def main(project_list, worktree_list=None, **_kwargs): |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1651 | """Main function invoked directly by repo. |
| 1652 | |
| 1653 | This function will exit directly upon error so that repo doesn't print some |
| 1654 | obscure error message. |
| 1655 | |
| 1656 | Args: |
| 1657 | project_list: List of projects to run on. |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1658 | worktree_list: A list of directories. It should be the same length as |
| 1659 | project_list, so that each entry in project_list matches with a directory |
| 1660 | in worktree_list. If None, we will attempt to calculate the directories |
| 1661 | automatically. |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1662 | kwargs: Leave this here for forward-compatibility. |
| 1663 | """ |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1664 | found_error = False |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1665 | if not worktree_list: |
| 1666 | worktree_list = [None] * len(project_list) |
| 1667 | for project, worktree in zip(project_list, worktree_list): |
| 1668 | if _run_project_hooks(project, proj_dir=worktree): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1669 | found_error = True |
| 1670 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1671 | if found_error: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1672 | msg = ('Preupload failed due to errors in project(s). HINTS:\n' |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1673 | '- To disable some source style checks, and for other hints, see ' |
| 1674 | '<checkout_dir>/src/repohooks/README\n' |
| 1675 | '- To upload only current project, run \'repo upload .\'') |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 1676 | print(msg, file=sys.stderr) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1677 | sys.exit(1) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 1678 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1679 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1680 | def _identify_project(path): |
| 1681 | """Identify the repo project associated with the given path. |
| 1682 | |
| 1683 | Returns: |
| 1684 | A string indicating what project is associated with the path passed in or |
| 1685 | a blank string upon failure. |
| 1686 | """ |
| 1687 | return _run_command(['repo', 'forall', '.', '-c', 'echo ${REPO_PROJECT}'], |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1688 | redirect_stderr=True, cwd=path).strip() |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1689 | |
| 1690 | |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1691 | def direct_main(argv): |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1692 | """Run hooks directly (outside of the context of repo). |
| 1693 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1694 | Args: |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1695 | argv: The command line args to process |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1696 | |
| 1697 | Returns: |
| 1698 | 0 if no pre-upload failures, 1 if failures. |
| 1699 | |
| 1700 | Raises: |
| 1701 | BadInvocation: On some types of invocation errors. |
| 1702 | """ |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1703 | parser = commandline.ArgumentParser(description=__doc__) |
| 1704 | parser.add_argument('--dir', default=None, |
| 1705 | help='The directory that the project lives in. If not ' |
| 1706 | 'specified, use the git project root based on the cwd.') |
| 1707 | parser.add_argument('--project', default=None, |
| 1708 | help='The project repo path; this can affect how the ' |
| 1709 | 'hooks get run, since some hooks are project-specific. ' |
| 1710 | 'For chromite this is chromiumos/chromite. If not ' |
| 1711 | 'specified, the repo tool will be used to figure this ' |
| 1712 | 'out based on the dir.') |
| 1713 | parser.add_argument('--rerun-since', default=None, |
| 1714 | help='Rerun hooks on old commits since the given date. ' |
| 1715 | 'The date should match git log\'s concept of a date. ' |
| 1716 | 'e.g. 2012-06-20. This option is mutually exclusive ' |
| 1717 | 'with --pre-submit.') |
| 1718 | parser.add_argument('--pre-submit', action="store_true", |
| 1719 | help='Run the check against the pending commit. ' |
| 1720 | 'This option should be used at the \'git commit\' ' |
| 1721 | 'phase as opposed to \'repo upload\'. This option ' |
| 1722 | 'is mutually exclusive with --rerun-since.') |
| 1723 | parser.add_argument('commits', nargs='*', |
| 1724 | help='Check specific commits') |
| 1725 | opts = parser.parse_args(argv) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1726 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1727 | if opts.rerun_since: |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1728 | if opts.commits: |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1729 | raise BadInvocation('Can\'t pass commits and use rerun-since: %s' % |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1730 | ' '.join(opts.commits)) |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1731 | |
| 1732 | cmd = ['git', 'log', '--since="%s"' % opts.rerun_since, '--pretty=%H'] |
| 1733 | all_commits = _run_command(cmd).splitlines() |
| 1734 | bot_commits = _run_command(cmd + ['--author=chrome-bot']).splitlines() |
| 1735 | |
| 1736 | # Eliminate chrome-bot commits but keep ordering the same... |
| 1737 | bot_commits = set(bot_commits) |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1738 | opts.commits = [c for c in all_commits if c not in bot_commits] |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1739 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1740 | if opts.pre_submit: |
| 1741 | raise BadInvocation('rerun-since and pre-submit can not be ' |
| 1742 | 'used together') |
| 1743 | if opts.pre_submit: |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1744 | if opts.commits: |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1745 | raise BadInvocation('Can\'t pass commits and use pre-submit: %s' % |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1746 | ' '.join(opts.commits)) |
| 1747 | opts.commits = [PRE_SUBMIT,] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1748 | |
| 1749 | # Check/normlaize git dir; if unspecified, we'll use the root of the git |
| 1750 | # project from CWD |
| 1751 | if opts.dir is None: |
| 1752 | git_dir = _run_command(['git', 'rev-parse', '--git-dir'], |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1753 | redirect_stderr=True).strip() |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1754 | if not git_dir: |
| 1755 | raise BadInvocation('The current directory is not part of a git project.') |
| 1756 | opts.dir = os.path.dirname(os.path.abspath(git_dir)) |
| 1757 | elif not os.path.isdir(opts.dir): |
| 1758 | raise BadInvocation('Invalid dir: %s' % opts.dir) |
| 1759 | elif not os.path.isdir(os.path.join(opts.dir, '.git')): |
| 1760 | raise BadInvocation('Not a git directory: %s' % opts.dir) |
| 1761 | |
| 1762 | # Identify the project if it wasn't specified; this _requires_ the repo |
| 1763 | # tool to be installed and for the project to be part of a repo checkout. |
| 1764 | if not opts.project: |
| 1765 | opts.project = _identify_project(opts.dir) |
| 1766 | if not opts.project: |
| 1767 | raise BadInvocation("Repo couldn't identify the project of %s" % opts.dir) |
| 1768 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1769 | found_error = _run_project_hooks(opts.project, proj_dir=opts.dir, |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1770 | commit_list=opts.commits, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1771 | presubmit=opts.pre_submit) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1772 | if found_error: |
| 1773 | return 1 |
| 1774 | return 0 |
| 1775 | |
| 1776 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1777 | if __name__ == '__main__': |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1778 | sys.exit(direct_main(sys.argv[1:])) |