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