| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium 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 |  | 
 | 5 |  | 
 | 6 | """Top-level presubmit script for Skia. | 
 | 7 |  | 
 | 8 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 
 | 9 | for more details about the presubmit API built into gcl. | 
 | 10 | """ | 
 | 11 |  | 
| commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 12 | import fnmatch | 
| rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 13 | import os | 
| commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 14 | import re | 
| rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 15 | import sys | 
| commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 16 | import traceback | 
| rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 17 |  | 
| rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 18 |  | 
| commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 19 | REVERT_CL_SUBJECT_PREFIX = 'Revert ' | 
 | 20 |  | 
| rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 21 | SKIA_TREE_STATUS_URL = 'http://skia-tree-status.appspot.com' | 
 | 22 |  | 
| rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 23 | PUBLIC_API_OWNERS = ( | 
 | 24 |     'reed@chromium.org', | 
 | 25 |     'reed@google.com', | 
 | 26 |     'bsalomon@chromium.org', | 
 | 27 |     'bsalomon@google.com', | 
 | 28 | ) | 
 | 29 |  | 
| commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 30 | AUTHORS_FILE_NAME = 'AUTHORS' | 
 | 31 |  | 
| rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 32 |  | 
| rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 33 | def _CheckChangeHasEol(input_api, output_api, source_file_filter=None): | 
 | 34 |   """Checks that files end with atleast one \n (LF).""" | 
 | 35 |   eof_files = [] | 
 | 36 |   for f in input_api.AffectedSourceFiles(source_file_filter): | 
 | 37 |     contents = input_api.ReadFile(f, 'rb') | 
 | 38 |     # Check that the file ends in atleast one newline character. | 
 | 39 |     if len(contents) > 1 and contents[-1:] != '\n': | 
 | 40 |       eof_files.append(f.LocalPath()) | 
 | 41 |  | 
 | 42 |   if eof_files: | 
 | 43 |     return [output_api.PresubmitPromptWarning( | 
 | 44 |       'These files should end in a newline character:', | 
 | 45 |       items=eof_files)] | 
 | 46 |   return [] | 
 | 47 |  | 
 | 48 |  | 
| Eric Boren | bb0ef0a | 2014-06-25 11:13:27 -0400 | [diff] [blame] | 49 | def _PythonChecks(input_api, output_api): | 
 | 50 |   """Run checks on any modified Python files.""" | 
 | 51 |   pylint_disabled_warnings = ( | 
 | 52 |       'F0401',  # Unable to import. | 
 | 53 |       'E0611',  # No name in module. | 
 | 54 |       'W0232',  # Class has no __init__ method. | 
 | 55 |       'E1002',  # Use of super on an old style class. | 
 | 56 |       'W0403',  # Relative import used. | 
 | 57 |       'R0201',  # Method could be a function. | 
 | 58 |       'E1003',  # Using class name in super. | 
 | 59 |       'W0613',  # Unused argument. | 
 | 60 |   ) | 
 | 61 |   # Run Pylint on only the modified python files. Unfortunately it still runs | 
 | 62 |   # Pylint on the whole file instead of just the modified lines. | 
 | 63 |   affected_python_files = [] | 
 | 64 |   for affected_file in input_api.AffectedSourceFiles(None): | 
 | 65 |     affected_file_path = affected_file.LocalPath() | 
 | 66 |     if affected_file_path.endswith('.py'): | 
 | 67 |       affected_python_files.append(affected_file_path) | 
 | 68 |   return input_api.canned_checks.RunPylint( | 
 | 69 |       input_api, output_api, | 
 | 70 |       disabled_warnings=pylint_disabled_warnings, | 
 | 71 |       white_list=affected_python_files) | 
 | 72 |  | 
 | 73 |  | 
| rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 74 | def _CommonChecks(input_api, output_api): | 
 | 75 |   """Presubmit checks common to upload and commit.""" | 
 | 76 |   results = [] | 
 | 77 |   sources = lambda x: (x.LocalPath().endswith('.h') or | 
 | 78 |                        x.LocalPath().endswith('.gypi') or | 
 | 79 |                        x.LocalPath().endswith('.gyp') or | 
 | 80 |                        x.LocalPath().endswith('.py') or | 
 | 81 |                        x.LocalPath().endswith('.sh') or | 
 | 82 |                        x.LocalPath().endswith('.cpp')) | 
 | 83 |   results.extend( | 
| rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 84 |       _CheckChangeHasEol( | 
| rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 85 |           input_api, output_api, source_file_filter=sources)) | 
| Eric Boren | bb0ef0a | 2014-06-25 11:13:27 -0400 | [diff] [blame] | 86 |   results.extend(_PythonChecks(input_api, output_api)) | 
| rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 87 |   return results | 
 | 88 |  | 
| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 89 |  | 
 | 90 | def CheckChangeOnUpload(input_api, output_api): | 
| rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 91 |   """Presubmit checks for the change on upload. | 
 | 92 |  | 
 | 93 |   The following are the presubmit checks: | 
 | 94 |   * Check change has one and only one EOL. | 
 | 95 |   """ | 
 | 96 |   results = [] | 
 | 97 |   results.extend(_CommonChecks(input_api, output_api)) | 
 | 98 |   return results | 
| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 99 |  | 
 | 100 |  | 
| rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 101 | def _CheckTreeStatus(input_api, output_api, json_url): | 
 | 102 |   """Check whether to allow commit. | 
 | 103 |  | 
 | 104 |   Args: | 
 | 105 |     input_api: input related apis. | 
 | 106 |     output_api: output related apis. | 
 | 107 |     json_url: url to download json style status. | 
 | 108 |   """ | 
 | 109 |   tree_status_results = input_api.canned_checks.CheckTreeIsOpen( | 
 | 110 |       input_api, output_api, json_url=json_url) | 
 | 111 |   if not tree_status_results: | 
 | 112 |     # Check for caution state only if tree is not closed. | 
 | 113 |     connection = input_api.urllib2.urlopen(json_url) | 
 | 114 |     status = input_api.json.loads(connection.read()) | 
 | 115 |     connection.close() | 
| rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 116 |     if ('caution' in status['message'].lower() and | 
 | 117 |         os.isatty(sys.stdout.fileno())): | 
 | 118 |       # Display a prompt only if we are in an interactive shell. Without this | 
 | 119 |       # check the commit queue behaves incorrectly because it considers | 
 | 120 |       # prompts to be failures. | 
| rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 121 |       short_text = 'Tree state is: ' + status['general_state'] | 
 | 122 |       long_text = status['message'] + '\n' + json_url | 
 | 123 |       tree_status_results.append( | 
 | 124 |           output_api.PresubmitPromptWarning( | 
 | 125 |               message=short_text, long_text=long_text)) | 
| rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 126 |   else: | 
 | 127 |     # Tree status is closed. Put in message about contacting sheriff. | 
 | 128 |     connection = input_api.urllib2.urlopen( | 
 | 129 |         SKIA_TREE_STATUS_URL + '/current-sheriff') | 
 | 130 |     sheriff_details = input_api.json.loads(connection.read()) | 
 | 131 |     if sheriff_details: | 
 | 132 |       tree_status_results[0]._message += ( | 
 | 133 |           '\n\nPlease contact the current Skia sheriff (%s) if you are trying ' | 
 | 134 |           'to submit a build fix\nand do not know how to submit because the ' | 
 | 135 |           'tree is closed') % sheriff_details['username'] | 
| rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 136 |   return tree_status_results | 
 | 137 |  | 
 | 138 |  | 
| commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 139 | def _CheckOwnerIsInAuthorsFile(input_api, output_api): | 
 | 140 |   results = [] | 
 | 141 |   issue = input_api.change.issue | 
 | 142 |   if issue and input_api.rietveld: | 
 | 143 |     issue_properties = input_api.rietveld.get_issue_properties(  | 
 | 144 |         issue=int(issue), messages=False) | 
 | 145 |     owner_email = issue_properties['owner_email'] | 
 | 146 |  | 
 | 147 |     try: | 
 | 148 |       authors_content = '' | 
 | 149 |       for line in open(AUTHORS_FILE_NAME): | 
 | 150 |         if not line.startswith('#'): | 
 | 151 |           authors_content += line | 
 | 152 |       email_fnmatches = re.findall('<(.*)>', authors_content) | 
 | 153 |       for email_fnmatch in email_fnmatches: | 
 | 154 |         if fnmatch.fnmatch(owner_email, email_fnmatch): | 
 | 155 |           # Found a match, the user is in the AUTHORS file break out of the loop | 
 | 156 |           break | 
 | 157 |       else: | 
 | 158 |         # TODO(rmistry): Remove the below CLA messaging once a CLA checker has | 
 | 159 |         # been added to the CQ. | 
 | 160 |         results.append( | 
 | 161 |           output_api.PresubmitError( | 
 | 162 |             'The email %s is not in Skia\'s AUTHORS file.\n' | 
 | 163 |             'Issue owner, this CL must include an addition to the Skia AUTHORS ' | 
 | 164 |             'file.\n' | 
 | 165 |             'Googler reviewers, please check that the AUTHORS entry ' | 
 | 166 |             'corresponds to an email address in http://goto/cla-signers. If it ' | 
 | 167 |             'does not then ask the issue owner to sign the CLA at ' | 
 | 168 |             'https://developers.google.com/open-source/cla/individual ' | 
 | 169 |             '(individual) or ' | 
 | 170 |             'https://developers.google.com/open-source/cla/corporate ' | 
 | 171 |             '(corporate).' | 
 | 172 |             % owner_email))  | 
 | 173 |     except IOError: | 
 | 174 |       # Do not fail if authors file cannot be found. | 
 | 175 |       traceback.print_exc() | 
 | 176 |       input_api.logging.error('AUTHORS file not found!') | 
 | 177 |  | 
 | 178 |   return results | 
 | 179 |  | 
 | 180 |  | 
| rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 181 | def _CheckLGTMsForPublicAPI(input_api, output_api): | 
 | 182 |   """Check LGTMs for public API changes. | 
 | 183 |  | 
 | 184 |   For public API files make sure there is an LGTM from the list of owners in | 
 | 185 |   PUBLIC_API_OWNERS. | 
 | 186 |   """ | 
 | 187 |   results = [] | 
 | 188 |   requires_owner_check = False | 
 | 189 |   for affected_svn_file in input_api.AffectedFiles(): | 
 | 190 |     affected_file_path = affected_svn_file.AbsoluteLocalPath() | 
 | 191 |     file_path, file_ext = os.path.splitext(affected_file_path) | 
 | 192 |     # We only care about files that end in .h and are under the include dir. | 
 | 193 |     if file_ext == '.h' and 'include' in file_path.split(os.path.sep): | 
 | 194 |       requires_owner_check = True | 
 | 195 |  | 
 | 196 |   if not requires_owner_check: | 
 | 197 |     return results | 
 | 198 |  | 
 | 199 |   lgtm_from_owner = False | 
 | 200 |   issue = input_api.change.issue | 
 | 201 |   if issue and input_api.rietveld: | 
 | 202 |     issue_properties = input_api.rietveld.get_issue_properties( | 
 | 203 |         issue=int(issue), messages=True) | 
| commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 204 |     if re.match(REVERT_CL_SUBJECT_PREFIX, issue_properties['subject'], re.I): | 
 | 205 |       # It is a revert CL, ignore the public api owners check. | 
 | 206 |       return results | 
| rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 207 |     if issue_properties['owner_email'] in PUBLIC_API_OWNERS: | 
 | 208 |       # An owner created the CL that is an automatic LGTM. | 
 | 209 |       lgtm_from_owner = True | 
 | 210 |  | 
 | 211 |     messages = issue_properties.get('messages') | 
 | 212 |     if messages: | 
 | 213 |       for message in messages: | 
 | 214 |         if (message['sender'] in PUBLIC_API_OWNERS and | 
 | 215 |             'lgtm' in message['text'].lower()): | 
 | 216 |           # Found an lgtm in a message from an owner. | 
 | 217 |           lgtm_from_owner = True | 
| Eric Boren | bb0ef0a | 2014-06-25 11:13:27 -0400 | [diff] [blame] | 218 |           break | 
| commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 219 |  | 
| rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 220 |   if not lgtm_from_owner: | 
 | 221 |     results.append( | 
 | 222 |         output_api.PresubmitError( | 
 | 223 |             'Since the CL is editing public API, you must have an LGTM from ' | 
 | 224 |             'one of: %s' % str(PUBLIC_API_OWNERS))) | 
 | 225 |   return results | 
 | 226 |  | 
 | 227 |  | 
| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 228 | def CheckChangeOnCommit(input_api, output_api): | 
 | 229 |   """Presubmit checks for the change on commit. | 
 | 230 |  | 
 | 231 |   The following are the presubmit checks: | 
| rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 232 |   * Check change has one and only one EOL. | 
| rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 233 |   * Ensures that the Skia tree is open in | 
 | 234 |     http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution' | 
 | 235 |     state and an error if it is in 'Closed' state. | 
| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 236 |   """ | 
 | 237 |   results = [] | 
| rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 238 |   results.extend(_CommonChecks(input_api, output_api)) | 
| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 239 |   results.extend( | 
| rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 240 |       _CheckTreeStatus(input_api, output_api, json_url=( | 
| rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 241 |           SKIA_TREE_STATUS_URL + '/banner-status?format=json'))) | 
| rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 242 |   results.extend(_CheckLGTMsForPublicAPI(input_api, output_api)) | 
| commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 243 |   results.extend(_CheckOwnerIsInAuthorsFile(input_api, output_api)) | 
| rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 244 |   return results |