Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
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 | |
George Engelbrecht | 1344a58 | 2021-08-24 09:56:25 -0600 | [diff] [blame] | 8 | UNLINTABLE_FILES = set(['recipes.py']) |
| 9 | |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 10 | |
| 11 | def PylintCheck(input_api, output_api): |
| 12 | """Run pylint checks for modified files.""" |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 13 | |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 14 | pylint_errors = [] |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 15 | |
| 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 Engelbrecht | b3f23ac | 2021-08-25 13:06:17 -0600 | [diff] [blame] | 22 | for affected in input_api.AffectedFiles(include_deletes=False): |
George Engelbrecht | 1344a58 | 2021-08-24 09:56:25 -0600 | [diff] [blame] | 23 | affected_str = str(affected) |
| 24 | if affected_str.endswith(".py"): |
| 25 | if affected_str in UNLINTABLE_FILES: |
| 26 | continue |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 27 | try: |
| 28 | input_api.subprocess.check_output( |
George Engelbrecht | 4a0d0fe | 2021-08-25 13:53:27 -0600 | [diff] [blame] | 29 | [pylint_path, '--rcfile', 'pylintrc', |
George Engelbrecht | 1344a58 | 2021-08-24 09:56:25 -0600 | [diff] [blame] | 30 | '%s' % (affected_str)]) |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 31 | 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-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 41 | |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 42 | |
| 43 | def CommonChecks(input_api, output_api): |
Josip Sokcevic | 52f5436 | 2021-07-13 07:30:36 -0700 | [diff] [blame] | 44 | file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg' |
| 45 | results = input_api.canned_checks.CheckJsonParses(input_api, output_api, |
| 46 | file_filter=file_filter) |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 47 | |
| 48 | # recipes.py test run |
| 49 | results += input_api.RunTests([ |
| 50 | input_api.Command( |
| 51 | name='recipes test', |
| 52 | cmd=[input_api.python_executable, 'recipes.py', 'test', 'run'], |
| 53 | kwargs={}, |
| 54 | message=output_api.PresubmitError, |
| 55 | ) |
| 56 | ]) |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 57 | results += PylintCheck(input_api, output_api) |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 58 | |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 59 | # Python formatting issues are errors, but we need to ignore recipes.py, which |
| 60 | # we do not control. |
| 61 | bad_format = False |
| 62 | cmd = [ |
| 63 | '-C', |
| 64 | input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run', |
| 65 | '--presubmit', '--python', '--no-clang-format', '--diff' |
| 66 | ] |
Madeleine Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame] | 67 | _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True) |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 68 | for line in out.splitlines(): |
| 69 | if line.startswith('--- ') or line.startswith('+++ '): |
| 70 | if not ' recipes.py\t' in line: |
| 71 | bad_format = True |
| 72 | break |
| 73 | |
| 74 | if bad_format: |
| 75 | results += input_api.canned_checks.CheckPatchFormatted( |
| 76 | input_api, output_api, check_python=True, check_clang_format=False, |
| 77 | result_factory=output_api.PresubmitError) |
Andrew Lamb | 9b2d086 | 2019-04-03 16:38:52 -0600 | [diff] [blame] | 78 | |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 79 | return results |
| 80 | |
| 81 | |
| 82 | CheckChangeOnUpload = CommonChecks |
| 83 | CheckChangeOnCommit = CommonChecks |