Greg Edelston | ff55089 | 2022-09-27 17:29:09 -0600 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors. |
David Burger | c0effc2 | 2020-03-04 08:27:07 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 4 | |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 5 | import git_cl |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 6 | from subprocess2 import CalledProcessError |
| 7 | |
Benjamin Shai | b79092c | 2022-11-07 16:41:26 +0000 | [diff] [blame^] | 8 | USE_PYTHON3 = True |
| 9 | |
George Engelbrecht | 1344a58 | 2021-08-24 09:56:25 -0600 | [diff] [blame] | 10 | UNLINTABLE_FILES = set(['recipes.py']) |
| 11 | |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 12 | |
| 13 | def PylintCheck(input_api, output_api): |
| 14 | """Run pylint checks for modified files.""" |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 15 | |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 16 | pylint_errors = [] |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 17 | |
Greg Edelston | ff55089 | 2022-09-27 17:29:09 -0600 | [diff] [blame] | 18 | # Find pylint (currently v2.7). |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 19 | 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 Edelston | ff55089 | 2022-09-27 17:29:09 -0600 | [diff] [blame] | 22 | pylint_path = input_api.os_path.join(depot_tools_path, 'pylint-2.7') |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 23 | |
George Engelbrecht | b3f23ac | 2021-08-25 13:06:17 -0600 | [diff] [blame] | 24 | for affected in input_api.AffectedFiles(include_deletes=False): |
George Engelbrecht | 1344a58 | 2021-08-24 09:56:25 -0600 | [diff] [blame] | 25 | affected_str = str(affected) |
| 26 | if affected_str.endswith(".py"): |
| 27 | if affected_str in UNLINTABLE_FILES: |
| 28 | continue |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 29 | try: |
| 30 | input_api.subprocess.check_output( |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 31 | [pylint_path, '--rcfile', 'pylintrc', |
George Engelbrecht | 1344a58 | 2021-08-24 09:56:25 -0600 | [diff] [blame] | 32 | '%s' % (affected_str)]) |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 33 | 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-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 43 | |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 44 | |
Dhanya Ganesh | 07c1aa4 | 2022-04-20 17:53:57 +0000 | [diff] [blame] | 45 | def FormatCheck(input_api, output_api): |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 46 | 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 Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 52 | _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True) |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 53 | 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 Ganesh | 07c1aa4 | 2022-04-20 17:53:57 +0000 | [diff] [blame] | 60 | return input_api.canned_checks.CheckPatchFormatted( |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 61 | input_api, output_api, check_python=True, check_clang_format=False, |
| 62 | result_factory=output_api.PresubmitError) |
Greg Edelston | ff55089 | 2022-09-27 17:29:09 -0600 | [diff] [blame] | 63 | return None |
Andrew Lamb | 9b2d086 | 2019-04-03 16:38:52 -0600 | [diff] [blame] | 64 | |
Dhanya Ganesh | 07c1aa4 | 2022-04-20 17:53:57 +0000 | [diff] [blame] | 65 | |
| 66 | def 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 Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 86 | return results |
| 87 | |
| 88 | |
Dhanya Ganesh | 07c1aa4 | 2022-04-20 17:53:57 +0000 | [diff] [blame] | 89 | def 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 Perez | 6d4fa16 | 2022-04-22 21:01:25 +0000 | [diff] [blame] | 93 | results += PylintCheck(input_api, output_api) |
Dhanya Ganesh | 07c1aa4 | 2022-04-20 17:53:57 +0000 | [diff] [blame] | 94 | # 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 | |
| 102 | CheckChangeOnUpload = UploadChecks |
| 103 | CheckChangeOnCommit = CommitChecks |