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