blob: ddac74a5b4fd0a52cee66ac113936200cacf3e58 [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.comc2993442013-01-23 14:35:58 +000012
rmistry@google.com713276b2013-01-25 18:27:34 +000013def _CheckChangeHasEol(input_api, output_api, source_file_filter=None):
14 """Checks that files end with atleast one \n (LF)."""
15 eof_files = []
16 for f in input_api.AffectedSourceFiles(source_file_filter):
17 contents = input_api.ReadFile(f, 'rb')
18 # Check that the file ends in atleast one newline character.
19 if len(contents) > 1 and contents[-1:] != '\n':
20 eof_files.append(f.LocalPath())
21
22 if eof_files:
23 return [output_api.PresubmitPromptWarning(
24 'These files should end in a newline character:',
25 items=eof_files)]
26 return []
27
28
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000029def _CommonChecks(input_api, output_api):
30 """Presubmit checks common to upload and commit."""
31 results = []
32 sources = lambda x: (x.LocalPath().endswith('.h') or
33 x.LocalPath().endswith('.gypi') or
34 x.LocalPath().endswith('.gyp') or
35 x.LocalPath().endswith('.py') or
36 x.LocalPath().endswith('.sh') or
37 x.LocalPath().endswith('.cpp'))
38 results.extend(
rmistry@google.com713276b2013-01-25 18:27:34 +000039 _CheckChangeHasEol(
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000040 input_api, output_api, source_file_filter=sources))
41 return results
42
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000043
44def CheckChangeOnUpload(input_api, output_api):
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000045 """Presubmit checks for the change on upload.
46
47 The following are the presubmit checks:
48 * Check change has one and only one EOL.
49 """
50 results = []
51 results.extend(_CommonChecks(input_api, output_api))
52 return results
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000053
54
rmistry@google.comc2993442013-01-23 14:35:58 +000055def _CheckTreeStatus(input_api, output_api, json_url):
56 """Check whether to allow commit.
57
58 Args:
59 input_api: input related apis.
60 output_api: output related apis.
61 json_url: url to download json style status.
62 """
63 tree_status_results = input_api.canned_checks.CheckTreeIsOpen(
64 input_api, output_api, json_url=json_url)
65 if not tree_status_results:
66 # Check for caution state only if tree is not closed.
67 connection = input_api.urllib2.urlopen(json_url)
68 status = input_api.json.loads(connection.read())
69 connection.close()
70 if 'caution' in status['message'].lower():
71 short_text = 'Tree state is: ' + status['general_state']
72 long_text = status['message'] + '\n' + json_url
73 tree_status_results.append(
74 output_api.PresubmitPromptWarning(
75 message=short_text, long_text=long_text))
76 return tree_status_results
77
78
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000079def CheckChangeOnCommit(input_api, output_api):
80 """Presubmit checks for the change on commit.
81
82 The following are the presubmit checks:
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000083 * Check change has one and only one EOL.
rmistry@google.comc2993442013-01-23 14:35:58 +000084 * Ensures that the Skia tree is open in
85 http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution'
86 state and an error if it is in 'Closed' state.
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000087 """
88 results = []
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000089 results.extend(_CommonChecks(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000090 results.extend(
rmistry@google.comc2993442013-01-23 14:35:58 +000091 _CheckTreeStatus(input_api, output_api, json_url=(
92 'http://skia-tree-status.appspot.com/banner-status?format=json')))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000093 return results