blob: 6aa57ff1ff2c6ddfc14528d44a22f1399db56eb8 [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
8
9def PylintCheck(input_api, output_api):
10 """Run pylint checks for modified files."""
11 pylint_errors = []
12 for affected in input_api.AffectedFiles():
13 if str(affected).endswith(".py"):
14 try:
15 input_api.subprocess.check_output(
16 ['pylint', '--rcfile', 'pylintrc',
17 '%s' % (affected)])
18 except CalledProcessError as error:
19 pylint_errors.append("%s" % error)
20 if pylint_errors:
21 pylint_errors = [
22 output_api.PresubmitError((
23 'Please fix pylint errors or add \'#pylint: disable=your-error\' in the source code '
24 'at the end of the relevant line:\n\n%s') %
25 "\n".join(pylint_errors))
26 ]
27 return pylint_errors
recipe-roller4202bc62020-09-17 17:38:22 -070028
Lann Martin46361ee2019-03-14 15:30:47 -060029
30def CommonChecks(input_api, output_api):
Josip Sokcevic52f54362021-07-13 07:30:36 -070031 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
32 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
33 file_filter=file_filter)
Lann Martin46361ee2019-03-14 15:30:47 -060034
35 # recipes.py test run
36 results += input_api.RunTests([
37 input_api.Command(
38 name='recipes test',
39 cmd=[input_api.python_executable, 'recipes.py', 'test', 'run'],
40 kwargs={},
41 message=output_api.PresubmitError,
42 )
43 ])
Madeleine Hardt93db4832021-08-13 17:48:24 +000044 results += PylintCheck(input_api, output_api)
Lann Martin46361ee2019-03-14 15:30:47 -060045
recipe-roller4202bc62020-09-17 17:38:22 -070046 # Python formatting issues are errors, but we need to ignore recipes.py, which
47 # we do not control.
48 bad_format = False
49 cmd = [
50 '-C',
51 input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run',
52 '--presubmit', '--python', '--no-clang-format', '--diff'
53 ]
Madeleine Hardt93db4832021-08-13 17:48:24 +000054 _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True)
recipe-roller4202bc62020-09-17 17:38:22 -070055 for line in out.splitlines():
56 if line.startswith('--- ') or line.startswith('+++ '):
57 if not ' recipes.py\t' in line:
58 bad_format = True
59 break
60
61 if bad_format:
62 results += input_api.canned_checks.CheckPatchFormatted(
63 input_api, output_api, check_python=True, check_clang_format=False,
64 result_factory=output_api.PresubmitError)
Andrew Lamb9b2d0862019-04-03 16:38:52 -060065
Lann Martin46361ee2019-03-14 15:30:47 -060066 return results
67
68
69CheckChangeOnUpload = CommonChecks
70CheckChangeOnCommit = CommonChecks