aboutsummaryrefslogtreecommitdiff
path: root/toolchain_utils_githooks/check-presubmit.py
diff options
context:
space:
mode:
Diffstat (limited to 'toolchain_utils_githooks/check-presubmit.py')
-rwxr-xr-xtoolchain_utils_githooks/check-presubmit.py55
1 files changed, 32 insertions, 23 deletions
diff --git a/toolchain_utils_githooks/check-presubmit.py b/toolchain_utils_githooks/check-presubmit.py
index 485737d5..0d2f3b31 100755
--- a/toolchain_utils_githooks/check-presubmit.py
+++ b/toolchain_utils_githooks/check-presubmit.py
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
#
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
@@ -23,6 +22,12 @@ import traceback
import typing as t
+# This was originally had many packages in it (notably scipy)
+# but due to changes in how scipy is built, we can no longer install
+# it in the chroot. See b/284489250
+PIP_DEPENDENCIES = ("numpy",)
+
+
def run_command_unchecked(
command: t.List[str], cwd: str, env: t.Dict[str, str] = None
) -> t.Tuple[int, str]:
@@ -49,7 +54,8 @@ def has_executable_on_path(exe: str) -> bool:
def escape_command(command: t.Iterable[str]) -> str:
"""Returns a human-readable and copy-pastable shell command.
- Only intended for use in output to users. shell=True is strongly discouraged.
+ Only intended for use in output to users. shell=True is strongly
+ discouraged.
"""
return " ".join(shlex.quote(x) for x in command)
@@ -172,9 +178,9 @@ def check_black(
toolchain_utils_root: str, black: Path, python_files: t.Iterable[str]
) -> CheckResult:
"""Subchecker of check_py_format. Checks python file formats with black"""
- # Folks have been bitten by accidentally using multiple formatter versions in
- # the past. This is an issue, since newer versions of black may format things
- # differently. Make the version obvious.
+ # Folks have been bitten by accidentally using multiple formatter
+ # versions in the past. This is an issue, since newer versions of
+ # black may format things differently. Make the version obvious.
command = [black, "--version"]
exit_code, stdout_and_stderr = run_command_unchecked(
command, cwd=toolchain_utils_root
@@ -182,7 +188,8 @@ def check_black(
if exit_code:
return CheckResult(
ok=False,
- output=f"Failed getting black version; stdstreams: {stdout_and_stderr}",
+ output="Failed getting black version; "
+ f"stdstreams: {stdout_and_stderr}",
autofix_commands=[],
)
@@ -235,15 +242,16 @@ def check_black(
err_str = "\n".join(errors)
return CheckResult(
ok=False,
- output=f"Using {black_version!r} had the following errors:\n{err_str}",
+ output=f"Using {black_version!r} had the following errors:\n"
+ f"{err_str}",
autofix_commands=[],
)
autofix = black_invocation + bad_files
return CheckResult(
ok=False,
- output=f"Using {black_version!r}, these file(s) have formatting errors: "
- f"{bad_files}",
+ output=f"Using {black_version!r}, these file(s) have formatting "
+ f"errors: {bad_files}",
autofix_commands=[autofix],
)
@@ -297,7 +305,7 @@ def check_py_format(
thread_pool: multiprocessing.pool.ThreadPool,
files: t.Iterable[str],
) -> t.List[CheckResult]:
- """Runs yapf on files to check for style bugs. Also checks for #!s."""
+ """Runs black on files to check for style bugs. Also checks for #!s."""
black = "black"
if not has_executable_on_path(black):
return CheckResult(
@@ -412,11 +420,10 @@ def check_cros_lint(
["golint", "-set_exit_status"] + go_files
)
- complaint = "\n".join(
- (
- "WARNING: go linting disabled. golint is not on your $PATH.",
- "Please either enter a chroot, or install go locally. Continuing.",
- )
+ complaint = (
+ "WARNING: go linting disabled. golint is not on your $PATH.\n"
+ "Please either enter a chroot, or install go locally. "
+ "Continuing."
)
return CheckResult(
ok=True,
@@ -426,13 +433,11 @@ def check_cros_lint(
tasks.append(("golint", thread_pool.apply_async(run_golint)))
- complaint = "\n".join(
- (
- "WARNING: No ChromeOS checkout detected, and no viable CrOS tree",
- "found; falling back to linting only python and go. If you have a",
- "ChromeOS checkout, please either develop from inside of the source",
- "tree, or set $CHROMEOS_ROOT_DIRECTORY to the root of it.",
- )
+ complaint = (
+ "WARNING: No ChromeOS checkout detected, and no viable CrOS tree\n"
+ "found; falling back to linting only python and go. If you have a\n"
+ "ChromeOS checkout, please either develop from inside of the source\n"
+ "tree, or set $CHROMEOS_ROOT_DIRECTORY to the root of it."
)
results = [(name, get_check_result_or_catch(task)) for name, task in tasks]
@@ -689,11 +694,15 @@ def maybe_reexec_inside_chroot(autofix: bool, files: t.List[str]) -> None:
def ensure_pip_deps_installed() -> None:
+ if not PIP_DEPENDENCIES:
+ # No need to install pip if we don't have any deps.
+ return
+
if not has_executable_on_path("pip"):
print("Autoinstalling `pip`...")
subprocess.check_call(["sudo", "emerge", "dev-python/pip"])
- for package in ("scipy", "yapf"):
+ for package in PIP_DEPENDENCIES:
exit_code = subprocess.call(
["python3", "-c", f"import {package}"],
stdout=subprocess.DEVNULL,