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 | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 16 | def _CheckChangeHasEol(input_api, output_api, source_file_filter=None): |
| 17 | """Checks that files end with atleast one \n (LF).""" |
| 18 | eof_files = [] |
| 19 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 20 | contents = input_api.ReadFile(f, 'rb') |
| 21 | # Check that the file ends in atleast one newline character. |
| 22 | if len(contents) > 1 and contents[-1:] != '\n': |
| 23 | eof_files.append(f.LocalPath()) |
| 24 | |
| 25 | if eof_files: |
| 26 | return [output_api.PresubmitPromptWarning( |
| 27 | 'These files should end in a newline character:', |
| 28 | items=eof_files)] |
| 29 | return [] |
| 30 | |
| 31 | |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 32 | def _CommonChecks(input_api, output_api): |
| 33 | """Presubmit checks common to upload and commit.""" |
| 34 | results = [] |
| 35 | sources = lambda x: (x.LocalPath().endswith('.h') or |
| 36 | x.LocalPath().endswith('.gypi') or |
| 37 | x.LocalPath().endswith('.gyp') or |
| 38 | x.LocalPath().endswith('.py') or |
| 39 | x.LocalPath().endswith('.sh') or |
| 40 | x.LocalPath().endswith('.cpp')) |
| 41 | results.extend( |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 42 | _CheckChangeHasEol( |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 43 | input_api, output_api, source_file_filter=sources)) |
| 44 | return results |
| 45 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 46 | |
| 47 | def CheckChangeOnUpload(input_api, output_api): |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 48 | """Presubmit checks for the change on upload. |
| 49 | |
| 50 | The following are the presubmit checks: |
| 51 | * Check change has one and only one EOL. |
| 52 | """ |
| 53 | results = [] |
| 54 | results.extend(_CommonChecks(input_api, output_api)) |
| 55 | return results |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 56 | |
| 57 | |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 58 | def _CheckTreeStatus(input_api, output_api, json_url): |
| 59 | """Check whether to allow commit. |
| 60 | |
| 61 | Args: |
| 62 | input_api: input related apis. |
| 63 | output_api: output related apis. |
| 64 | json_url: url to download json style status. |
| 65 | """ |
| 66 | tree_status_results = input_api.canned_checks.CheckTreeIsOpen( |
| 67 | input_api, output_api, json_url=json_url) |
| 68 | if not tree_status_results: |
| 69 | # Check for caution state only if tree is not closed. |
| 70 | connection = input_api.urllib2.urlopen(json_url) |
| 71 | status = input_api.json.loads(connection.read()) |
| 72 | connection.close() |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 73 | if ('caution' in status['message'].lower() and |
| 74 | os.isatty(sys.stdout.fileno())): |
| 75 | # Display a prompt only if we are in an interactive shell. Without this |
| 76 | # check the commit queue behaves incorrectly because it considers |
| 77 | # prompts to be failures. |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 78 | short_text = 'Tree state is: ' + status['general_state'] |
| 79 | long_text = status['message'] + '\n' + json_url |
| 80 | tree_status_results.append( |
| 81 | output_api.PresubmitPromptWarning( |
| 82 | message=short_text, long_text=long_text)) |
| 83 | return tree_status_results |
| 84 | |
| 85 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 86 | def CheckChangeOnCommit(input_api, output_api): |
| 87 | """Presubmit checks for the change on commit. |
| 88 | |
| 89 | The following are the presubmit checks: |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 90 | * Check change has one and only one EOL. |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 91 | * Ensures that the Skia tree is open in |
| 92 | http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution' |
| 93 | state and an error if it is in 'Closed' state. |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 94 | """ |
| 95 | results = [] |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 96 | results.extend(_CommonChecks(input_api, output_api)) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 97 | results.extend( |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 98 | _CheckTreeStatus(input_api, output_api, json_url=( |
| 99 | 'http://skia-tree-status.appspot.com/banner-status?format=json'))) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 100 | return results |