Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: PRESUBMIT.py

Issue 341193004: Add lots of utils, PRESUBMIT.py (Closed) Base URL: https://skia.googlesource.com/common.git@master
Patch Set: Remove another pritn Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « .gitignore ('k') | py/__init__.py » ('j') | py/utils/misc.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2014 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 """Presubmit checks for common code."""
7
8 import os
9 import subprocess
10 import sys
11
12
13 SKIA_TREE_STATUS_URL = 'http://skia-tree-status.appspot.com'
14 SKIP_RUNS_KEYWORD = '(SkipBuildbotRuns)'
15
16
17 def _RunUnitTests(input_api, output_api):
rmistry 2014/06/20 16:44:03 _RunUnitTests -> _RunPyUnitTests
borenet 2014/06/20 16:55:02 Done.
18 """ Run the unit tests and return a list of strings containing any errors. """
19 results = []
20 success = True
21 try:
22 proc = subprocess.Popen(['python', 'run_unittests'],
23 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
24 success = proc.wait() == 0
25 long_text = proc.communicate()[0]
26 except Exception:
27 success = False
28 long_text = 'Failed to run the common tests!'
29 if not success:
30 results.append(output_api.PresubmitPromptWarning(
31 message='One or more unit tests failed.',
32 long_text=long_text))
33 return results
34
35
36 def CheckChange(input_api, output_api):
37 """Presubmit checks for the change on upload or commit.
38
39 The presubmit checks have been handpicked from the list of canned checks
40 here:
41 http://src.chromium.org/viewvc/chrome/trunk/tools/depot_tools/presubmit_canned _checks.py
tfarina 2014/06/20 16:45:19 can you use the url from chromium.googlesource.com
borenet 2014/06/20 16:55:02 Done.
42
43 The following are the presubmit checks:
44 * Pylint is run if the change contains any .py files.
45 * Enforces max length for all lines is 80.
46 * Checks that the user didn't add TODO(name) without an owner.
47 * Checks that there is no stray whitespace at source lines end.
48 * Checks that there are no tab characters in any of the text files.
49 """
50 results = []
51
52 pylint_disabled_warnings = (
53 'F0401', # Unable to import.
54 'E0611', # No name in module.
55 'W0232', # Class has no __init__ method.
56 'E1002', # Use of super on an old style class.
57 'W0403', # Relative import used.
58 'R0201', # Method could be a function.
59 'E1003', # Using class name in super.
60 'W0613', # Unused argument.
61 )
62 # Run Pylint on only the modified python files. Unfortunately it still runs
63 # Pylint on the whole file instead of just the modified lines.
64 affected_python_files = []
65 for affected_file in input_api.AffectedSourceFiles(None):
66 affected_file_path = affected_file.LocalPath()
67 if affected_file_path.endswith('.py'):
68 affected_python_files.append(affected_file_path)
69 results += input_api.canned_checks.RunPylint(
70 input_api, output_api,
71 disabled_warnings=pylint_disabled_warnings,
72 white_list=affected_python_files)
73
74 # Use 100 for max length for files other than python. Python length is
75 # already checked during the Pylint above.
76 results += input_api.canned_checks.CheckLongLines(input_api, output_api, 100)
77 results += input_api.canned_checks.CheckChangeTodoHasOwner(
78 input_api, output_api)
79 results += input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
80 input_api, output_api)
81 results += input_api.canned_checks.CheckChangeHasNoTabs(input_api, output_api)
82
83 results += _RunUnitTests(input_api, output_api)
84
85 return results
86
87
88 def _CheckTreeStatus(input_api, output_api, json_url):
89 """Check whether to allow commit.
90
91 Args:
92 input_api: input related apis.
93 output_api: output related apis.
94 json_url: url to download json style status.
95 """
96 results = []
97 tree_status_results = input_api.canned_checks.CheckTreeIsOpen(
98 input_api, output_api, json_url=json_url)
99 display_skip_keyword_prompt = False
100 tree_status = None
101 if not tree_status_results:
102 # Check for caution state only if tree is not closed.
103 connection = input_api.urllib2.urlopen(json_url)
104 status = input_api.json.loads(connection.read())
105 connection.close()
106 tree_status = status['message']
107 if 'caution' in tree_status.lower():
108 # Display a prompt only if we are in an interactive shell. Without this
109 # check the commit queue behaves incorrectly because it considers
110 # prompts to be failures.
111 display_skip_keyword_prompt = True
112 else:
113 # pylint: disable=W0212
114 tree_status = tree_status_results[0]._message
115 display_skip_keyword_prompt = True
116
117 if (display_skip_keyword_prompt
118 and os.isatty(sys.stdout.fileno())
119 and not SKIP_RUNS_KEYWORD in input_api.change.DescriptionText()):
120 long_text = (
121 '%s\nAre you sure the change should be submitted. If it should be '
122 'submitted but not run on the buildbots you can use the %s keyword.' % (
123 tree_status, SKIP_RUNS_KEYWORD))
124 results.append(output_api.PresubmitPromptWarning(
125 message=tree_status, long_text=long_text))
126 return results
127
128
129 def CheckChangeOnUpload(input_api, output_api):
130 return CheckChange(input_api, output_api)
131
132
133 def CheckChangeOnCommit(input_api, output_api):
134 results = CheckChange(input_api, output_api)
135 results.extend(
136 _CheckTreeStatus(input_api, output_api, json_url=(
137 SKIA_TREE_STATUS_URL + '/banner-status?format=json')))
138 return results
139
OLDNEW
« no previous file with comments | « .gitignore ('k') | py/__init__.py » ('j') | py/utils/misc.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698