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 | |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 12 | import os |
| 13 | import sys |
| 14 | |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 15 | |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 16 | SKIA_TREE_STATUS_URL = 'http://skia-tree-status.appspot.com' |
| 17 | |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 18 | PUBLIC_API_OWNERS = ( |
| 19 | 'reed@chromium.org', |
| 20 | 'reed@google.com', |
| 21 | 'bsalomon@chromium.org', |
| 22 | 'bsalomon@google.com', |
| 23 | ) |
| 24 | |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 25 | |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 26 | def _CheckChangeHasEol(input_api, output_api, source_file_filter=None): |
| 27 | """Checks that files end with atleast one \n (LF).""" |
| 28 | eof_files = [] |
| 29 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 30 | contents = input_api.ReadFile(f, 'rb') |
| 31 | # Check that the file ends in atleast one newline character. |
| 32 | if len(contents) > 1 and contents[-1:] != '\n': |
| 33 | eof_files.append(f.LocalPath()) |
| 34 | |
| 35 | if eof_files: |
| 36 | return [output_api.PresubmitPromptWarning( |
| 37 | 'These files should end in a newline character:', |
| 38 | items=eof_files)] |
| 39 | return [] |
| 40 | |
| 41 | |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 42 | def _CommonChecks(input_api, output_api): |
| 43 | """Presubmit checks common to upload and commit.""" |
| 44 | results = [] |
| 45 | sources = lambda x: (x.LocalPath().endswith('.h') or |
| 46 | x.LocalPath().endswith('.gypi') or |
| 47 | x.LocalPath().endswith('.gyp') or |
| 48 | x.LocalPath().endswith('.py') or |
| 49 | x.LocalPath().endswith('.sh') or |
| 50 | x.LocalPath().endswith('.cpp')) |
| 51 | results.extend( |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 52 | _CheckChangeHasEol( |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 53 | input_api, output_api, source_file_filter=sources)) |
| 54 | return results |
| 55 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 56 | |
| 57 | def CheckChangeOnUpload(input_api, output_api): |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 58 | """Presubmit checks for the change on upload. |
| 59 | |
| 60 | The following are the presubmit checks: |
| 61 | * Check change has one and only one EOL. |
| 62 | """ |
| 63 | results = [] |
| 64 | results.extend(_CommonChecks(input_api, output_api)) |
| 65 | return results |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 66 | |
| 67 | |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 68 | def _CheckTreeStatus(input_api, output_api, json_url): |
| 69 | """Check whether to allow commit. |
| 70 | |
| 71 | Args: |
| 72 | input_api: input related apis. |
| 73 | output_api: output related apis. |
| 74 | json_url: url to download json style status. |
| 75 | """ |
| 76 | tree_status_results = input_api.canned_checks.CheckTreeIsOpen( |
| 77 | input_api, output_api, json_url=json_url) |
| 78 | if not tree_status_results: |
| 79 | # Check for caution state only if tree is not closed. |
| 80 | connection = input_api.urllib2.urlopen(json_url) |
| 81 | status = input_api.json.loads(connection.read()) |
| 82 | connection.close() |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 83 | if ('caution' in status['message'].lower() and |
| 84 | os.isatty(sys.stdout.fileno())): |
| 85 | # Display a prompt only if we are in an interactive shell. Without this |
| 86 | # check the commit queue behaves incorrectly because it considers |
| 87 | # prompts to be failures. |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 88 | short_text = 'Tree state is: ' + status['general_state'] |
| 89 | long_text = status['message'] + '\n' + json_url |
| 90 | tree_status_results.append( |
| 91 | output_api.PresubmitPromptWarning( |
| 92 | message=short_text, long_text=long_text)) |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 93 | else: |
| 94 | # Tree status is closed. Put in message about contacting sheriff. |
| 95 | connection = input_api.urllib2.urlopen( |
| 96 | SKIA_TREE_STATUS_URL + '/current-sheriff') |
| 97 | sheriff_details = input_api.json.loads(connection.read()) |
| 98 | if sheriff_details: |
| 99 | tree_status_results[0]._message += ( |
| 100 | '\n\nPlease contact the current Skia sheriff (%s) if you are trying ' |
| 101 | 'to submit a build fix\nand do not know how to submit because the ' |
| 102 | 'tree is closed') % sheriff_details['username'] |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 103 | return tree_status_results |
| 104 | |
| 105 | |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 106 | def _CheckLGTMsForPublicAPI(input_api, output_api): |
| 107 | """Check LGTMs for public API changes. |
| 108 | |
| 109 | For public API files make sure there is an LGTM from the list of owners in |
| 110 | PUBLIC_API_OWNERS. |
| 111 | """ |
| 112 | results = [] |
| 113 | requires_owner_check = False |
| 114 | for affected_svn_file in input_api.AffectedFiles(): |
| 115 | affected_file_path = affected_svn_file.AbsoluteLocalPath() |
| 116 | file_path, file_ext = os.path.splitext(affected_file_path) |
| 117 | # We only care about files that end in .h and are under the include dir. |
| 118 | if file_ext == '.h' and 'include' in file_path.split(os.path.sep): |
| 119 | requires_owner_check = True |
| 120 | |
| 121 | if not requires_owner_check: |
| 122 | return results |
| 123 | |
| 124 | lgtm_from_owner = False |
| 125 | issue = input_api.change.issue |
| 126 | if issue and input_api.rietveld: |
| 127 | issue_properties = input_api.rietveld.get_issue_properties( |
| 128 | issue=int(issue), messages=True) |
| 129 | if issue_properties['owner_email'] in PUBLIC_API_OWNERS: |
| 130 | # An owner created the CL that is an automatic LGTM. |
| 131 | lgtm_from_owner = True |
| 132 | |
| 133 | messages = issue_properties.get('messages') |
| 134 | if messages: |
| 135 | for message in messages: |
| 136 | if (message['sender'] in PUBLIC_API_OWNERS and |
| 137 | 'lgtm' in message['text'].lower()): |
| 138 | # Found an lgtm in a message from an owner. |
| 139 | lgtm_from_owner = True |
| 140 | break; |
| 141 | |
| 142 | if not lgtm_from_owner: |
| 143 | results.append( |
| 144 | output_api.PresubmitError( |
| 145 | 'Since the CL is editing public API, you must have an LGTM from ' |
| 146 | 'one of: %s' % str(PUBLIC_API_OWNERS))) |
| 147 | return results |
| 148 | |
| 149 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 150 | def CheckChangeOnCommit(input_api, output_api): |
| 151 | """Presubmit checks for the change on commit. |
| 152 | |
| 153 | The following are the presubmit checks: |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 154 | * Check change has one and only one EOL. |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 155 | * Ensures that the Skia tree is open in |
| 156 | http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution' |
| 157 | state and an error if it is in 'Closed' state. |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 158 | """ |
| 159 | results = [] |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 160 | results.extend(_CommonChecks(input_api, output_api)) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 161 | results.extend( |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 162 | _CheckTreeStatus(input_api, output_api, json_url=( |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 163 | SKIA_TREE_STATUS_URL + '/banner-status?format=json'))) |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 164 | results.extend(_CheckLGTMsForPublicAPI(input_api, output_api)) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 165 | return results |