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 | |
| 8 | |
| 9 | def 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-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 28 | |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 29 | |
| 30 | def CommonChecks(input_api, output_api): |
Josip Sokcevic | 52f5436 | 2021-07-13 07:30:36 -0700 | [diff] [blame] | 31 | 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 Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 34 | |
| 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 Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame^] | 44 | results += PylintCheck(input_api, output_api) |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 45 | |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 46 | # 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 Hardt | 93db483 | 2021-08-13 17:48:24 +0000 | [diff] [blame^] | 54 | _, out = git_cl.RunGitWithCode(cmd, suppress_stderr=True) |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 55 | 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 Lamb | 9b2d086 | 2019-04-03 16:38:52 -0600 | [diff] [blame] | 65 | |
Lann Martin | 46361ee | 2019-03-14 15:30:47 -0600 | [diff] [blame] | 66 | return results |
| 67 | |
| 68 | |
| 69 | CheckChangeOnUpload = CommonChecks |
| 70 | CheckChangeOnCommit = CommonChecks |