| #!/usr/bin/env python3 |
| # Copyright (c) 2021 The Chromium Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import sys |
| |
| USE_PYTHON3 = True |
| |
| |
| def CommonChecks(input_api, output_api, pylint_only_changed=True): |
| results = [] |
| |
| # Run Pylint over the files in the directory. |
| if pylint_only_changed: |
| # Get relative paths (to this PRESUBMIT.py) for all changed files |
| changed_files = input_api.change.AbsoluteLocalPaths() |
| changed_py_files = filter(lambda f: f.endswith('.py'), changed_files) |
| presubmit_base_path = input_api.PresubmitLocalPath() |
| pylint_files_to_check = [ |
| input_api.os_path.relpath(cf, presubmit_base_path) |
| for cf in changed_py_files |
| ] |
| else: |
| pylint_files_to_check = None |
| |
| # GetPylint runs on all files if an empty list is provided for |
| # `files_to_check`, but no files got changed |
| if pylint_files_to_check is None or len(pylint_files_to_check) > 0: |
| results.extend( |
| input_api.RunTests( |
| input_api.canned_checks.GetPylint( |
| input_api, |
| output_api, |
| files_to_check=pylint_files_to_check, |
| version='2.6'))) |
| |
| # Run Python unittests. |
| results.extend( |
| input_api.canned_checks.RunUnitTestsInDirectory( |
| input_api, output_api, '.', [r'^.+_test\.py$'], run_on_python2=False)) |
| |
| # Check for license header |
| results.extend(input_api.canned_checks.CheckLicense(input_api, output_api)) |
| |
| return results |
| |
| |
| def CheckChangeOnUpload(input_api, output_api): |
| return CommonChecks(input_api, output_api, pylint_only_changed=True) |
| |
| |
| def CheckChangeOnCommit(input_api, output_api): |
| return CommonChecks(input_api, output_api, pylint_only_changed=False) |