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.
5 """Top-level presubmit script for Blink.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
17 def _CheckForVersionControlConflictsInFile(input_api
, f
):
18 pattern
= input_api
.re
.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
20 for line_num
, line
in f
.ChangedContents():
21 if pattern
.match(line
):
22 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
26 def _CheckForVersionControlConflicts(input_api
, output_api
):
27 """Usually this is not intentional and will cause a compile failure."""
29 for f
in input_api
.AffectedFiles():
30 errors
.extend(_CheckForVersionControlConflictsInFile(input_api
, f
))
34 results
.append(output_api
.PresubmitError(
35 'Version control conflict markers found, please resolve.', errors
))
39 def _CheckWatchlist(input_api
, output_api
):
40 """Check that the WATCHLIST file parses correctly."""
42 for f
in input_api
.AffectedFiles():
43 if f
.LocalPath() != 'WATCHLISTS':
49 log_buffer
= StringIO
.StringIO()
50 log_handler
= logging
.StreamHandler(log_buffer
)
51 log_handler
.setFormatter(
52 logging
.Formatter('%(levelname)s: %(message)s'))
53 logger
= logging
.getLogger()
54 logger
.addHandler(log_handler
)
56 wl
= watchlists
.Watchlists(input_api
.change
.RepositoryRoot())
58 logger
.removeHandler(log_handler
)
62 if log_buffer
.getvalue():
63 errors
.append(output_api
.PresubmitError(
64 'Cannot parse WATCHLISTS file, please resolve.',
65 log_buffer
.getvalue().splitlines()))
69 def _CommonChecks(input_api
, output_api
):
70 """Checks common to both upload and commit."""
71 # We should figure out what license checks we actually want to use.
72 license_header
= r
'.*'
75 results
.extend(input_api
.canned_checks
.PanProjectChecks(
76 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
,
77 maxlen
=800, license_header
=license_header
))
78 results
.extend(_CheckForVersionControlConflicts(input_api
, output_api
))
79 results
.extend(_CheckPatchFiles(input_api
, output_api
))
80 results
.extend(_CheckTestExpectations(input_api
, output_api
))
81 results
.extend(_CheckUnwantedDependencies(input_api
, output_api
))
82 results
.extend(_CheckChromiumPlatformMacros(input_api
, output_api
))
83 results
.extend(_CheckWatchlist(input_api
, output_api
))
84 results
.extend(_CheckFilePermissions(input_api
, output_api
))
88 def _CheckSubversionConfig(input_api
, output_api
):
89 """Verifies the subversion config file is correctly setup.
91 Checks that autoprops are enabled, returns an error otherwise.
93 join
= input_api
.os_path
.join
94 if input_api
.platform
== 'win32':
95 appdata
= input_api
.environ
.get('APPDATA', '')
97 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
98 path
= join(appdata
, 'Subversion', 'config')
100 home
= input_api
.environ
.get('HOME', '')
102 return [output_api
.PresubmitError('$HOME is not configured.')]
103 path
= join(home
, '.subversion', 'config')
106 'Please look at http://dev.chromium.org/developers/coding-style to\n'
107 'configure your subversion configuration file. This enables automatic\n'
108 'properties to simplify the project maintenance.\n'
109 'Pro-tip: just download and install\n'
110 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
113 lines
= open(path
, 'r').read().splitlines()
114 # Make sure auto-props is enabled and check for 2 Chromium standard
116 if (not '*.cc = svn:eol-style=LF' in lines
or
117 not '*.pdf = svn:mime-type=application/pdf' in lines
or
118 not 'enable-auto-props = yes' in lines
):
120 output_api
.PresubmitNotifyResult(
121 'It looks like you have not configured your subversion config '
122 'file or it is not up-to-date.\n' + error_msg
)
124 except (OSError, IOError):
126 output_api
.PresubmitNotifyResult(
127 'Can\'t find your subversion config file.\n' + error_msg
)
132 def _CheckPatchFiles(input_api
, output_api
):
133 problems
= [f
.LocalPath() for f
in input_api
.AffectedFiles()
134 if f
.LocalPath().endswith(('.orig', '.rej'))]
136 return [output_api
.PresubmitError(
137 "Don't commit .rej and .orig files.", problems
)]
142 def _CheckTestExpectations(input_api
, output_api
):
143 local_paths
= [f
.LocalPath() for f
in input_api
.AffectedFiles()]
144 if any(path
.startswith('LayoutTests') for path
in local_paths
):
145 lint_path
= input_api
.os_path
.join(input_api
.PresubmitLocalPath(),
146 'Tools', 'Scripts', 'lint-test-expectations')
147 _
, errs
= input_api
.subprocess
.Popen(
148 [input_api
.python_executable
, lint_path
],
149 stdout
=input_api
.subprocess
.PIPE
,
150 stderr
=input_api
.subprocess
.PIPE
).communicate()
152 return [output_api
.PresubmitError(
153 "lint-test-expectations failed "
154 "to produce output; check by hand. ")]
155 if errs
.strip() != 'Lint succeeded.':
156 return [output_api
.PresubmitError(errs
)]
160 def _CheckStyle(input_api
, output_api
):
161 style_checker_path
= input_api
.os_path
.join(input_api
.PresubmitLocalPath(),
162 'Tools', 'Scripts', 'check-webkit-style')
163 args
= ([input_api
.python_executable
, style_checker_path
, '--diff-files']
164 + [f
.LocalPath() for f
in input_api
.AffectedFiles()])
168 child
= input_api
.subprocess
.Popen(args
,
169 stderr
=input_api
.subprocess
.PIPE
)
170 _
, stderrdata
= child
.communicate()
171 if child
.returncode
!= 0:
172 results
.append(output_api
.PresubmitError(
173 'check-webkit-style failed', [stderrdata
]))
174 except Exception as e
:
175 results
.append(output_api
.PresubmitNotifyResult(
176 'Could not run check-webkit-style', [str(e
)]))
181 def _CheckUnwantedDependencies(input_api
, output_api
):
182 """Runs checkdeps on #include statements added in this
183 change. Breaking - rules is an error, breaking ! rules is a
186 # We need to wait until we have an input_api object and use this
187 # roundabout construct to import checkdeps because this file is
188 # eval-ed and thus doesn't have __file__.
189 original_sys_path
= sys
.path
191 sys
.path
= sys
.path
+ [input_api
.os_path
.realpath(input_api
.os_path
.join(
192 input_api
.PresubmitLocalPath(), '..', '..', 'buildtools', 'checkdeps'))]
194 from cpp_checker
import CppChecker
195 from rules
import Rule
197 # Restore sys.path to what it was before.
198 sys
.path
= original_sys_path
201 for f
in input_api
.AffectedFiles():
202 if not CppChecker
.IsCppFile(f
.LocalPath()):
205 changed_lines
= [line
for line_num
, line
in f
.ChangedContents()]
206 added_includes
.append([f
.LocalPath(), changed_lines
])
208 deps_checker
= checkdeps
.DepsChecker(
209 input_api
.os_path
.join(input_api
.PresubmitLocalPath()))
211 error_descriptions
= []
212 warning_descriptions
= []
213 for path
, rule_type
, rule_description
in deps_checker
.CheckAddedCppIncludes(
215 description_with_path
= '%s\n %s' % (path
, rule_description
)
216 if rule_type
== Rule
.DISALLOW
:
217 error_descriptions
.append(description_with_path
)
219 warning_descriptions
.append(description_with_path
)
222 if error_descriptions
:
223 results
.append(output_api
.PresubmitError(
224 'You added one or more #includes that violate checkdeps rules.',
226 if warning_descriptions
:
227 results
.append(output_api
.PresubmitPromptOrNotify(
228 'You added one or more #includes of files that are temporarily\n'
229 'allowed but being removed. Can you avoid introducing the\n'
230 '#include? See relevant DEPS file(s) for details and contacts.',
231 warning_descriptions
))
235 def _CheckChromiumPlatformMacros(input_api
, output_api
, source_file_filter
=None):
236 """Ensures that Blink code uses WTF's platform macros instead of
237 Chromium's. Using the latter has resulted in at least one subtle
239 os_macro_re
= input_api
.re
.compile(r
'^\s*#(el)?if.*\bOS_')
240 errors
= input_api
.canned_checks
._FindNewViolationsOfRule
(
241 lambda _
, x
: not os_macro_re
.search(x
),
242 input_api
, source_file_filter
)
243 errors
= ['Found use of Chromium OS_* macro in %s. '
244 'Use WTF platform macros instead.' % violation
for violation
in errors
]
246 return [output_api
.PresubmitPromptWarning('\n'.join(errors
))]
250 def _CheckForPrintfDebugging(input_api
, output_api
):
251 """Generally speaking, we'd prefer not to land patches that printf
253 printf_re
= input_api
.re
.compile(r
'^\s*printf\(')
254 errors
= input_api
.canned_checks
._FindNewViolationsOfRule
(
255 lambda _
, x
: not printf_re
.search(x
),
257 errors
= [' * %s' % violation
for violation
in errors
]
259 return [output_api
.PresubmitPromptOrNotify(
260 'printf debugging is best debugging! That said, it might '
261 'be a good idea to drop the following occurances from '
262 'your patch before uploading:\n%s' % '\n'.join(errors
))]
266 def _CheckForDangerousTestFunctions(input_api
, output_api
):
267 """Tests should not be using serveAsynchronousMockedRequests, since it does
268 not guarantee that the threaded HTML parser will have completed."""
269 serve_async_requests_re
= input_api
.re
.compile(
270 r
'serveAsynchronousMockedRequests')
271 errors
= input_api
.canned_checks
._FindNewViolationsOfRule
(
272 lambda _
, x
: not serve_async_requests_re
.search(x
),
274 errors
= [' * %s' % violation
for violation
in errors
]
276 return [output_api
.PresubmitError(
277 'You should be using FrameTestHelpers::'
278 'pumpPendingRequests() instead of '
279 'serveAsynchronousMockedRequests() in the following '
280 'locations:\n%s' % '\n'.join(errors
))]
284 def _CheckForFailInFile(input_api
, f
):
285 pattern
= input_api
.re
.compile('^FAIL')
287 for line_num
, line
in f
.ChangedContents():
288 if pattern
.match(line
):
289 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
293 def _CheckFilePermissions(input_api
, output_api
):
294 """Check that all files have their permissions properly set."""
295 if input_api
.platform
== 'win32':
297 path
= input_api
.os_path
.join(
298 '..', '..', 'tools', 'checkperms', 'checkperms.py')
299 args
= [sys
.executable
, path
, '--root', input_api
.change
.RepositoryRoot()]
300 for f
in input_api
.AffectedFiles():
301 args
+= ['--file', f
.LocalPath()]
302 checkperms
= input_api
.subprocess
.Popen(
303 args
, stdout
=input_api
.subprocess
.PIPE
)
304 errors
= checkperms
.communicate()[0].strip()
306 return [output_api
.PresubmitError(
307 'checkperms.py failed.', errors
.splitlines())]
311 def _CheckForInvalidPreferenceError(input_api
, output_api
):
312 pattern
= input_api
.re
.compile('Invalid name for preference: (.+)')
315 for f
in input_api
.AffectedFiles():
316 if not f
.LocalPath().endswith('-expected.txt'):
318 for line_num
, line
in f
.ChangedContents():
319 error
= pattern
.search(line
)
321 results
.append(output_api
.PresubmitError('Found an invalid preference %s in expected result %s:%s' % (error
.group(1), f
, line_num
)))
324 def CheckChangeOnUpload(input_api
, output_api
):
326 results
.extend(_CommonChecks(input_api
, output_api
))
327 results
.extend(_CheckStyle(input_api
, output_api
))
328 results
.extend(_CheckForPrintfDebugging(input_api
, output_api
))
329 results
.extend(_CheckForDangerousTestFunctions(input_api
, output_api
))
330 results
.extend(_CheckForInvalidPreferenceError(input_api
, output_api
))
334 def CheckChangeOnCommit(input_api
, output_api
):
336 results
.extend(_CommonChecks(input_api
, output_api
))
337 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
338 input_api
, output_api
,
339 json_url
='http://chromium-status.appspot.com/current?format=json'))
340 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
341 input_api
, output_api
))
342 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
346 def GetPreferredTryMasters(project
, change
):
352 cq_config_path
= os
.path
.join(
353 change
.RepositoryRoot(), 'infra', 'config', 'cq.cfg')
354 # commit_queue.py below is a script in depot_tools directory, which has a
355 # 'builders' command to retrieve a list of CQ builders from the CQ config.
356 is_win
= platform
.system() == 'Windows'
357 masters
= json
.loads(subprocess
.check_output(
358 ['commit_queue', 'builders', cq_config_path
], shell
=is_win
))
361 for master
in masters
:
362 try_config
.setdefault(master
, {})
363 for builder
in masters
[master
]:
364 # Do not trigger presubmit builders, since they're likely to fail
365 # (e.g. OWNERS checks before finished code review), and we're
366 # running local presubmit anyway.
367 if 'presubmit' not in builder
:
368 try_config
[master
][builder
] = ['defaulttests']