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