blob: c97cdd2c1a5bfcc1bfaa3387554e8baaeca3b545 [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',
23)
24
rmistry@google.com547012d2013-04-12 19:45:46 +000025
rmistry@google.com713276b2013-01-25 18:27:34 +000026def _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.com6be0b4c2013-01-17 14:50:59 +000042def _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.com713276b2013-01-25 18:27:34 +000052 _CheckChangeHasEol(
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000053 input_api, output_api, source_file_filter=sources))
54 return results
55
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000056
57def CheckChangeOnUpload(input_api, output_api):
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000058 """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.com8e3ff8c2013-01-17 12:55:34 +000066
67
rmistry@google.comc2993442013-01-23 14:35:58 +000068def _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.comf6c5f752013-03-29 17:26:00 +000083 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.comc2993442013-01-23 14:35:58 +000088 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.com547012d2013-04-12 19:45:46 +000093 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.comc2993442013-01-23 14:35:58 +0000103 return tree_status_results
104
105
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000106def _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.com8e3ff8c2013-01-17 12:55:34 +0000150def CheckChangeOnCommit(input_api, output_api):
151 """Presubmit checks for the change on commit.
152
153 The following are the presubmit checks:
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000154 * Check change has one and only one EOL.
rmistry@google.comc2993442013-01-23 14:35:58 +0000155 * 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.com8e3ff8c2013-01-17 12:55:34 +0000158 """
159 results = []
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000160 results.extend(_CommonChecks(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000161 results.extend(
rmistry@google.comc2993442013-01-23 14:35:58 +0000162 _CheckTreeStatus(input_api, output_api, json_url=(
rmistry@google.com547012d2013-04-12 19:45:46 +0000163 SKIA_TREE_STATUS_URL + '/banner-status?format=json')))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000164 results.extend(_CheckLGTMsForPublicAPI(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000165 return results