blob: 7307f827bb1de9de6444f910914e63f9c54f0acc [file] [log] [blame]
Mike Frysinger9dfd69f2020-12-01 13:27:56 -05001#!/usr/bin/env python3
Mike Frysinger4f42a972019-06-12 17:42:43 -04002# 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 Mak57cb4282023-03-30 05:06:01 +000016"""Wrapper to run linters and pytest with the right settings."""
Mike Frysinger4f42a972019-06-12 17:42:43 -040017
Gavin Makea2e3302023-03-11 06:46:20 +000018import os
19import subprocess
Mike Frysinger4f42a972019-06-12 17:42:43 -040020import sys
Mike Frysinger64477332023-08-21 21:20:32 -040021
Gavin Mak3ed84462023-01-25 21:19:54 +000022import pytest
Mike Frysinger4f42a972019-06-12 17:42:43 -040023
Gavin Makea2e3302023-03-11 06:46:20 +000024
Gavin Mak57cb4282023-03-30 05:06:01 +000025ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
26
27
Gavin Makea2e3302023-03-11 06:46:20 +000028def run_black():
Gavin Mak57cb4282023-03-30 05:06:01 +000029 """Returns the exit code from black."""
Mike Frysingerb8615112023-09-01 13:58:46 -040030 # 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 Makea2e3302023-03-11 06:46:20 +000037 return subprocess.run(
Mike Frysingerb8615112023-09-01 13:58:46 -040038 [sys.executable, "-m", "black", "--check", ROOT_DIR] + extra_programs,
39 check=False,
Gavin Mak57cb4282023-03-30 05:06:01 +000040 ).returncode
41
42
43def run_flake8():
44 """Returns the exit code from flake8."""
45 return subprocess.run(
46 [sys.executable, "-m", "flake8", ROOT_DIR], check=False
Gavin Makea2e3302023-03-11 06:46:20 +000047 ).returncode
48
49
Mike Frysinger64477332023-08-21 21:20:32 -040050def 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 Makea2e3302023-03-11 06:46:20 +000057def main(argv):
58 """The main entry."""
Gavin Mak57cb4282023-03-30 05:06:01 +000059 checks = (
60 lambda: pytest.main(argv),
61 run_black,
62 run_flake8,
Mike Frysinger64477332023-08-21 21:20:32 -040063 run_isort,
Gavin Mak57cb4282023-03-30 05:06:01 +000064 )
65 return 0 if all(not c() for c in checks) else 1
Gavin Makea2e3302023-03-11 06:46:20 +000066
67
68if __name__ == "__main__":
69 sys.exit(main(sys.argv[1:]))