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