Mike Frysinger | 9dfd69f | 2020-12-01 13:27:56 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 2 | # Copyright 2019 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Gavin Mak | 57cb428 | 2023-03-30 05:06:01 +0000 | [diff] [blame] | 16 | """Wrapper to run linters and pytest with the right settings.""" |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 17 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 18 | import os |
| 19 | import subprocess |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 20 | import sys |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 21 | |
Gavin Mak | 3ed8446 | 2023-01-25 21:19:54 +0000 | [diff] [blame] | 22 | import pytest |
Mike Frysinger | 4f42a97 | 2019-06-12 17:42:43 -0400 | [diff] [blame] | 23 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 24 | |
Gavin Mak | 57cb428 | 2023-03-30 05:06:01 +0000 | [diff] [blame] | 25 | ROOT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 26 | |
| 27 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 28 | def run_black(): |
Gavin Mak | 57cb428 | 2023-03-30 05:06:01 +0000 | [diff] [blame] | 29 | """Returns the exit code from black.""" |
Mike Frysinger | b861511 | 2023-09-01 13:58:46 -0400 | [diff] [blame] | 30 | # Black by default only matches .py files. We have to list standalone |
| 31 | # scripts manually. |
| 32 | extra_programs = [ |
| 33 | "repo", |
| 34 | "run_tests", |
| 35 | "release/update-manpages", |
| 36 | ] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 37 | return subprocess.run( |
Mike Frysinger | b861511 | 2023-09-01 13:58:46 -0400 | [diff] [blame] | 38 | [sys.executable, "-m", "black", "--check", ROOT_DIR] + extra_programs, |
| 39 | check=False, |
Gavin Mak | 57cb428 | 2023-03-30 05:06:01 +0000 | [diff] [blame] | 40 | ).returncode |
| 41 | |
| 42 | |
| 43 | def run_flake8(): |
| 44 | """Returns the exit code from flake8.""" |
| 45 | return subprocess.run( |
| 46 | [sys.executable, "-m", "flake8", ROOT_DIR], check=False |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 47 | ).returncode |
| 48 | |
| 49 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 50 | def run_isort(): |
| 51 | """Returns the exit code from isort.""" |
| 52 | return subprocess.run( |
| 53 | [sys.executable, "-m", "isort", "--check", ROOT_DIR], check=False |
| 54 | ).returncode |
| 55 | |
| 56 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 57 | def main(argv): |
| 58 | """The main entry.""" |
Gavin Mak | 57cb428 | 2023-03-30 05:06:01 +0000 | [diff] [blame] | 59 | checks = ( |
| 60 | lambda: pytest.main(argv), |
| 61 | run_black, |
| 62 | run_flake8, |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 63 | run_isort, |
Gavin Mak | 57cb428 | 2023-03-30 05:06:01 +0000 | [diff] [blame] | 64 | ) |
| 65 | return 0 if all(not c() for c in checks) else 1 |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | sys.exit(main(sys.argv[1:])) |