blob: 9f79acbe32ea98dddf6d6383402c862c138a4f10 [file] [log] [blame]
Greg Edelstonff550892022-09-27 17:29:09 -06001# Copyright 2019 The ChromiumOS Authors.
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
Benjamin Shaib79092c2022-11-07 16:41:26 +00008USE_PYTHON3 = True
9
George Engelbrecht1344a582021-08-24 09:56:25 -060010UNLINTABLE_FILES = set(['recipes.py'])
11
Madeleine Hardt93db4832021-08-13 17:48:24 +000012
13def PylintCheck(input_api, output_api):
14 """Run pylint checks for modified files."""
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060015
Madeleine Hardt93db4832021-08-13 17:48:24 +000016 pylint_errors = []
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060017
Greg Edelstonff550892022-09-27 17:29:09 -060018 # Find pylint (currently v2.7).
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060019 canned_checks_path = input_api.canned_checks.__file__
20 canned_checks_path = input_api.os_path.abspath(canned_checks_path)
21 depot_tools_path = input_api.os_path.dirname(canned_checks_path)
Greg Edelstonff550892022-09-27 17:29:09 -060022 pylint_path = input_api.os_path.join(depot_tools_path, 'pylint-2.7')
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060023
George Engelbrechtb3f23ac2021-08-25 13:06:17 -060024 for affected in input_api.AffectedFiles(include_deletes=False):
George Engelbrecht1344a582021-08-24 09:56:25 -060025 affected_str = str(affected)
26 if affected_str.endswith(".py"):
27 if affected_str in UNLINTABLE_FILES:
28 continue
Madeleine Hardt93db4832021-08-13 17:48:24 +000029 try:
30 input_api.subprocess.check_output(
George Engelbrecht4a0d0fe2021-08-25 13:53:27 -060031 [pylint_path, '--rcfile', 'pylintrc',
George Engelbrecht1344a582021-08-24 09:56:25 -060032 '%s' % (affected_str)])
Madeleine Hardt93db4832021-08-13 17:48:24 +000033 except CalledProcessError as error:
34 pylint_errors.append("%s" % error)
35 if pylint_errors:
36 pylint_errors = [
37 output_api.PresubmitError((
38 'Please fix pylint errors or add \'#pylint: disable=your-error\' in the source code '
39 'at the end of the relevant line:\n\n%s') %
40 "\n".join(pylint_errors))
41 ]
42 return pylint_errors
recipe-roller4202bc62020-09-17 17:38:22 -070043
Lann Martin46361ee2019-03-14 15:30:47 -060044
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000045def FormatCheck(input_api, output_api):
recipe-roller4202bc62020-09-17 17:38:22 -070046 bad_format = False
47 cmd = [
48 '-C',
49 input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run',
50 '--presubmit', '--python', '--no-clang-format', '--diff'
51 ]
Madeleine Hardt93db4832021-08-13 17:48:24 +000052 _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True)
recipe-roller4202bc62020-09-17 17:38:22 -070053 for line in out.splitlines():
54 if line.startswith('--- ') or line.startswith('+++ '):
55 if not ' recipes.py\t' in line:
56 bad_format = True
57 break
58
59 if bad_format:
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000060 return input_api.canned_checks.CheckPatchFormatted(
recipe-roller4202bc62020-09-17 17:38:22 -070061 input_api, output_api, check_python=True, check_clang_format=False,
62 result_factory=output_api.PresubmitError)
Greg Edelstonff550892022-09-27 17:29:09 -060063 return None
Andrew Lamb9b2d0862019-04-03 16:38:52 -060064
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000065
66def CommitChecks(input_api, output_api):
67 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
68 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
69 file_filter=file_filter)
70
71 # recipes.py test run
72 results += input_api.RunTests([
73 input_api.Command(
74 name='recipes test',
75 cmd=[input_api.python_executable, 'recipes.py', 'test', 'run'],
76 kwargs={},
77 message=output_api.PresubmitError,
78 )
79 ])
80 results += PylintCheck(input_api, output_api)
81 # Python formatting issues are errors, but we need to ignore recipes.py, which
82 # we do not control.
83 fmt_results = FormatCheck(input_api, output_api)
84 if fmt_results:
85 results += fmt_results
Lann Martin46361ee2019-03-14 15:30:47 -060086 return results
87
88
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000089def UploadChecks(input_api, output_api):
90 file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
91 results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
92 file_filter=file_filter)
Navil Perez6d4fa162022-04-22 21:01:25 +000093 results += PylintCheck(input_api, output_api)
Dhanya Ganesh07c1aa42022-04-20 17:53:57 +000094 # Python formatting issues are errors, but we need to ignore recipes.py, which
95 # we do not control.
96 fmt_results = FormatCheck(input_api, output_api)
97 if fmt_results:
98 results += fmt_results
99 return results
100
101
102CheckChangeOnUpload = UploadChecks
103CheckChangeOnCommit = CommitChecks