blob: 92da271705af016cefb90e46e06361e71662433c [file] [log] [blame]
Lann Martin46361ee2019-03-14 15:30:47 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
David Burgerc0effc22020-03-04 08:27:07 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Lann Martin46361ee2019-03-14 15:30:47 -06004
recipe-roller4202bc62020-09-17 17:38:22 -07005import git_cl
Madeleine Hardt93db4832021-08-13 17:48:24 +00006from subprocess2 import CalledProcessError
7
George Engelbrecht1344a582021-08-24 09:56:25 -06008UNLINTABLE_FILES = set(['recipes.py'])
9
Madeleine Hardt93db4832021-08-13 17:48:24 +000010
11def PylintCheck(input_api, output_api):
12 """Run pylint checks for modified files."""
13 pylint_errors = []
George Engelbrechtb3f23ac2021-08-25 13:06:17 -060014 for affected in input_api.AffectedFiles(include_deletes=False):
George Engelbrecht1344a582021-08-24 09:56:25 -060015 affected_str = str(affected)
16 if affected_str.endswith(".py"):
17 if affected_str in UNLINTABLE_FILES:
18 continue
Madeleine Hardt93db4832021-08-13 17:48:24 +000019 try:
20 input_api.subprocess.check_output(
21 ['pylint', '--rcfile', 'pylintrc',
George Engelbrecht1344a582021-08-24 09:56:25 -060022 '%s' % (affected_str)])
Madeleine Hardt93db4832021-08-13 17:48:24 +000023 except CalledProcessError as error:
24 pylint_errors.append("%s" % error)
25 if pylint_errors:
26 pylint_errors = [
27 output_api.PresubmitError((
28 'Please fix pylint errors or add \'#pylint: disable=your-error\' in the source code '
29 'at the end of the relevant line:\n\n%s') %
30 "\n".join(pylint_errors))
31 ]
32 return pylint_errors
recipe-roller4202bc62020-09-17 17:38:22 -070033
Lann Martin46361ee2019-03-14 15:30:47 -060034
35def CommonChecks(input_api, output_api):
Josip Sokcevic52f54362021-07-13 07:30:36 -070036 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
37 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
38 file_filter=file_filter)
Lann Martin46361ee2019-03-14 15:30:47 -060039
40 # recipes.py test run
41 results += input_api.RunTests([
42 input_api.Command(
43 name='recipes test',
44 cmd=[input_api.python_executable, 'recipes.py', 'test', 'run'],
45 kwargs={},
46 message=output_api.PresubmitError,
47 )
48 ])
Madeleine Hardt93db4832021-08-13 17:48:24 +000049 results += PylintCheck(input_api, output_api)
Lann Martin46361ee2019-03-14 15:30:47 -060050
recipe-roller4202bc62020-09-17 17:38:22 -070051 # Python formatting issues are errors, but we need to ignore recipes.py, which
52 # we do not control.
53 bad_format = False
54 cmd = [
55 '-C',
56 input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run',
57 '--presubmit', '--python', '--no-clang-format', '--diff'
58 ]
Madeleine Hardt93db4832021-08-13 17:48:24 +000059 _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True)
recipe-roller4202bc62020-09-17 17:38:22 -070060 for line in out.splitlines():
61 if line.startswith('--- ') or line.startswith('+++ '):
62 if not ' recipes.py\t' in line:
63 bad_format = True
64 break
65
66 if bad_format:
67 results += input_api.canned_checks.CheckPatchFormatted(
68 input_api, output_api, check_python=True, check_clang_format=False,
69 result_factory=output_api.PresubmitError)
Andrew Lamb9b2d0862019-04-03 16:38:52 -060070
Lann Martin46361ee2019-03-14 15:30:47 -060071 return results
72
73
74CheckChangeOnUpload = CommonChecks
75CheckChangeOnCommit = CommonChecks