Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/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 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 13 | import ConfigParser |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 14 | import functools |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 15 | import json |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 16 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 17 | import re |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 18 | import sys |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 19 | import stat |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 20 | import subprocess |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 21 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 22 | from errors import (VerifyException, HookFailure, PrintErrorForProject, |
| 23 | PrintErrorsForCommit) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 24 | |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 25 | # If repo imports us, the __name__ will be __builtin__, and the wrapper will |
| 26 | # be in $CHROMEOS_CHECKOUT/.repo/repo/main.py, so we need to go two directories |
| 27 | # up. The same logic also happens to work if we're executed directly. |
| 28 | if __name__ in ('__builtin__', '__main__'): |
| 29 | sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', '..')) |
| 30 | |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 31 | from chromite.lib import commandline |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 32 | from chromite.lib import git |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 33 | from chromite.lib import osutils |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 34 | from chromite.lib import patch |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 35 | from chromite.licensing import licenses_lib |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 36 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 37 | PRE_SUBMIT = 'pre-submit' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 38 | |
| 39 | COMMON_INCLUDED_PATHS = [ |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 40 | # C++ and friends |
| 41 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 42 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 43 | # Scripts |
| 44 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 45 | # No extension at all, note that ALL CAPS files are black listed in |
| 46 | # COMMON_EXCLUDED_LIST below. |
| 47 | r"(^|.*[\\\/])[^.]+$", |
| 48 | # Other |
| 49 | r".*\.java$", r".*\.mk$", r".*\.am$", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 50 | ] |
| 51 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 52 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 53 | COMMON_EXCLUDED_PATHS = [ |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 54 | # avoid doing source file checks for kernel |
| 55 | r"/src/third_party/kernel/", |
| 56 | r"/src/third_party/kernel-next/", |
| 57 | r"/src/third_party/ktop/", |
| 58 | r"/src/third_party/punybench/", |
| 59 | r".*\bexperimental[\\\/].*", |
| 60 | r".*\b[A-Z0-9_]{2,}$", |
| 61 | r".*[\\\/]debian[\\\/]rules$", |
| 62 | # for ebuild trees, ignore any caches and manifest data |
| 63 | r".*/Manifest$", |
| 64 | r".*/metadata/[^/]*cache[^/]*/[^/]+/[^/]+$", |
Doug Anderson | 5bfb679 | 2011-10-25 16:45:41 -0700 | [diff] [blame] | 65 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 66 | # ignore profiles data (like overlay-tegra2/profiles) |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 67 | r"(^|.*/)overlay-.*/profiles/.*", |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 68 | r"^profiles/.*$", |
| 69 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 70 | # ignore minified js and jquery |
| 71 | r".*\.min\.js", |
| 72 | r".*jquery.*\.js", |
Mike Frysinger | 33a458d | 2014-03-03 17:00:51 -0500 | [diff] [blame] | 73 | |
| 74 | # Ignore license files as the content is often taken verbatim. |
| 75 | r'.*/licenses/.*', |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 76 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 77 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 78 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 79 | _CONFIG_FILE = 'PRESUBMIT.cfg' |
| 80 | |
| 81 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 82 | # Exceptions |
| 83 | |
| 84 | |
| 85 | class BadInvocation(Exception): |
| 86 | """An Exception indicating a bad invocation of the program.""" |
| 87 | pass |
| 88 | |
| 89 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 90 | # General Helpers |
| 91 | |
Sean Paul | ba01d40 | 2011-05-05 11:36:23 -0400 | [diff] [blame] | 92 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 93 | def _run_command(cmd, cwd=None, stderr=None): |
| 94 | """Executes the passed in command and returns raw stdout output. |
| 95 | |
| 96 | Args: |
| 97 | cmd: The command to run; should be a list of strings. |
| 98 | cwd: The directory to switch to for running the command. |
| 99 | stderr: Can be one of None (print stderr to console), subprocess.STDOUT |
| 100 | (combine stderr with stdout), or subprocess.PIPE (ignore stderr). |
| 101 | |
| 102 | Returns: |
| 103 | The standard out from the process. |
| 104 | """ |
| 105 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=stderr, cwd=cwd) |
| 106 | return p.communicate()[0] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 107 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 108 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 109 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 110 | """Returns the absolute path to the repohooks directory.""" |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 111 | if __name__ == '__main__': |
| 112 | # Works when file is run on its own (__file__ is defined)... |
| 113 | return os.path.abspath(os.path.dirname(__file__)) |
| 114 | else: |
| 115 | # We need to do this when we're run through repo. Since repo executes |
| 116 | # us with execfile(), we don't get __file__ defined. |
| 117 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
| 118 | return _run_command(cmd).strip() |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 119 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 120 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 121 | def _match_regex_list(subject, expressions): |
| 122 | """Try to match a list of regular expressions to a string. |
| 123 | |
| 124 | Args: |
| 125 | subject: The string to match regexes on |
| 126 | expressions: A list of regular expressions to check for matches with. |
| 127 | |
| 128 | Returns: |
| 129 | Whether the passed in subject matches any of the passed in regexes. |
| 130 | """ |
| 131 | for expr in expressions: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 132 | if re.search(expr, subject): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 133 | return True |
| 134 | return False |
| 135 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 136 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 137 | def _filter_files(files, include_list, exclude_list=()): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 138 | """Filter out files based on the conditions passed in. |
| 139 | |
| 140 | Args: |
| 141 | files: list of filepaths to filter |
| 142 | include_list: list of regex that when matched with a file path will cause it |
| 143 | to be added to the output list unless the file is also matched with a |
| 144 | regex in the exclude_list. |
| 145 | exclude_list: list of regex that when matched with a file will prevent it |
| 146 | from being added to the output list, even if it is also matched with a |
| 147 | regex in the include_list. |
| 148 | |
| 149 | Returns: |
| 150 | A list of filepaths that contain files matched in the include_list and not |
| 151 | in the exclude_list. |
| 152 | """ |
| 153 | filtered = [] |
| 154 | for f in files: |
| 155 | if (_match_regex_list(f, include_list) and |
| 156 | not _match_regex_list(f, exclude_list)): |
| 157 | filtered.append(f) |
| 158 | return filtered |
| 159 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 160 | |
| 161 | # Git Helpers |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 162 | |
| 163 | |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 164 | def _get_upstream_branch(): |
| 165 | """Returns the upstream tracking branch of the current branch. |
| 166 | |
| 167 | Raises: |
| 168 | Error if there is no tracking branch |
| 169 | """ |
| 170 | current_branch = _run_command(['git', 'symbolic-ref', 'HEAD']).strip() |
| 171 | current_branch = current_branch.replace('refs/heads/', '') |
| 172 | if not current_branch: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 173 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 174 | |
| 175 | cfg_option = 'branch.' + current_branch + '.%s' |
| 176 | full_upstream = _run_command(['git', 'config', cfg_option % 'merge']).strip() |
| 177 | remote = _run_command(['git', 'config', cfg_option % 'remote']).strip() |
| 178 | if not remote or not full_upstream: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 179 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 180 | |
| 181 | return full_upstream.replace('heads', 'remotes/' + remote) |
| 182 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 183 | |
Che-Liang Chiou | 5ce2d7b | 2013-03-22 18:47:55 -0700 | [diff] [blame] | 184 | def _get_patch(commit): |
| 185 | """Returns the patch for this commit.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 186 | if commit == PRE_SUBMIT: |
| 187 | return _run_command(['git', 'diff', '--cached', 'HEAD']) |
| 188 | else: |
| 189 | return _run_command(['git', 'format-patch', '--stdout', '-1', commit]) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 190 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 191 | |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 192 | def _try_utf8_decode(data): |
| 193 | """Attempts to decode a string as UTF-8. |
| 194 | |
| 195 | Returns: |
| 196 | The decoded Unicode object, or the original string if parsing fails. |
| 197 | """ |
| 198 | try: |
| 199 | return unicode(data, 'utf-8', 'strict') |
| 200 | except UnicodeDecodeError: |
| 201 | return data |
| 202 | |
| 203 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 204 | def _get_file_content(path, commit): |
| 205 | """Returns the content of a file at a specific commit. |
| 206 | |
| 207 | We can't rely on the file as it exists in the filesystem as people might be |
| 208 | uploading a series of changes which modifies the file multiple times. |
| 209 | |
| 210 | Note: The "content" of a symlink is just the target. So if you're expecting |
| 211 | a full file, you should check that first. One way to detect is that the |
| 212 | content will not have any newlines. |
| 213 | """ |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 214 | if commit == PRE_SUBMIT: |
| 215 | return _run_command(['git', 'diff', 'HEAD', path]) |
| 216 | else: |
| 217 | return _run_command(['git', 'show', '%s:%s' % (commit, path)]) |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 218 | |
| 219 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 220 | def _get_file_diff(path, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 221 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 222 | command = ['git', 'diff', '-p', '--pretty=format:', '--no-ext-diff'] |
| 223 | if commit == PRE_SUBMIT: |
| 224 | command += ['HEAD', path] |
| 225 | else: |
| 226 | command += [commit, path] |
| 227 | output = _run_command(command) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 228 | |
| 229 | new_lines = [] |
| 230 | line_num = 0 |
| 231 | for line in output.splitlines(): |
| 232 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 233 | if m: |
| 234 | line_num = int(m.groups(1)[0]) |
| 235 | continue |
| 236 | if line.startswith('+') and not line.startswith('++'): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 237 | new_lines.append((line_num, _try_utf8_decode(line[1:]))) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 238 | if not line.startswith('-'): |
| 239 | line_num += 1 |
| 240 | return new_lines |
| 241 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 242 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 243 | def _get_affected_files(commit, include_deletes=False, relative=False, |
| 244 | include_symlinks=False, include_adds=True, |
| 245 | full_details=False): |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 246 | """Returns list of file paths that were modified/added, excluding symlinks. |
| 247 | |
| 248 | Args: |
| 249 | commit: The commit |
| 250 | include_deletes: If true, we'll include deleted files in the result |
| 251 | relative: Whether to return relative or full paths to files |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 252 | include_symlinks: If true, we'll include symlinks in the result |
| 253 | include_adds: If true, we'll include new files in the result |
| 254 | full_details: If False, return filenames, else return structured results. |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 255 | |
| 256 | Returns: |
| 257 | A list of modified/added (and perhaps deleted) files |
| 258 | """ |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 259 | if not relative and full_details: |
| 260 | raise ValueError('full_details only supports relative paths currently') |
| 261 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 262 | if commit == PRE_SUBMIT: |
| 263 | return _run_command(['git', 'diff-index', '--cached', |
| 264 | '--name-only', 'HEAD']).split() |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 265 | |
| 266 | path = os.getcwd() |
| 267 | files = git.RawDiff(path, '%s^!' % commit) |
| 268 | |
| 269 | # Filter out symlinks. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 270 | if not include_symlinks: |
| 271 | 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] | 272 | |
| 273 | if not include_deletes: |
| 274 | files = [x for x in files if x.status != 'D'] |
| 275 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 276 | if not include_adds: |
| 277 | files = [x for x in files if x.status != 'A'] |
| 278 | |
| 279 | if full_details: |
| 280 | # Caller wants the raw objects to parse status/etc... themselves. |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 281 | return files |
| 282 | else: |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 283 | # Caller only cares about filenames. |
| 284 | files = [x.dst_file if x.dst_file else x.src_file for x in files] |
| 285 | if relative: |
| 286 | return files |
| 287 | else: |
| 288 | return [os.path.join(path, x) for x in files] |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 289 | |
| 290 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 291 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 292 | """Returns a list of commits for this review.""" |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 293 | cmd = ['git', 'log', '%s..' % _get_upstream_branch(), '--format=%H'] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 294 | return _run_command(cmd).split() |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 295 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 296 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 297 | def _get_commit_desc(commit): |
| 298 | """Returns the full commit message of a commit.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 299 | if commit == PRE_SUBMIT: |
| 300 | return '' |
Sean Paul | 23a2c58 | 2011-05-06 13:10:44 -0400 | [diff] [blame] | 301 | return _run_command(['git', 'log', '--format=%s%n%n%b', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 302 | |
| 303 | |
| 304 | # Common Hooks |
| 305 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 306 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 307 | def _check_no_long_lines(_project, commit): |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 308 | """Checks there are no lines longer than MAX_LEN in any of the text files.""" |
| 309 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 310 | MAX_LEN = 80 |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 311 | SKIP_REGEXP = re.compile('|'.join([ |
| 312 | r'https?://', |
| 313 | r'^#\s*(define|include|import|pragma|if|endif)\b'])) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 314 | |
| 315 | errors = [] |
| 316 | files = _filter_files(_get_affected_files(commit), |
| 317 | COMMON_INCLUDED_PATHS, |
| 318 | COMMON_EXCLUDED_PATHS) |
| 319 | |
| 320 | for afile in files: |
| 321 | for line_num, line in _get_file_diff(afile, commit): |
| 322 | # Allow certain lines to exceed the maxlen rule. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 323 | if len(line) <= MAX_LEN or SKIP_REGEXP.search(line): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 324 | continue |
| 325 | |
| 326 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 327 | if len(errors) == 5: # Just show the first 5 errors. |
| 328 | break |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 329 | |
| 330 | if errors: |
| 331 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 332 | return HookFailure(msg, errors) |
| 333 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 334 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 335 | def _check_no_stray_whitespace(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 336 | """Checks that there is no stray whitespace at source lines end.""" |
| 337 | errors = [] |
| 338 | files = _filter_files(_get_affected_files(commit), |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 339 | COMMON_INCLUDED_PATHS, |
| 340 | COMMON_EXCLUDED_PATHS) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 341 | for afile in files: |
| 342 | for line_num, line in _get_file_diff(afile, commit): |
| 343 | if line.rstrip() != line: |
| 344 | errors.append('%s, line %s' % (afile, line_num)) |
| 345 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 346 | return HookFailure('Found line ending with white space in:', errors) |
| 347 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 348 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 349 | def _check_no_tabs(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 350 | """Checks there are no unexpanded tabs.""" |
| 351 | TAB_OK_PATHS = [ |
Ryan Cui | 31e0c17 | 2011-05-04 21:00:45 -0700 | [diff] [blame] | 352 | r"/src/third_party/u-boot/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 353 | r".*\.ebuild$", |
| 354 | r".*\.eclass$", |
Elly Jones | 5ab3419 | 2011-11-15 14:57:06 -0500 | [diff] [blame] | 355 | r".*/[M|m]akefile$", |
| 356 | r".*\.mk$" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 357 | ] |
| 358 | |
| 359 | errors = [] |
| 360 | files = _filter_files(_get_affected_files(commit), |
| 361 | COMMON_INCLUDED_PATHS, |
| 362 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
| 363 | |
| 364 | for afile in files: |
| 365 | for line_num, line in _get_file_diff(afile, commit): |
| 366 | if '\t' in line: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 367 | errors.append('%s, line %s' % (afile, line_num)) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 368 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 369 | return HookFailure('Found a tab character in:', errors) |
| 370 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 371 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 372 | def _check_change_has_test_field(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 373 | """Check for a non-empty 'TEST=' field in the commit message.""" |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 374 | TEST_RE = r'\nTEST=\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 375 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 376 | if not re.search(TEST_RE, _get_commit_desc(commit)): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 377 | msg = 'Changelist description needs TEST field (after first line)' |
| 378 | return HookFailure(msg) |
| 379 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 380 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 381 | def _check_change_has_valid_cq_depend(_project, commit): |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 382 | """Check for a correctly formatted CQ-DEPEND field in the commit message.""" |
| 383 | msg = 'Changelist has invalid CQ-DEPEND target.' |
| 384 | example = 'Example: CQ-DEPEND=CL:1234, CL:2345' |
| 385 | try: |
| 386 | patch.GetPaladinDeps(_get_commit_desc(commit)) |
| 387 | except ValueError as ex: |
| 388 | return HookFailure(msg, [example, str(ex)]) |
| 389 | |
| 390 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 391 | def _check_change_has_bug_field(_project, commit): |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 392 | """Check for a correctly formatted 'BUG=' field in the commit message.""" |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 393 | OLD_BUG_RE = r'\nBUG=.*chromium-os' |
| 394 | if re.search(OLD_BUG_RE, _get_commit_desc(commit)): |
| 395 | msg = ('The chromium-os bug tracker is now deprecated. Please use\n' |
| 396 | 'the chromium tracker in your BUG= line now.') |
| 397 | return HookFailure(msg) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 398 | |
Stefan Sauer | d74ad56 | 2015-03-10 10:14:28 +0100 | [diff] [blame] | 399 | BUG_RE = r'\nBUG=([Nn]one|(chrome-os-partner|chromium|brillo|b):\d+)' |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 400 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 401 | msg = ('Changelist description needs BUG field (after first line):\n' |
Christopher Wiley | c92721b | 2015-01-26 13:07:39 -0800 | [diff] [blame] | 402 | 'BUG=brillo:9999 (for Brillo tracker)\n' |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 403 | 'BUG=chromium:9999 (for public tracker)\n' |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 404 | 'BUG=chrome-os-partner:9999 (for partner tracker)\n' |
Stefan Sauer | d74ad56 | 2015-03-10 10:14:28 +0100 | [diff] [blame] | 405 | 'BUG=b:9999 (for buganizer)\n' |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 406 | 'BUG=None') |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 407 | return HookFailure(msg) |
| 408 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 409 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 410 | def _check_for_uprev(project, commit, project_top=None): |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 411 | """Check that we're not missing a revbump of an ebuild in the given commit. |
| 412 | |
| 413 | If the given commit touches files in a directory that has ebuilds somewhere |
| 414 | up the directory hierarchy, it's very likely that we need an ebuild revbump |
| 415 | in order for those changes to take effect. |
| 416 | |
| 417 | It's not totally trivial to detect a revbump, so at least detect that an |
| 418 | ebuild with a revision number in it was touched. This should handle the |
| 419 | common case where we use a symlink to do the revbump. |
| 420 | |
| 421 | TODO: it would be nice to enhance this hook to: |
| 422 | * Handle cases where people revbump with a slightly different syntax. I see |
| 423 | one ebuild (puppy) that revbumps with _pN. This is a false positive. |
| 424 | * Catches cases where people aren't using symlinks for revbumps. If they |
| 425 | edit a revisioned file directly (and are expected to rename it for revbump) |
| 426 | we'll miss that. Perhaps we could detect that the file touched is a |
| 427 | symlink? |
| 428 | |
| 429 | If a project doesn't use symlinks we'll potentially miss a revbump, but we're |
| 430 | still better off than without this check. |
| 431 | |
| 432 | Args: |
| 433 | project: The project to look at |
| 434 | commit: The commit to look at |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 435 | project_top: Top dir to process commits in |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 436 | |
| 437 | Returns: |
| 438 | A HookFailure or None. |
| 439 | """ |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 440 | # If this is the portage-stable overlay, then ignore the check. It's rare |
| 441 | # that we're doing anything other than importing files from upstream, so |
| 442 | # forcing a rev bump makes no sense. |
| 443 | whitelist = ( |
| 444 | 'chromiumos/overlays/portage-stable', |
| 445 | ) |
| 446 | if project in whitelist: |
| 447 | return None |
| 448 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 449 | def FinalName(obj): |
| 450 | # If the file is being deleted, then the dst_file is not set. |
| 451 | if obj.dst_file is None: |
| 452 | return obj.src_file |
| 453 | else: |
| 454 | return obj.dst_file |
| 455 | |
| 456 | affected_path_objs = _get_affected_files( |
| 457 | commit, include_deletes=True, include_symlinks=True, relative=True, |
| 458 | full_details=True) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 459 | |
| 460 | # Don't yell about changes to whitelisted files... |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 461 | whitelist = ('ChangeLog', 'Manifest', 'metadata.xml') |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 462 | affected_path_objs = [x for x in affected_path_objs |
| 463 | if os.path.basename(FinalName(x)) not in whitelist] |
| 464 | if not affected_path_objs: |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 465 | return None |
| 466 | |
| 467 | # If we've touched any file named with a -rN.ebuild then we'll say we're |
| 468 | # OK right away. See TODO above about enhancing this. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 469 | touched_revved_ebuild = any(re.search(r'-r\d*\.ebuild$', FinalName(x)) |
| 470 | for x in affected_path_objs) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 471 | if touched_revved_ebuild: |
| 472 | return None |
| 473 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 474 | # If we're creating new ebuilds from scratch, then we don't need an uprev. |
| 475 | # Find all the dirs that new ebuilds and ignore their files/. |
| 476 | ebuild_dirs = [os.path.dirname(FinalName(x)) + '/' for x in affected_path_objs |
| 477 | if FinalName(x).endswith('.ebuild') and x.status == 'A'] |
| 478 | affected_path_objs = [obj for obj in affected_path_objs |
| 479 | if not any(FinalName(obj).startswith(x) |
| 480 | for x in ebuild_dirs)] |
| 481 | if not affected_path_objs: |
| 482 | return |
| 483 | |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 484 | # We want to examine the current contents of all directories that are parents |
| 485 | # of files that were touched (up to the top of the project). |
| 486 | # |
| 487 | # ...note: we use the current directory contents even though it may have |
| 488 | # changed since the commit we're looking at. This is just a heuristic after |
| 489 | # all. Worst case we don't flag a missing revbump. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 490 | if project_top is None: |
| 491 | project_top = os.getcwd() |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 492 | dirs_to_check = set([project_top]) |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 493 | for obj in affected_path_objs: |
| 494 | path = os.path.join(project_top, os.path.dirname(FinalName(obj))) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 495 | while os.path.exists(path) and not os.path.samefile(path, project_top): |
| 496 | dirs_to_check.add(path) |
| 497 | path = os.path.dirname(path) |
| 498 | |
| 499 | # Look through each directory. If it's got an ebuild in it then we'll |
| 500 | # consider this as a case when we need a revbump. |
Gwendal Grignou | a3086c3 | 2014-12-09 11:17:22 -0800 | [diff] [blame] | 501 | affected_paths = set(os.path.join(project_top, FinalName(x)) |
| 502 | for x in affected_path_objs) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 503 | for dir_path in dirs_to_check: |
| 504 | contents = os.listdir(dir_path) |
| 505 | ebuilds = [os.path.join(dir_path, path) |
| 506 | for path in contents if path.endswith('.ebuild')] |
| 507 | ebuilds_9999 = [path for path in ebuilds if path.endswith('-9999.ebuild')] |
| 508 | |
| 509 | # If the -9999.ebuild file was touched the bot will uprev for us. |
| 510 | # ...we'll use a simple intersection here as a heuristic... |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 511 | if set(ebuilds_9999) & affected_paths: |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 512 | continue |
| 513 | |
| 514 | if ebuilds: |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 515 | return HookFailure('Changelist probably needs a revbump of an ebuild, ' |
| 516 | 'or a -r1.ebuild symlink if this is a new ebuild:\n' |
| 517 | '%s' % dir_path) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 518 | |
| 519 | return None |
| 520 | |
| 521 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 522 | def _check_ebuild_eapi(project, commit): |
| 523 | """Make sure we have people use EAPI=4 or newer with custom ebuilds. |
| 524 | |
| 525 | We want to get away from older EAPI's as it makes life confusing and they |
| 526 | have less builtin error checking. |
| 527 | |
| 528 | Args: |
| 529 | project: The project to look at |
| 530 | commit: The commit to look at |
| 531 | |
| 532 | Returns: |
| 533 | A HookFailure or None. |
| 534 | """ |
| 535 | # 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] | 536 | # that we're doing anything other than importing files from upstream, and |
| 537 | # we shouldn't be rewriting things fundamentally anyways. |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 538 | whitelist = ( |
| 539 | 'chromiumos/overlays/portage-stable', |
| 540 | ) |
| 541 | if project in whitelist: |
| 542 | return None |
| 543 | |
| 544 | BAD_EAPIS = ('0', '1', '2', '3') |
| 545 | |
| 546 | get_eapi = re.compile(r'^\s*EAPI=[\'"]?([^\'"]+)') |
| 547 | |
| 548 | ebuilds_re = [r'\.ebuild$'] |
| 549 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 550 | ebuilds_re) |
| 551 | bad_ebuilds = [] |
| 552 | |
| 553 | for ebuild in ebuilds: |
| 554 | # If the ebuild does not specify an EAPI, it defaults to 0. |
| 555 | eapi = '0' |
| 556 | |
| 557 | lines = _get_file_content(ebuild, commit).splitlines() |
| 558 | if len(lines) == 1: |
| 559 | # This is most likely a symlink, so skip it entirely. |
| 560 | continue |
| 561 | |
| 562 | for line in lines: |
| 563 | m = get_eapi.match(line) |
| 564 | if m: |
| 565 | # Once we hit the first EAPI line in this ebuild, stop processing. |
| 566 | # The spec requires that there only be one and it be first, so |
| 567 | # checking all possible values is pointless. We also assume that |
| 568 | # it's "the" EAPI line and not something in the middle of a heredoc. |
| 569 | eapi = m.group(1) |
| 570 | break |
| 571 | |
| 572 | if eapi in BAD_EAPIS: |
| 573 | bad_ebuilds.append((ebuild, eapi)) |
| 574 | |
| 575 | if bad_ebuilds: |
| 576 | # pylint: disable=C0301 |
| 577 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/upgrade-ebuild-eapis' |
| 578 | # pylint: enable=C0301 |
| 579 | return HookFailure( |
Mike Frysinger | cd6adfc | 2014-02-06 01:03:56 -0500 | [diff] [blame] | 580 | 'These ebuilds are using old EAPIs. If these are imported from\n' |
| 581 | 'Gentoo, then you may ignore and upload once with the --no-verify\n' |
| 582 | 'flag. Otherwise, please update to 4 or newer.\n' |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 583 | '\t%s\n' |
| 584 | 'See this guide for more details:\n%s\n' % |
| 585 | ('\n\t'.join(['%s: EAPI=%s' % x for x in bad_ebuilds]), url)) |
| 586 | |
| 587 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 588 | def _check_ebuild_keywords(_project, commit): |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 589 | """Make sure we use the new style KEYWORDS when possible in ebuilds. |
| 590 | |
| 591 | If an ebuild generally does not care about the arch it is running on, then |
| 592 | ebuilds should flag it with one of: |
| 593 | KEYWORDS="*" # A stable ebuild. |
| 594 | KEYWORDS="~*" # An unstable ebuild. |
| 595 | KEYWORDS="-* ..." # Is known to only work on specific arches. |
| 596 | |
| 597 | Args: |
| 598 | project: The project to look at |
| 599 | commit: The commit to look at |
| 600 | |
| 601 | Returns: |
| 602 | A HookFailure or None. |
| 603 | """ |
| 604 | WHITELIST = set(('*', '-*', '~*')) |
| 605 | |
| 606 | get_keywords = re.compile(r'^\s*KEYWORDS="(.*)"') |
| 607 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 608 | ebuilds_re = [r'\.ebuild$'] |
| 609 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 610 | ebuilds_re) |
| 611 | |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 612 | bad_ebuilds = [] |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 613 | for ebuild in ebuilds: |
Mike Frysinger | 5c9e58d | 2014-09-09 03:32:50 -0400 | [diff] [blame] | 614 | # We get the full content rather than a diff as the latter does not work |
| 615 | # on new files (like when adding new ebuilds). |
| 616 | lines = _get_file_content(ebuild, commit).splitlines() |
| 617 | for line in lines: |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 618 | m = get_keywords.match(line) |
| 619 | if m: |
| 620 | keywords = set(m.group(1).split()) |
| 621 | if not keywords or WHITELIST - keywords != WHITELIST: |
| 622 | continue |
| 623 | |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 624 | bad_ebuilds.append(ebuild) |
| 625 | |
| 626 | if bad_ebuilds: |
| 627 | return HookFailure( |
| 628 | '%s\n' |
| 629 | 'Please update KEYWORDS to use a glob:\n' |
| 630 | 'If the ebuild should be marked stable (normal for non-9999 ebuilds):\n' |
| 631 | ' KEYWORDS="*"\n' |
| 632 | 'If the ebuild should be marked unstable (normal for ' |
| 633 | 'cros-workon / 9999 ebuilds):\n' |
| 634 | ' KEYWORDS="~*"\n' |
| 635 | 'If the ebuild needs to be marked for only specific arches,' |
| 636 | 'then use -* like so:\n' |
| 637 | ' KEYWORDS="-* arm ..."\n' % '\n* '.join(bad_ebuilds)) |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 638 | |
| 639 | |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 640 | def _check_ebuild_licenses(_project, commit): |
| 641 | """Check if the LICENSE field in the ebuild is correct.""" |
| 642 | affected_paths = _get_affected_files(commit) |
| 643 | touched_ebuilds = [x for x in affected_paths if x.endswith('.ebuild')] |
| 644 | |
| 645 | # A list of licenses to ignore for now. |
Yu-Ju Hong | c0963fa | 2014-03-03 12:36:52 -0800 | [diff] [blame] | 646 | LICENSES_IGNORE = ['||', '(', ')'] |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 647 | |
| 648 | for ebuild in touched_ebuilds: |
| 649 | # Skip virutal packages. |
| 650 | if ebuild.split('/')[-3] == 'virtual': |
| 651 | continue |
| 652 | |
| 653 | try: |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 654 | license_types = licenses_lib.GetLicenseTypesFromEbuild(ebuild) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 655 | except ValueError as e: |
| 656 | return HookFailure(e.message, [ebuild]) |
| 657 | |
| 658 | # Also ignore licenses ending with '?' |
| 659 | for license_type in [x for x in license_types |
| 660 | if x not in LICENSES_IGNORE and not x.endswith('?')]: |
| 661 | try: |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 662 | licenses_lib.Licensing.FindLicenseType(license_type) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 663 | except AssertionError as e: |
| 664 | return HookFailure(e.message, [ebuild]) |
| 665 | |
| 666 | |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 667 | def _check_ebuild_virtual_pv(project, commit): |
| 668 | """Enforce the virtual PV policies.""" |
| 669 | # If this is the portage-stable overlay, then ignore the check. |
| 670 | # We want to import virtuals as-is from upstream Gentoo. |
| 671 | whitelist = ( |
| 672 | 'chromiumos/overlays/portage-stable', |
| 673 | ) |
| 674 | if project in whitelist: |
| 675 | return None |
| 676 | |
| 677 | # We assume the repo name is the same as the dir name on disk. |
| 678 | # It would be dumb to not have them match though. |
| 679 | project = os.path.basename(project) |
| 680 | |
| 681 | is_variant = lambda x: x.startswith('overlay-variant-') |
| 682 | is_board = lambda x: x.startswith('overlay-') |
| 683 | is_private = lambda x: x.endswith('-private') |
| 684 | |
| 685 | get_pv = re.compile(r'(.*?)virtual/([^/]+)/\2-([^/]*)\.ebuild$') |
| 686 | |
| 687 | ebuilds_re = [r'\.ebuild$'] |
| 688 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 689 | ebuilds_re) |
| 690 | bad_ebuilds = [] |
| 691 | |
| 692 | for ebuild in ebuilds: |
| 693 | m = get_pv.match(ebuild) |
| 694 | if m: |
| 695 | overlay = m.group(1) |
| 696 | if not overlay or not is_board(overlay): |
| 697 | overlay = project |
| 698 | |
| 699 | pv = m.group(3).split('-', 1)[0] |
| 700 | |
| 701 | if is_private(overlay): |
| 702 | want_pv = '3.5' if is_variant(overlay) else '3' |
| 703 | elif is_board(overlay): |
| 704 | want_pv = '2.5' if is_variant(overlay) else '2' |
| 705 | else: |
| 706 | want_pv = '1' |
| 707 | |
| 708 | if pv != want_pv: |
| 709 | bad_ebuilds.append((ebuild, pv, want_pv)) |
| 710 | |
| 711 | if bad_ebuilds: |
| 712 | # pylint: disable=C0301 |
| 713 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/portage-build-faq#TOC-Virtuals-and-central-management' |
| 714 | # pylint: enable=C0301 |
| 715 | return HookFailure( |
| 716 | 'These virtuals have incorrect package versions (PVs). Please adjust:\n' |
| 717 | '\t%s\n' |
| 718 | 'If this is an upstream Gentoo virtual, then you may ignore this\n' |
| 719 | 'check (and re-run w/--no-verify). Otherwise, please see this\n' |
| 720 | 'page for more details:\n%s\n' % |
| 721 | ('\n\t'.join(['%s:\n\t\tPV is %s but should be %s' % x |
| 722 | for x in bad_ebuilds]), url)) |
| 723 | |
| 724 | |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 725 | def _check_portage_make_use_var(_project, commit): |
| 726 | """Verify that $USE is set correctly in make.conf and make.defaults.""" |
| 727 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 728 | [r'(^|/)make.(conf|defaults)$']) |
| 729 | |
| 730 | errors = [] |
| 731 | for path in files: |
| 732 | basename = os.path.basename(path) |
| 733 | |
| 734 | # Has a USE= line already been encountered in this file? |
| 735 | saw_use = False |
| 736 | |
| 737 | for i, line in enumerate(_get_file_content(path, commit).splitlines(), 1): |
| 738 | if not line.startswith('USE='): |
| 739 | continue |
| 740 | |
| 741 | preserves_use = '${USE}' in line or '$USE' in line |
| 742 | |
| 743 | if (basename == 'make.conf' or |
| 744 | (basename == 'make.defaults' and saw_use)) and not preserves_use: |
| 745 | errors.append('%s:%d: missing ${USE}' % (path, i)) |
| 746 | elif basename == 'make.defaults' and not saw_use and preserves_use: |
| 747 | errors.append('%s:%d: ${USE} referenced in initial declaration' % |
| 748 | (path, i)) |
| 749 | |
| 750 | saw_use = True |
| 751 | |
| 752 | if errors: |
| 753 | return HookFailure( |
| 754 | 'One or more Portage make files appear to set USE incorrectly.\n' |
| 755 | '\n' |
| 756 | 'All USE assignments in make.conf and all assignments after the\n' |
| 757 | 'initial declaration in make.defaults should contain "${USE}" to\n' |
| 758 | 'preserve previously-set flags.\n' |
| 759 | '\n' |
| 760 | 'The initial USE declaration in make.defaults should not contain\n' |
| 761 | '"${USE}".\n', |
| 762 | errors) |
| 763 | |
| 764 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 765 | def _check_change_has_proper_changeid(_project, commit): |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 766 | """Verify that Change-ID is present in last paragraph of commit message.""" |
Mike Frysinger | 4a22bf0 | 2014-10-31 13:53:35 -0400 | [diff] [blame] | 767 | CHANGE_ID_RE = r'\nChange-Id: I[a-f0-9]+\n' |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 768 | desc = _get_commit_desc(commit) |
Mike Frysinger | 4a22bf0 | 2014-10-31 13:53:35 -0400 | [diff] [blame] | 769 | m = re.search(CHANGE_ID_RE, desc) |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 770 | if not m: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 771 | return HookFailure('Change-Id must be in last paragraph of description.') |
| 772 | |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 773 | # Allow s-o-b tags to follow it, but only those. |
| 774 | end = desc[m.end():].strip().splitlines() |
| 775 | if [x for x in end if not x.startswith('Signed-off-by: ')]: |
| 776 | return HookFailure('Only "Signed-off-by:" tags may follow the Change-Id.') |
| 777 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 778 | |
Mike Frysinger | 36b2ebc | 2014-10-31 14:02:03 -0400 | [diff] [blame] | 779 | def _check_commit_message_style(_project, commit): |
| 780 | """Verify that the commit message matches our style. |
| 781 | |
| 782 | We do not check for BUG=/TEST=/etc... lines here as that is handled by other |
| 783 | commit hooks. |
| 784 | """ |
| 785 | desc = _get_commit_desc(commit) |
| 786 | |
| 787 | # The first line should be by itself. |
| 788 | lines = desc.splitlines() |
| 789 | if len(lines) > 1 and lines[1]: |
| 790 | return HookFailure('The second line of the commit message must be blank.') |
| 791 | |
| 792 | # The first line should be one sentence. |
| 793 | if '. ' in lines[0]: |
| 794 | return HookFailure('The first line cannot be more than one sentence.') |
| 795 | |
| 796 | # The first line cannot be too long. |
| 797 | MAX_FIRST_LINE_LEN = 100 |
| 798 | if len(lines[0]) > MAX_FIRST_LINE_LEN: |
| 799 | return HookFailure('The first line must be less than %i chars.' % |
| 800 | MAX_FIRST_LINE_LEN) |
| 801 | |
| 802 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 803 | def _check_license(_project, commit): |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 804 | """Verifies the license/copyright header. |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 805 | |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 806 | Should be following the spec: |
| 807 | http://dev.chromium.org/developers/coding-style#TOC-File-headers |
| 808 | """ |
| 809 | # For older years, be a bit more flexible as our policy says leave them be. |
| 810 | LICENSE_HEADER = ( |
| 811 | r'.* Copyright( \(c\))? 20[-0-9]{2,7} The Chromium OS Authors\. ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 812 | r'All rights reserved\.' r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 813 | 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] | 814 | r'be\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 815 | r'.* found in the LICENSE file\.' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 816 | r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 817 | ) |
| 818 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 819 | |
| 820 | # For newer years, be stricter. |
| 821 | COPYRIGHT_LINE = ( |
| 822 | 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] | 823 | r'All rights reserved\.' r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 824 | ) |
| 825 | copyright_re = re.compile(COPYRIGHT_LINE) |
| 826 | |
| 827 | bad_files = [] |
| 828 | bad_copyright_files = [] |
| 829 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 830 | COMMON_INCLUDED_PATHS, |
| 831 | COMMON_EXCLUDED_PATHS) |
| 832 | |
| 833 | for f in files: |
| 834 | contents = _get_file_content(f, commit) |
| 835 | if not contents: |
| 836 | # Ignore empty files. |
| 837 | continue |
| 838 | |
| 839 | if not license_re.search(contents): |
| 840 | bad_files.append(f) |
| 841 | elif copyright_re.search(contents): |
| 842 | bad_copyright_files.append(f) |
| 843 | |
| 844 | if bad_files: |
| 845 | msg = '%s:\n%s\n%s' % ( |
| 846 | 'License must match', license_re.pattern, |
| 847 | 'Found a bad header in these files:') |
| 848 | return HookFailure(msg, bad_files) |
| 849 | |
| 850 | if bad_copyright_files: |
| 851 | msg = 'Do not use (c) in copyright headers in new files:' |
| 852 | return HookFailure(msg, bad_copyright_files) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 853 | |
| 854 | |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 855 | def _check_layout_conf(_project, commit): |
| 856 | """Verifies the metadata/layout.conf file.""" |
| 857 | repo_name = 'profiles/repo_name' |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 858 | repo_names = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 859 | layout_path = 'metadata/layout.conf' |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 860 | layout_paths = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 861 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 862 | # Handle multiple overlays in a single commit (like the public tree). |
| 863 | for f in _get_affected_files(commit, relative=True): |
| 864 | if f.endswith(repo_name): |
| 865 | repo_names.append(f) |
| 866 | elif f.endswith(layout_path): |
| 867 | layout_paths.append(f) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 868 | |
| 869 | # Disallow new repos with the repo_name file. |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 870 | if repo_names: |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 871 | return HookFailure('%s: use "repo-name" in %s instead' % |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 872 | (repo_names, layout_path)) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 873 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 874 | # Gather all the errors in one pass so we show one full message. |
| 875 | all_errors = {} |
| 876 | for layout_path in layout_paths: |
| 877 | all_errors[layout_path] = errors = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 878 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 879 | # Make sure the config file is sorted. |
| 880 | data = [x for x in _get_file_content(layout_path, commit).splitlines() |
| 881 | if x and x[0] != '#'] |
| 882 | if sorted(data) != data: |
| 883 | errors += ['keep lines sorted'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 884 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 885 | # Require people to set specific values all the time. |
| 886 | settings = ( |
| 887 | # TODO: Enable this for everyone. http://crbug.com/408038 |
| 888 | #('fast caching', 'cache-format = md5-dict'), |
| 889 | ('fast manifests', 'thin-manifests = true'), |
| 890 | ('extra features', 'profile-formats = portage-2'), |
| 891 | ) |
| 892 | for reason, line in settings: |
| 893 | if line not in data: |
| 894 | errors += ['enable %s with: %s' % (reason, line)] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 895 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 896 | # Require one of these settings. |
| 897 | if ('use-manifests = true' not in data and |
| 898 | 'use-manifests = strict' not in data): |
| 899 | errors += ['enable file checking with: use-manifests = true'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 900 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 901 | # Require repo-name to be set. |
Mike Frysinger | 324cf68 | 2014-09-22 15:52:50 -0400 | [diff] [blame] | 902 | for line in data: |
| 903 | if line.startswith('repo-name = '): |
| 904 | break |
| 905 | else: |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 906 | errors += ['set the board name with: repo-name = $BOARD'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 907 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 908 | # Summarize all the errors we saw (if any). |
| 909 | lines = '' |
| 910 | for layout_path, errors in all_errors.items(): |
| 911 | if errors: |
| 912 | lines += '\n\t- '.join(['\n* %s:' % layout_path] + errors) |
| 913 | if lines: |
| 914 | lines = 'See the portage(5) man page for layout.conf details' + lines + '\n' |
| 915 | return HookFailure(lines) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 916 | |
| 917 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 918 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 919 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 920 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 921 | def _run_checkpatch(_project, commit, options=()): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 922 | """Runs checkpatch.pl on the given project""" |
| 923 | hooks_dir = _get_hooks_dir() |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 924 | options = list(options) |
| 925 | if commit == PRE_SUBMIT: |
| 926 | # The --ignore option must be present and include 'MISSING_SIGN_OFF' in |
| 927 | # this case. |
| 928 | options.append('--ignore=MISSING_SIGN_OFF') |
| 929 | cmd = ['%s/checkpatch.pl' % hooks_dir] + options + ['-'] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 930 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Che-Liang Chiou | 5ce2d7b | 2013-03-22 18:47:55 -0700 | [diff] [blame] | 931 | output = p.communicate(_get_patch(commit))[0] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 932 | if p.returncode: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 933 | return HookFailure('checkpatch.pl errors/warnings\n\n' + output) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 934 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 935 | |
Anton Staaf | 815d685 | 2011-08-22 10:08:45 -0700 | [diff] [blame] | 936 | def _run_checkpatch_no_tree(project, commit): |
| 937 | return _run_checkpatch(project, commit, ['--no-tree']) |
| 938 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 939 | |
Randall Spangler | 7318fd6 | 2013-11-21 12:16:58 -0800 | [diff] [blame] | 940 | def _run_checkpatch_ec(project, commit): |
| 941 | """Runs checkpatch with options for Chromium EC projects.""" |
| 942 | return _run_checkpatch(project, commit, ['--no-tree', |
| 943 | '--ignore=MSLEEP,VOLATILE']) |
| 944 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 945 | |
Vadim Bendebury | f853603 | 2014-06-22 12:42:18 -0700 | [diff] [blame] | 946 | def _run_checkpatch_depthcharge(project, commit): |
| 947 | """Runs checkpatch with options for depthcharge.""" |
| 948 | return _run_checkpatch(project, commit, [ |
| 949 | '--no-tree', |
Julius Werner | 83a35bb | 2014-06-23 12:51:48 -0700 | [diff] [blame] | 950 | '--ignore=CAMELCASE,C99_COMMENTS,NEW_TYPEDEFS,CONFIG_DESCRIPTION,' |
| 951 | 'SPACING,PREFER_PACKED,PREFER_PRINTF,PREFER_ALIGNED,GLOBAL_INITIALISERS,' |
| 952 | 'INITIALISED_STATIC,OPEN_BRACE,TRAILING_STATEMENTS']) |
Vadim Bendebury | f853603 | 2014-06-22 12:42:18 -0700 | [diff] [blame] | 953 | |
Vadim Bendebury | 4bcd9fa | 2014-08-07 12:27:37 -0700 | [diff] [blame] | 954 | def _run_checkpatch_coreboot(project, commit): |
| 955 | """Runs checkpatch with options for coreboot.""" |
| 956 | return _run_checkpatch(project, commit, [ |
Vadim Bendebury | ac4bc6c | 2014-08-29 19:56:43 -0700 | [diff] [blame] | 957 | '--min-conf-desc-length=2', |
Vadim Bendebury | 4bcd9fa | 2014-08-07 12:27:37 -0700 | [diff] [blame] | 958 | '--no-tree', |
| 959 | '--ignore=NEW_TYPEDEFS,PREFER_PACKED,PREFER_PRINTF,PREFER_ALIGNED,' |
Shawn Nematbakhsh | 6442c58 | 2014-08-22 14:32:06 -0700 | [diff] [blame] | 960 | 'GLOBAL_INITIALISERS,INITIALISED_STATIC,C99_COMMENTS']) |
Vadim Bendebury | 4bcd9fa | 2014-08-07 12:27:37 -0700 | [diff] [blame] | 961 | |
Vadim Bendebury | f853603 | 2014-06-22 12:42:18 -0700 | [diff] [blame] | 962 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 963 | def _kernel_configcheck(_project, commit): |
Olof Johansson | a96810f | 2012-09-04 16:20:03 -0700 | [diff] [blame] | 964 | """Makes sure kernel config changes are not mixed with code changes""" |
| 965 | files = _get_affected_files(commit) |
| 966 | if not len(_filter_files(files, [r'chromeos/config'])) in [0, len(files)]: |
| 967 | return HookFailure('Changes to chromeos/config/ and regular files must ' |
| 968 | 'be in separate commits:\n%s' % '\n'.join(files)) |
Anton Staaf | 815d685 | 2011-08-22 10:08:45 -0700 | [diff] [blame] | 969 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 970 | |
| 971 | def _run_json_check(_project, commit): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 972 | """Checks that all JSON files are syntactically valid.""" |
Dale Curtis | a039cfd | 2011-05-04 12:01:05 -0700 | [diff] [blame] | 973 | for f in _filter_files(_get_affected_files(commit), [r'.*\.json']): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 974 | try: |
| 975 | json.load(open(f)) |
| 976 | except Exception, e: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 977 | return HookFailure('Invalid JSON in %s: %s' % (f, e)) |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 978 | |
| 979 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 980 | def _check_manifests(_project, commit): |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 981 | """Make sure Manifest files only have DIST lines""" |
| 982 | paths = [] |
| 983 | |
| 984 | for path in _get_affected_files(commit): |
| 985 | if os.path.basename(path) != 'Manifest': |
| 986 | continue |
| 987 | if not os.path.exists(path): |
| 988 | continue |
| 989 | |
| 990 | with open(path, 'r') as f: |
| 991 | for line in f.readlines(): |
| 992 | if not line.startswith('DIST '): |
| 993 | paths.append(path) |
| 994 | break |
| 995 | |
| 996 | if paths: |
| 997 | return HookFailure('Please remove lines that do not start with DIST:\n%s' % |
| 998 | ('\n'.join(paths),)) |
| 999 | |
| 1000 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1001 | def _check_change_has_branch_field(_project, commit): |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1002 | """Check for a non-empty 'BRANCH=' field in the commit message.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1003 | if commit == PRE_SUBMIT: |
| 1004 | return |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1005 | BRANCH_RE = r'\nBRANCH=\S+' |
| 1006 | |
| 1007 | if not re.search(BRANCH_RE, _get_commit_desc(commit)): |
| 1008 | msg = ('Changelist description needs BRANCH field (after first line)\n' |
| 1009 | 'E.g. BRANCH=none or BRANCH=link,snow') |
| 1010 | return HookFailure(msg) |
| 1011 | |
| 1012 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1013 | def _check_change_has_signoff_field(_project, commit): |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1014 | """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] | 1015 | if commit == PRE_SUBMIT: |
| 1016 | return |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1017 | SIGNOFF_RE = r'\nSigned-off-by: \S+' |
| 1018 | |
| 1019 | if not re.search(SIGNOFF_RE, _get_commit_desc(commit)): |
| 1020 | msg = ('Changelist description needs Signed-off-by: field\n' |
| 1021 | 'E.g. Signed-off-by: My Name <me@chromium.org>') |
| 1022 | return HookFailure(msg) |
| 1023 | |
| 1024 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1025 | def _run_project_hook_script(script, project, commit): |
| 1026 | """Runs a project hook script. |
| 1027 | |
| 1028 | The script is run with the following environment variables set: |
| 1029 | PRESUBMIT_PROJECT: The affected project |
| 1030 | PRESUBMIT_COMMIT: The affected commit |
| 1031 | PRESUBMIT_FILES: A newline-separated list of affected files |
| 1032 | |
| 1033 | The script is considered to fail if the exit code is non-zero. It should |
| 1034 | write an error message to stdout. |
| 1035 | """ |
| 1036 | env = dict(os.environ) |
| 1037 | env['PRESUBMIT_PROJECT'] = project |
| 1038 | env['PRESUBMIT_COMMIT'] = commit |
| 1039 | |
| 1040 | # Put affected files in an environment variable |
| 1041 | files = _get_affected_files(commit) |
| 1042 | env['PRESUBMIT_FILES'] = '\n'.join(files) |
| 1043 | |
| 1044 | process = subprocess.Popen(script, env=env, shell=True, |
| 1045 | stdin=open(os.devnull), |
Jon Salz | 7b618af | 2012-08-31 06:03:16 +0800 | [diff] [blame] | 1046 | stdout=subprocess.PIPE, |
| 1047 | stderr=subprocess.STDOUT) |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1048 | stdout, _ = process.communicate() |
| 1049 | if process.wait(): |
Jon Salz | 7b618af | 2012-08-31 06:03:16 +0800 | [diff] [blame] | 1050 | if stdout: |
| 1051 | stdout = re.sub('(?m)^', ' ', stdout) |
| 1052 | return HookFailure('Hook script "%s" failed with code %d%s' % |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1053 | (script, process.returncode, |
| 1054 | ':\n' + stdout if stdout else '')) |
| 1055 | |
| 1056 | |
Bertrand SIMONNET | 6ec83f9 | 2014-05-30 10:42:34 -0700 | [diff] [blame] | 1057 | def _moved_to_platform2(project, _commit): |
| 1058 | """Forbids commits to legacy repo in src/platform.""" |
| 1059 | return HookFailure('%s has been moved to platform2. This change should be ' |
| 1060 | 'made there.' % project) |
| 1061 | |
| 1062 | |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1063 | def _check_project_prefix(_project, commit): |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1064 | """Require the commit message have a project specific prefix as needed.""" |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1065 | |
| 1066 | files = _get_affected_files(commit, relative=True) |
| 1067 | prefix = os.path.commonprefix(files) |
| 1068 | prefix = os.path.dirname(prefix) |
| 1069 | |
| 1070 | # If there is no common prefix, the CL span multiple projects. |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 1071 | if not prefix: |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1072 | return |
| 1073 | |
| 1074 | project_name = prefix.split('/')[0] |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 1075 | |
| 1076 | # The common files may all be within a subdirectory of the main project |
| 1077 | # directory, so walk up the tree until we find an alias file. |
| 1078 | # _get_affected_files() should return relative paths, but check against '/' to |
| 1079 | # ensure that this loop terminates even if it receives an absolute path. |
| 1080 | while prefix and prefix != '/': |
| 1081 | alias_file = os.path.join(prefix, '.project_alias') |
| 1082 | |
| 1083 | # If an alias exists, use it. |
| 1084 | if os.path.isfile(alias_file): |
| 1085 | project_name = osutils.ReadFile(alias_file).strip() |
| 1086 | |
| 1087 | prefix = os.path.dirname(prefix) |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1088 | |
| 1089 | if not _get_commit_desc(commit).startswith(project_name + ': '): |
| 1090 | return HookFailure('The commit title for changes affecting only %s' |
| 1091 | ' should start with \"%s: \"' |
| 1092 | % (project_name, project_name)) |
| 1093 | |
| 1094 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1095 | # Base |
| 1096 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1097 | # A list of hooks which are not project specific and check patch description |
| 1098 | # (as opposed to patch body). |
| 1099 | _PATCH_DESCRIPTION_HOOKS = [ |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1100 | _check_change_has_bug_field, |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 1101 | _check_change_has_valid_cq_depend, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1102 | _check_change_has_test_field, |
| 1103 | _check_change_has_proper_changeid, |
Mike Frysinger | 36b2ebc | 2014-10-31 14:02:03 -0400 | [diff] [blame] | 1104 | _check_commit_message_style, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1105 | ] |
| 1106 | |
| 1107 | |
| 1108 | # A list of hooks that are not project-specific |
| 1109 | _COMMON_HOOKS = [ |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 1110 | _check_ebuild_eapi, |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 1111 | _check_ebuild_keywords, |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 1112 | _check_ebuild_licenses, |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 1113 | _check_ebuild_virtual_pv, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1114 | _check_for_uprev, |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1115 | _check_layout_conf, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1116 | _check_license, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1117 | _check_no_long_lines, |
| 1118 | _check_no_stray_whitespace, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1119 | _check_no_tabs, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1120 | _check_portage_make_use_var, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1121 | ] |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1122 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1123 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1124 | # A dictionary of project-specific hooks(callbacks), indexed by project name. |
| 1125 | # dict[project] = [callback1, callback2] |
| 1126 | _PROJECT_SPECIFIC_HOOKS = { |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 1127 | "chromeos/autotest-tools": [_run_json_check], |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 1128 | "chromeos/overlays/chromeos-overlay": [_check_manifests], |
| 1129 | "chromeos/overlays/chromeos-partner-overlay": [_check_manifests], |
Randall Spangler | 7318fd6 | 2013-11-21 12:16:58 -0800 | [diff] [blame] | 1130 | "chromeos/platform/ec-private": [_run_checkpatch_ec, |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1131 | _check_change_has_branch_field], |
Puneet Kumar | 57b9c09 | 2012-08-14 18:58:29 -0700 | [diff] [blame] | 1132 | "chromeos/third_party/intel-framework": [_check_change_has_branch_field], |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 1133 | "chromeos/vendor/kernel-exynos-staging": [_run_checkpatch, |
| 1134 | _kernel_configcheck], |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 1135 | "chromiumos/overlays/board-overlays": [_check_manifests], |
| 1136 | "chromiumos/overlays/chromiumos-overlay": [_check_manifests], |
| 1137 | "chromiumos/overlays/portage-stable": [_check_manifests], |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1138 | "chromiumos/platform2": [_check_project_prefix], |
Vadim Bendebury | 4205285 | 2014-10-06 16:02:38 -0700 | [diff] [blame] | 1139 | "chromiumos/platform/depthcharge": [_check_change_has_branch_field, |
| 1140 | _check_change_has_signoff_field, |
Vadim Bendebury | 4bcd9fa | 2014-08-07 12:27:37 -0700 | [diff] [blame] | 1141 | _run_checkpatch_depthcharge], |
Randall Spangler | 7318fd6 | 2013-11-21 12:16:58 -0800 | [diff] [blame] | 1142 | "chromiumos/platform/ec": [_run_checkpatch_ec, |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 1143 | _check_change_has_branch_field], |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1144 | "chromiumos/platform/mosys": [_check_change_has_branch_field], |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 1145 | "chromiumos/platform/vboot_reference": [_check_change_has_branch_field], |
Vadim Bendebury | 4205285 | 2014-10-06 16:02:38 -0700 | [diff] [blame] | 1146 | "chromiumos/third_party/coreboot": [_check_change_has_branch_field, |
| 1147 | _check_change_has_signoff_field, |
Vadim Bendebury | 4bcd9fa | 2014-08-07 12:27:37 -0700 | [diff] [blame] | 1148 | _run_checkpatch_coreboot], |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1149 | "chromiumos/third_party/flashrom": [_check_change_has_branch_field], |
Mike Frysinger | df98070 | 2013-08-22 22:25:22 -0400 | [diff] [blame] | 1150 | "chromiumos/third_party/kernel": [_run_checkpatch, _kernel_configcheck], |
| 1151 | "chromiumos/third_party/kernel-next": [_run_checkpatch, |
| 1152 | _kernel_configcheck], |
Vadim Bendebury | 203fbc2 | 2014-04-22 11:58:14 -0700 | [diff] [blame] | 1153 | "chromiumos/third_party/u-boot": [_run_checkpatch_no_tree], |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1154 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1155 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1156 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1157 | # 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^] | 1158 | # that the flag controls (value). |
| 1159 | _HOOK_FLAGS = { |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1160 | 'stray_whitespace_check': _check_no_stray_whitespace, |
| 1161 | 'long_line_check': _check_no_long_lines, |
| 1162 | 'cros_license_check': _check_license, |
| 1163 | 'tab_check': _check_no_tabs, |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1164 | 'branch_check': _check_change_has_branch_field, |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1165 | 'signoff_check': _check_change_has_signoff_field, |
Josh Triplett | 0e8fc7f | 2014-04-23 16:00:00 -0700 | [diff] [blame] | 1166 | 'bug_field_check': _check_change_has_bug_field, |
| 1167 | 'test_field_check': _check_change_has_test_field, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1171 | def _get_override_hooks(config): |
| 1172 | """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] | 1173 | |
| 1174 | Expects to be called within the project root. |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1175 | |
| 1176 | Args: |
| 1177 | config: A ConfigParser for the project's config file. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1178 | """ |
| 1179 | SECTION = 'Hook Overrides' |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1180 | if not config.has_section(SECTION): |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1181 | return set(), set() |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1182 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1183 | valid_keys = set(_HOOK_FLAGS.iterkeys()) |
| 1184 | |
| 1185 | enable_flags = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1186 | disable_flags = [] |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1187 | for flag in config.options(SECTION): |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1188 | if flag not in valid_keys: |
| 1189 | raise ValueError('Error: unknown key "%s" in hook section of "%s"' % |
| 1190 | (flag, _CONFIG_FILE)) |
| 1191 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1192 | try: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1193 | if not config.getboolean(SECTION, flag): |
| 1194 | disable_flags.append(flag) |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1195 | else: |
| 1196 | enable_flags.append(flag) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1197 | except ValueError as e: |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1198 | raise ValueError('Error: parsing flag "%s" in "%s" failed: %s' % |
| 1199 | (flag, _CONFIG_FILE, e)) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1200 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1201 | enabled_hooks = set(_HOOK_FLAGS[key] for key in enable_flags) |
| 1202 | disabled_hooks = set(_HOOK_FLAGS[key] for key in disable_flags) |
| 1203 | return enabled_hooks, disabled_hooks |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1204 | |
| 1205 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1206 | def _get_project_hook_scripts(config): |
| 1207 | """Returns a list of project-specific hook scripts. |
| 1208 | |
| 1209 | Args: |
| 1210 | config: A ConfigParser for the project's config file. |
| 1211 | """ |
| 1212 | SECTION = 'Hook Scripts' |
| 1213 | if not config.has_section(SECTION): |
| 1214 | return [] |
| 1215 | |
| 1216 | hook_names_values = config.items(SECTION) |
| 1217 | hook_names_values.sort(key=lambda x: x[0]) |
| 1218 | return [x[1] for x in hook_names_values] |
| 1219 | |
| 1220 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1221 | def _get_project_hooks(project, presubmit): |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1222 | """Returns a list of hooks that need to be run for a project. |
| 1223 | |
| 1224 | Expects to be called from within the project root. |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1225 | |
| 1226 | Args: |
| 1227 | project: A string, name of the project. |
| 1228 | 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] | 1229 | """ |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1230 | config = ConfigParser.RawConfigParser() |
| 1231 | try: |
| 1232 | config.read(_CONFIG_FILE) |
| 1233 | except ConfigParser.Error: |
| 1234 | # Just use an empty config file |
| 1235 | config = ConfigParser.RawConfigParser() |
| 1236 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1237 | if presubmit: |
| 1238 | hook_list = _COMMON_HOOKS |
| 1239 | else: |
| 1240 | hook_list = _PATCH_DESCRIPTION_HOOKS + _COMMON_HOOKS |
| 1241 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame^] | 1242 | enabled_hooks, disabled_hooks = _get_override_hooks(config) |
| 1243 | hooks = (list(enabled_hooks) + |
| 1244 | [hook for hook in hook_list if hook not in disabled_hooks]) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1245 | |
| 1246 | if project in _PROJECT_SPECIFIC_HOOKS: |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1247 | hooks.extend(hook for hook in _PROJECT_SPECIFIC_HOOKS[project] |
| 1248 | if hook not in disabled_hooks) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1249 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1250 | for script in _get_project_hook_scripts(config): |
| 1251 | hooks.append(functools.partial(_run_project_hook_script, script)) |
| 1252 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1253 | return hooks |
| 1254 | |
| 1255 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1256 | def _run_project_hooks(project, proj_dir=None, |
| 1257 | commit_list=None, presubmit=False): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1258 | """For each project run its project specific hook from the hooks dictionary. |
| 1259 | |
| 1260 | Args: |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1261 | project: The name of project to run hooks for. |
| 1262 | proj_dir: If non-None, this is the directory the project is in. If None, |
| 1263 | we'll ask repo. |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1264 | commit_list: A list of commits to run hooks against. If None or empty list |
| 1265 | 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] | 1266 | 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] | 1267 | |
| 1268 | Returns: |
| 1269 | Boolean value of whether any errors were ecountered while running the hooks. |
| 1270 | """ |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1271 | if proj_dir is None: |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1272 | proj_dirs = _run_command(['repo', 'forall', project, '-c', 'pwd']).split() |
| 1273 | if len(proj_dirs) == 0: |
| 1274 | print('%s cannot be found.' % project, file=sys.stderr) |
| 1275 | print('Please specify a valid project.', file=sys.stderr) |
| 1276 | return True |
| 1277 | if len(proj_dirs) > 1: |
| 1278 | print('%s is associated with multiple directories.' % project, |
| 1279 | file=sys.stderr) |
| 1280 | print('Please specify a directory to help disambiguate.', file=sys.stderr) |
| 1281 | return True |
| 1282 | proj_dir = proj_dirs[0] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1283 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1284 | pwd = os.getcwd() |
| 1285 | # hooks assume they are run from the root of the project |
| 1286 | os.chdir(proj_dir) |
| 1287 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1288 | if not commit_list: |
| 1289 | try: |
| 1290 | commit_list = _get_commits() |
| 1291 | except VerifyException as e: |
| 1292 | PrintErrorForProject(project, HookFailure(str(e))) |
| 1293 | os.chdir(pwd) |
| 1294 | return True |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1295 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1296 | hooks = _get_project_hooks(project, presubmit) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1297 | error_found = False |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1298 | for commit in commit_list: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1299 | error_list = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1300 | for hook in hooks: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1301 | hook_error = hook(project, commit) |
| 1302 | if hook_error: |
| 1303 | error_list.append(hook_error) |
| 1304 | error_found = True |
| 1305 | if error_list: |
| 1306 | PrintErrorsForCommit(project, commit, _get_commit_desc(commit), |
| 1307 | error_list) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1308 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1309 | os.chdir(pwd) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1310 | return error_found |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1311 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1312 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1313 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1314 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1315 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1316 | def main(project_list, worktree_list=None, **_kwargs): |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1317 | """Main function invoked directly by repo. |
| 1318 | |
| 1319 | This function will exit directly upon error so that repo doesn't print some |
| 1320 | obscure error message. |
| 1321 | |
| 1322 | Args: |
| 1323 | project_list: List of projects to run on. |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1324 | worktree_list: A list of directories. It should be the same length as |
| 1325 | project_list, so that each entry in project_list matches with a directory |
| 1326 | in worktree_list. If None, we will attempt to calculate the directories |
| 1327 | automatically. |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1328 | kwargs: Leave this here for forward-compatibility. |
| 1329 | """ |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1330 | found_error = False |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1331 | if not worktree_list: |
| 1332 | worktree_list = [None] * len(project_list) |
| 1333 | for project, worktree in zip(project_list, worktree_list): |
| 1334 | if _run_project_hooks(project, proj_dir=worktree): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1335 | found_error = True |
| 1336 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1337 | if found_error: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1338 | msg = ('Preupload failed due to errors in project(s). HINTS:\n' |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1339 | '- To disable some source style checks, and for other hints, see ' |
| 1340 | '<checkout_dir>/src/repohooks/README\n' |
| 1341 | '- To upload only current project, run \'repo upload .\'') |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 1342 | print(msg, file=sys.stderr) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1343 | sys.exit(1) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 1344 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1345 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1346 | def _identify_project(path): |
| 1347 | """Identify the repo project associated with the given path. |
| 1348 | |
| 1349 | Returns: |
| 1350 | A string indicating what project is associated with the path passed in or |
| 1351 | a blank string upon failure. |
| 1352 | """ |
| 1353 | return _run_command(['repo', 'forall', '.', '-c', 'echo ${REPO_PROJECT}'], |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 1354 | stderr=subprocess.PIPE, cwd=path).strip() |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1355 | |
| 1356 | |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1357 | def direct_main(argv): |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1358 | """Run hooks directly (outside of the context of repo). |
| 1359 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1360 | Args: |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1361 | argv: The command line args to process |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1362 | |
| 1363 | Returns: |
| 1364 | 0 if no pre-upload failures, 1 if failures. |
| 1365 | |
| 1366 | Raises: |
| 1367 | BadInvocation: On some types of invocation errors. |
| 1368 | """ |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1369 | parser = commandline.ArgumentParser(description=__doc__) |
| 1370 | parser.add_argument('--dir', default=None, |
| 1371 | help='The directory that the project lives in. If not ' |
| 1372 | 'specified, use the git project root based on the cwd.') |
| 1373 | parser.add_argument('--project', default=None, |
| 1374 | help='The project repo path; this can affect how the ' |
| 1375 | 'hooks get run, since some hooks are project-specific. ' |
| 1376 | 'For chromite this is chromiumos/chromite. If not ' |
| 1377 | 'specified, the repo tool will be used to figure this ' |
| 1378 | 'out based on the dir.') |
| 1379 | parser.add_argument('--rerun-since', default=None, |
| 1380 | help='Rerun hooks on old commits since the given date. ' |
| 1381 | 'The date should match git log\'s concept of a date. ' |
| 1382 | 'e.g. 2012-06-20. This option is mutually exclusive ' |
| 1383 | 'with --pre-submit.') |
| 1384 | parser.add_argument('--pre-submit', action="store_true", |
| 1385 | help='Run the check against the pending commit. ' |
| 1386 | 'This option should be used at the \'git commit\' ' |
| 1387 | 'phase as opposed to \'repo upload\'. This option ' |
| 1388 | 'is mutually exclusive with --rerun-since.') |
| 1389 | parser.add_argument('commits', nargs='*', |
| 1390 | help='Check specific commits') |
| 1391 | opts = parser.parse_args(argv) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1392 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1393 | if opts.rerun_since: |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1394 | if opts.commits: |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1395 | raise BadInvocation('Can\'t pass commits and use rerun-since: %s' % |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1396 | ' '.join(opts.commits)) |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1397 | |
| 1398 | cmd = ['git', 'log', '--since="%s"' % opts.rerun_since, '--pretty=%H'] |
| 1399 | all_commits = _run_command(cmd).splitlines() |
| 1400 | bot_commits = _run_command(cmd + ['--author=chrome-bot']).splitlines() |
| 1401 | |
| 1402 | # Eliminate chrome-bot commits but keep ordering the same... |
| 1403 | bot_commits = set(bot_commits) |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1404 | 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] | 1405 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1406 | if opts.pre_submit: |
| 1407 | raise BadInvocation('rerun-since and pre-submit can not be ' |
| 1408 | 'used together') |
| 1409 | if opts.pre_submit: |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1410 | if opts.commits: |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1411 | raise BadInvocation('Can\'t pass commits and use pre-submit: %s' % |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1412 | ' '.join(opts.commits)) |
| 1413 | opts.commits = [PRE_SUBMIT,] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1414 | |
| 1415 | # Check/normlaize git dir; if unspecified, we'll use the root of the git |
| 1416 | # project from CWD |
| 1417 | if opts.dir is None: |
| 1418 | git_dir = _run_command(['git', 'rev-parse', '--git-dir'], |
| 1419 | stderr=subprocess.PIPE).strip() |
| 1420 | if not git_dir: |
| 1421 | raise BadInvocation('The current directory is not part of a git project.') |
| 1422 | opts.dir = os.path.dirname(os.path.abspath(git_dir)) |
| 1423 | elif not os.path.isdir(opts.dir): |
| 1424 | raise BadInvocation('Invalid dir: %s' % opts.dir) |
| 1425 | elif not os.path.isdir(os.path.join(opts.dir, '.git')): |
| 1426 | raise BadInvocation('Not a git directory: %s' % opts.dir) |
| 1427 | |
| 1428 | # Identify the project if it wasn't specified; this _requires_ the repo |
| 1429 | # tool to be installed and for the project to be part of a repo checkout. |
| 1430 | if not opts.project: |
| 1431 | opts.project = _identify_project(opts.dir) |
| 1432 | if not opts.project: |
| 1433 | raise BadInvocation("Repo couldn't identify the project of %s" % opts.dir) |
| 1434 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1435 | found_error = _run_project_hooks(opts.project, proj_dir=opts.dir, |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1436 | commit_list=opts.commits, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1437 | presubmit=opts.pre_submit) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1438 | if found_error: |
| 1439 | return 1 |
| 1440 | return 0 |
| 1441 | |
| 1442 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1443 | if __name__ == '__main__': |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1444 | sys.exit(direct_main(sys.argv[1:])) |