crosvm/tools/health-check
Dennis Kempin ba4adc0efb Add python presubmit checks
This change adds python type and formatting checks and
consolidates code health checks in ./tools/health-check.

Dealing with relative imports in python is tricky, so
we are making ./tools/impl a proper package with no
directly executable files.

Some of the bash shorthands in ./tools had to be converted
to python for this.

To make the new checks pass, we run the formatter and fix
some mypy type checks.

TEST=./tools/health-check
BUG=b:218559722,b:219965702

Change-Id: Ie18d3d6dd2f5a033141e167a6e1aa762791941d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3558592
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2022-04-15 19:22:53 +00:00

47 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python3
# Copyright 2022 The Chromium OS 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 doctest
import importlib
from pathlib import Path
import sys
from impl.common import CROSVM_ROOT, run_main, cmd, chdir, parallel, find_scripts
from impl.check_code_hygiene import has_crlf_line_endings
mypy = cmd("mypy --pretty --color-output").env("MYPY_FORCE_COLOR", "1").env("MYPYPATH", "tools")
def check_python():
print("Running python doctests")
for source in Path("tools/impl").glob("*.py"):
lib = importlib.import_module(f"impl.{source.stem}")
doctest.testmod(lib, optionflags=doctest.ELLIPSIS)
print("Type-checking python")
parallel(
mypy("tools/impl"),
*mypy.foreach(find_scripts(Path("tools"), "/usr/bin/env python3")),
).fg()
def check_crlf_line_endings():
crlf_endings = has_crlf_line_endings()
if crlf_endings:
print("Error: Following files have crlf(dos) line encodings")
print(*crlf_endings)
sys.exit(-1)
def main():
chdir(CROSVM_ROOT)
check_python()
check_crlf_line_endings()
cmd("./tools/fmt --check").fg()
cmd("./tools/clippy").fg()
if __name__ == "__main__":
run_main(main)