Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 5 | import json |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 6 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 7 | import re |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 8 | import sys |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 9 | import subprocess |
| 10 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 11 | |
| 12 | # General Helpers |
| 13 | |
| 14 | COMMON_INCLUDED_PATHS = [ |
| 15 | # C++ and friends |
| 16 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 17 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 18 | # Scripts |
| 19 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 20 | # No extension at all, note that ALL CAPS files are black listed in |
| 21 | # COMMON_EXCLUDED_LIST below. |
| 22 | r"(^|.*?[\\\/])[^.]+$", |
| 23 | # Other |
| 24 | r".*\.java$", r".*\.mk$", r".*\.am$", |
| 25 | ] |
| 26 | |
| 27 | COMMON_EXCLUDED_PATHS = [ |
Ryan Cui | e37fe1a | 2011-05-03 19:00:10 -0700 | [diff] [blame^] | 28 | # don't do source file checks for all third_party projects for now |
| 29 | r"/src/third_party/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 30 | r".*\bexperimental[\\\/].*", |
| 31 | r".*\b[A-Z0-9_]{2,}$", |
| 32 | r".*[\\\/]debian[\\\/]rules$", |
| 33 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 34 | |
| 35 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 36 | """Returns the absolute path to the repohooks directory.""" |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 37 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
| 38 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip() |
| 39 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 40 | def _match_regex_list(subject, expressions): |
| 41 | """Try to match a list of regular expressions to a string. |
| 42 | |
| 43 | Args: |
| 44 | subject: The string to match regexes on |
| 45 | expressions: A list of regular expressions to check for matches with. |
| 46 | |
| 47 | Returns: |
| 48 | Whether the passed in subject matches any of the passed in regexes. |
| 49 | """ |
| 50 | for expr in expressions: |
| 51 | if (re.search(expr, subject)): |
| 52 | return True |
| 53 | return False |
| 54 | |
| 55 | def _filter_files(files, include_list, exclude_list=[]): |
| 56 | """Filter out files based on the conditions passed in. |
| 57 | |
| 58 | Args: |
| 59 | files: list of filepaths to filter |
| 60 | include_list: list of regex that when matched with a file path will cause it |
| 61 | to be added to the output list unless the file is also matched with a |
| 62 | regex in the exclude_list. |
| 63 | exclude_list: list of regex that when matched with a file will prevent it |
| 64 | from being added to the output list, even if it is also matched with a |
| 65 | regex in the include_list. |
| 66 | |
| 67 | Returns: |
| 68 | A list of filepaths that contain files matched in the include_list and not |
| 69 | in the exclude_list. |
| 70 | """ |
| 71 | filtered = [] |
| 72 | for f in files: |
| 73 | if (_match_regex_list(f, include_list) and |
| 74 | not _match_regex_list(f, exclude_list)): |
| 75 | filtered.append(f) |
| 76 | return filtered |
| 77 | |
| 78 | def _report_error(msg, items=None): |
| 79 | """Raises an exception with the passed in error message. |
| 80 | |
| 81 | If extra error detail is passed in, it will be appended to the error message. |
| 82 | |
| 83 | Args: |
| 84 | msg: Error message header. |
| 85 | items: A list of lines that follow the header that give extra error |
| 86 | information. |
| 87 | """ |
| 88 | if items: |
| 89 | msg += '\n' + '\n'.join(items) |
| 90 | raise Exception(msg) |
| 91 | |
| 92 | |
| 93 | # Git Helpers |
| 94 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 95 | def _get_diff(commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 96 | """Returns the diff for this commit.""" |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 97 | cmd = ['git', 'show', commit] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 98 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 99 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 100 | def _get_file_diff(file, commit): |
| 101 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
| 102 | cmd = ['git', 'show', '-p', '--no-ext-diff', commit, file] |
| 103 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 104 | |
| 105 | new_lines = [] |
| 106 | line_num = 0 |
| 107 | for line in output.splitlines(): |
| 108 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 109 | if m: |
| 110 | line_num = int(m.groups(1)[0]) |
| 111 | continue |
| 112 | if line.startswith('+') and not line.startswith('++'): |
| 113 | new_lines.append((line_num, line[1:])) |
| 114 | if not line.startswith('-'): |
| 115 | line_num += 1 |
| 116 | return new_lines |
| 117 | |
| 118 | def _get_affected_files(commit): |
| 119 | """Returns list of absolute filepaths that were modified/added.""" |
| 120 | cmd = ['git', 'diff', '--name-status', commit + '^!'] |
| 121 | output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 122 | files = [] |
| 123 | for statusline in output.splitlines(): |
| 124 | m = re.match('^(\w)+\t(.+)$', statusline.rstrip()) |
| 125 | # Ignore deleted files, and return absolute paths of files |
| 126 | if (m.group(1)[0] != 'D'): |
| 127 | pwd = os.getcwd() |
| 128 | files.append(os.path.join(pwd, m.group(2))) |
| 129 | return files |
| 130 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 131 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 132 | """Returns a list of commits for this review.""" |
| 133 | cmd = ['git', 'log', 'm/master..', '--format=%H'] |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 134 | commits = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 135 | return commits.split() |
| 136 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 137 | def _get_commit_desc(commit): |
| 138 | """Returns the full commit message of a commit.""" |
| 139 | cmd = ['git', 'log', '--format=%B', commit + '^!'] |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 140 | return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 141 | |
| 142 | |
| 143 | # Common Hooks |
| 144 | |
| 145 | def _check_no_long_lines(project, commit): |
| 146 | """Checks that there aren't any lines longer than maxlen characters in any of |
| 147 | the text files to be submitted. |
| 148 | """ |
| 149 | MAX_LEN = 80 |
| 150 | |
| 151 | errors = [] |
| 152 | files = _filter_files(_get_affected_files(commit), |
| 153 | COMMON_INCLUDED_PATHS, |
| 154 | COMMON_EXCLUDED_PATHS) |
| 155 | |
| 156 | for afile in files: |
| 157 | for line_num, line in _get_file_diff(afile, commit): |
| 158 | # Allow certain lines to exceed the maxlen rule. |
| 159 | if (len(line) > MAX_LEN and |
| 160 | not 'http://' in line and |
| 161 | not 'https://' in line and |
| 162 | not line.startswith('#define') and |
| 163 | not line.startswith('#include') and |
| 164 | not line.startswith('#import') and |
| 165 | not line.startswith('#pragma') and |
| 166 | not line.startswith('#if') and |
| 167 | not line.startswith('#endif')): |
| 168 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 169 | if len(errors) == 5: # Just show the first 5 errors. |
| 170 | break |
| 171 | |
| 172 | if errors: |
| 173 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
| 174 | _report_error(msg, errors) |
| 175 | |
| 176 | def _check_no_stray_whitespace(project, commit): |
| 177 | """Checks that there is no stray whitespace at source lines end.""" |
| 178 | errors = [] |
| 179 | files = _filter_files(_get_affected_files(commit), |
| 180 | COMMON_INCLUDED_PATHS, |
| 181 | COMMON_EXCLUDED_PATHS) |
| 182 | |
| 183 | for afile in files: |
| 184 | for line_num, line in _get_file_diff(afile, commit): |
| 185 | if line.rstrip() != line: |
| 186 | errors.append('%s, line %s' % (afile, line_num)) |
| 187 | if errors: |
| 188 | _report_error('Found line ending with white space in:', errors) |
| 189 | |
| 190 | def _check_no_tabs(project, commit): |
| 191 | """Checks there are no unexpanded tabs.""" |
| 192 | TAB_OK_PATHS = [ |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 193 | r".*\.ebuild$", |
| 194 | r".*\.eclass$", |
| 195 | r".*/[M|m]akefile$" |
| 196 | ] |
| 197 | |
| 198 | errors = [] |
| 199 | files = _filter_files(_get_affected_files(commit), |
| 200 | COMMON_INCLUDED_PATHS, |
| 201 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
| 202 | |
| 203 | for afile in files: |
| 204 | for line_num, line in _get_file_diff(afile, commit): |
| 205 | if '\t' in line: |
| 206 | errors.append('%s, line %s' % (afile, line_num)) |
| 207 | if errors: |
| 208 | _report_error('Found a tab character in:', errors) |
| 209 | |
| 210 | def _check_change_has_test_field(project, commit): |
| 211 | """Check for a non-empty 'TEST=' field in the commit message.""" |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 212 | TEST_RE = r'\n\s*TEST\s*=[^\n]*\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 213 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 214 | if not re.search(TEST_RE, _get_commit_desc(commit)): |
| 215 | _report_error('Changelist description needs TEST field (after first line)') |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 216 | |
| 217 | def _check_change_has_bug_field(project, commit): |
| 218 | """Check for a non-empty 'BUG=' field in the commit message.""" |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 219 | BUG_RE = r'\n\s*BUG\s*=[^\n]*\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 220 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 221 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
| 222 | _report_error('Changelist description needs BUG field (after first line)') |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 223 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 224 | def _check_change_has_proper_changeid(project, commit): |
| 225 | """Verify that Change-ID is present in last paragraph of commit message.""" |
| 226 | desc = _get_commit_desc(commit) |
| 227 | loc = desc.rfind('\nChange-Id:') |
| 228 | if loc == -1 or re.search('\n\s*\n\s*\S+', desc[loc:]): |
| 229 | _report_error('Change-Id must be in last paragraph of description.') |
| 230 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 231 | def _check_license(project, commit): |
| 232 | """Verifies the license header.""" |
| 233 | LICENSE_HEADER = ( |
| 234 | r".*? Copyright \(c\) 20[-0-9]{2,7} The Chromium OS Authors\. All rights " |
| 235 | r"reserved\." "\n" |
| 236 | r".*? Use of this source code is governed by a BSD-style license that can " |
| 237 | "be\n" |
| 238 | r".*? found in the LICENSE file\." |
| 239 | "\n" |
| 240 | ) |
| 241 | |
| 242 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 243 | bad_files = [] |
| 244 | files = _filter_files(_get_affected_files(commit), |
| 245 | COMMON_INCLUDED_PATHS, |
| 246 | COMMON_EXCLUDED_PATHS) |
| 247 | |
| 248 | for f in files: |
| 249 | contents = open(f).read() |
| 250 | if len(contents) == 0: continue # Ignore empty files |
| 251 | if not license_re.search(contents): |
| 252 | bad_files.append(f) |
| 253 | if bad_files: |
| 254 | _report_error('License must match:\n%s\n' % license_re.pattern + |
| 255 | 'Found a bad license header in these files:', |
| 256 | bad_files) |
| 257 | |
| 258 | |
| 259 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 260 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 261 | def _run_checkpatch(project, commit): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 262 | """Runs checkpatch.pl on the given project""" |
| 263 | hooks_dir = _get_hooks_dir() |
| 264 | cmd = ['%s/checkpatch.pl' % hooks_dir, '-'] |
| 265 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 266 | output = p.communicate(_get_diff(commit))[0] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 267 | if p.returncode: |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 268 | _report_error('checkpatch.pl errors/warnings\n\n' + output) |
| 269 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 270 | |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 271 | def _run_json_check(project, commit): |
| 272 | """Checks that all JSON files are syntactically valid.""" |
Dale Curtis | a039cfd | 2011-05-04 12:01:05 -0700 | [diff] [blame] | 273 | for f in _filter_files(_get_affected_files(commit), [r'.*\.json']): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 274 | try: |
| 275 | json.load(open(f)) |
| 276 | except Exception, e: |
| 277 | _report_error('Invalid JSON in %s: %s' % (f, e)) |
| 278 | |
| 279 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 280 | # Base |
| 281 | |
Ryan Cui | e37fe1a | 2011-05-03 19:00:10 -0700 | [diff] [blame^] | 282 | COMMON_HOOKS = [_check_change_has_bug_field, |
| 283 | _check_change_has_test_field, |
| 284 | _check_change_has_proper_changeid, |
| 285 | _check_no_long_lines, |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 286 | _check_no_stray_whitespace, |
| 287 | _check_no_tabs, |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 288 | _check_license] |
| 289 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 290 | def _setup_project_hooks(): |
| 291 | """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]""" |
| 292 | return { |
Doug Anderson | 830216f | 2011-05-02 10:08:37 -0700 | [diff] [blame] | 293 | "chromiumos/third_party/kernel": [_run_checkpatch], |
| 294 | "chromiumos/third_party/kernel-next": [_run_checkpatch], |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 295 | "chromeos/autotest-tools": [_run_json_check], |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | def _run_project_hooks(project, hooks): |
| 299 | """For each project run its project specific hook from the hooks dictionary""" |
| 300 | cmd = ['repo', 'forall', project, '-c', 'pwd'] |
| 301 | proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 302 | proj_dir = proj_dir.strip() |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 303 | pwd = os.getcwd() |
| 304 | # hooks assume they are run from the root of the project |
| 305 | os.chdir(proj_dir) |
| 306 | |
| 307 | project_specific_hooks = [] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 308 | if project in hooks: |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 309 | project_specific_hooks = hooks[project] |
| 310 | |
| 311 | for commit in _get_commits(): |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 312 | try: |
| 313 | for hook in COMMON_HOOKS + project_specific_hooks: |
| 314 | hook(project, commit) |
| 315 | except: |
| 316 | msg = 'ERROR: pre-upload failed: commit=%s, project=%s' % (commit[:8], |
| 317 | project) |
| 318 | print >> sys.stderr, msg |
| 319 | raise |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 320 | os.chdir(pwd) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 321 | |
| 322 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 323 | |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 324 | def main(project_list, **kwargs): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 325 | hooks = _setup_project_hooks() |
| 326 | for project in project_list: |
| 327 | _run_project_hooks(project, hooks) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 328 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 329 | if __name__ == '__main__': |
| 330 | main() |