blob: 0f633d980e824f476985c873f7cc5216e0f74a1e [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."""
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060013
Madeleine Hardt93db4832021-08-13 17:48:24 +000014 pylint_errors = []
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060015
16 # Find pylint (currently v1.5).
17 canned_checks_path = input_api.canned_checks.__file__
18 canned_checks_path = input_api.os_path.abspath(canned_checks_path)
19 depot_tools_path = input_api.os_path.dirname(canned_checks_path)
20 pylint_path = input_api.os_path.join(depot_tools_path, 'pylint-1.5')
21
George Engelbrechtb3f23ac2021-08-25 13:06:17 -060022 for affected in input_api.AffectedFiles(include_deletes=False):
George Engelbrecht1344a582021-08-24 09:56:25 -060023 affected_str = str(affected)
24 if affected_str.endswith(".py"):
25 if affected_str in UNLINTABLE_FILES:
26 continue
Madeleine Hardt93db4832021-08-13 17:48:24 +000027 try:
28 input_api.subprocess.check_output(
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060029 [pylint_path, '--rcfile', 'pylintrc',
George Engelbrecht1344a582021-08-24 09:56:25 -060030 '%s' % (affected_str)])
Madeleine Hardt93db4832021-08-13 17:48:24 +000031 except CalledProcessError as error:
32 pylint_errors.append("%s" % error)
33 if pylint_errors:
34 pylint_errors = [
35 output_api.PresubmitError((
36 'Please fix pylint errors or add \'#pylint: disable=your-error\' in the source code '
37 'at the end of the relevant line:\n\n%s') %
38 "\n".join(pylint_errors))
39 ]
40 return pylint_errors
recipe-roller4202bc62020-09-17 17:38:22 -070041
Lann Martin46361ee2019-03-14 15:30:47 -060042
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000043def FormatCheck(input_api, output_api):
recipe-roller4202bc62020-09-17 17:38:22 -070044 bad_format = False
45 cmd = [
46 '-C',
47 input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run',
48 '--presubmit', '--python', '--no-clang-format', '--diff'
49 ]
Madeleine Hardt93db4832021-08-13 17:48:24 +000050 _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True)
recipe-roller4202bc62020-09-17 17:38:22 -070051 for line in out.splitlines():
52 if line.startswith('--- ') or line.startswith('+++ '):
53 if not ' recipes.py\t' in line:
54 bad_format = True
55 break
56
57 if bad_format:
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000058 return input_api.canned_checks.CheckPatchFormatted(
recipe-roller4202bc62020-09-17 17:38:22 -070059 input_api, output_api, check_python=True, check_clang_format=False,
60 result_factory=output_api.PresubmitError)
Andrew Lamb9b2d0862019-04-03 16:38:52 -060061
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000062
63def CommitChecks(input_api, output_api):
64 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
65 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
66 file_filter=file_filter)
67
68 # recipes.py test run
69 results += input_api.RunTests([
70 input_api.Command(
71 name='recipes test',
72 cmd=[input_api.python_executable, 'recipes.py', 'test', 'run'],
73 kwargs={},
74 message=output_api.PresubmitError,
75 )
76 ])
77 results += PylintCheck(input_api, output_api)
78 # Python formatting issues are errors, but we need to ignore recipes.py, which
79 # we do not control.
80 fmt_results = FormatCheck(input_api, output_api)
81 if fmt_results:
82 results += fmt_results
Lann Martin46361ee2019-03-14 15:30:47 -060083 return results
84
85
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000086def UploadChecks(input_api, output_api):
87 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
88 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
89 file_filter=file_filter)
Navil Perez6d4fa162022-04-22 21:01:25 +000090 results += PylintCheck(input_api, output_api)
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000091 # Python formatting issues are errors, but we need to ignore recipes.py, which
92 # we do not control.
93 fmt_results = FormatCheck(input_api, output_api)
94 if fmt_results:
95 results += fmt_results
96 return results
97
98
99CheckChangeOnUpload = UploadChecks
100CheckChangeOnCommit = CommitChecks