blob: 742a222b6df8435b8527fc30edbed702f83d94f0 [file] [log] [blame] [edit]
#!/usr/bin/env python3
# Copyright 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Wrapper to run analyzers & tests with the right settings."""
import functools
import os
from pathlib import Path
import shlex
import subprocess
import sys
from typing import List
ROOT_DIR = Path(__file__).resolve().parent
def log_cmd(cmd: str, argv: list[str]) -> None:
"""Log a debug message to make history easier to track."""
print("+", cmd, shlex.join(str(x) for x in argv), file=sys.stderr)
@functools.lru_cache()
def is_ci() -> bool:
"""Whether we're running in our CI system."""
return os.getenv("LUCI_CQ") == "yes"
def run_pytest(argv: List[str]) -> int:
"""Returns the exit code from pytest."""
if is_ci():
argv = ["-m", "not skip_cq"] + argv
log_cmd("pytest", argv)
return subprocess.run(
[sys.executable, "-m", "pytest"] + argv,
check=False,
cwd=ROOT_DIR,
).returncode
def run_black() -> int:
"""Returns the exit code from black."""
argv = ["--version"]
log_cmd("black", argv)
subprocess.run(
[sys.executable, "-m", "black"] + argv,
check=True,
cwd=ROOT_DIR,
)
# Black by default only matches .py files. We have to list standalone
# scripts manually.
extra_programs = [
"run_tests",
"tools/cpplint.py-update",
]
argv = ["--diff", "--check", ROOT_DIR] + extra_programs
log_cmd("black", argv)
return subprocess.run(
[sys.executable, "-m", "black"] + argv,
check=False,
cwd=ROOT_DIR,
).returncode
def run_pylint() -> int:
"""Returns the exit code from pylint."""
argv = ["--version"]
log_cmd("pylint", argv)
subprocess.run(
[sys.executable, "-m", "pylint"] + argv,
check=True,
cwd=ROOT_DIR,
)
argv = [
"--rc-file=tools/pylintrc",
"pre-upload.py",
"run_tests",
"rh/*.py",
"tools/*.py",
"tools/cpplint.py-update",
]
log_cmd("pylint", argv)
return subprocess.run(
[sys.executable, "-m", "pylint"] + argv,
check=False,
cwd=ROOT_DIR,
).returncode
def main(argv):
"""The main entry."""
checks = (
functools.partial(run_pytest, argv),
run_black,
run_pylint,
)
# Run all the tests all the time to get full feedback. Don't exit on the
# first error as that makes it more difficult to iterate in the CQ.
return 1 if sum(c() for c in checks) else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))