diff options
author | George Burgess IV <gbiv@google.com> | 2019-01-15 13:10:07 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-01-18 04:01:37 -0800 |
commit | ea973ec56fa436a801937111a6bd7218d1a1be1d (patch) | |
tree | b2afab14c61d88178db2fe40dc97abe044bcff5b /toolchain_utils_githooks | |
parent | 56620aa44e75db5710896fedcf53b4ea51606612 (diff) | |
download | toolchain-utils-ea973ec56fa436a801937111a6bd7218d1a1be1d.tar.gz |
git-hooks: support linting outside of the source tree
We appear to have workflows and committers that prefer to `push` from
outside of the CrOS source tree.
It's relatively straightforward to have fallbacks to support that, so I
don't see why not.
If you want to interact with this repository from outside of a CrOS
source tree, you now have two options:
- If you have a source tree somewhere on your machine, you can set
CHROMEOS_ROOT_DIRECTORY to that path. If `check-lint` detects a usable
`cros` in there, it'll function as usual.
- If you don't have a source tree on your machine, `check-lint` will
fall back to pylint-only linting (with a warning). This isn't ideal
(we miss shell linting, as well as a few other nits in Python,
somehow), but should get us most of the way to happiness.
This also includes a refactor of `test` to `[[`
BUG=chromium:918755
TEST=Ran check-lint on some python files in a checkout outside and
inside of the source tree
Change-Id: I101c4c8b142f0c8f78819779adcfd0b3b0e1d614
Reviewed-on: https://chromium-review.googlesource.com/1412682
Commit-Ready: George Burgess <gbiv@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Caroline Tice <cmtice@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Diffstat (limited to 'toolchain_utils_githooks')
-rwxr-xr-x | toolchain_utils_githooks/check-lint | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/toolchain_utils_githooks/check-lint b/toolchain_utils_githooks/check-lint index 5b9ade37..a9345a7e 100755 --- a/toolchain_utils_githooks/check-lint +++ b/toolchain_utils_githooks/check-lint @@ -1,11 +1,11 @@ -#!/bin/bash -u +#!/bin/bash -ue # Copyright 2019 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. # # This script runs `cros lint` on everything it's handed. -if test $# -eq 0; then +if [[ $# -eq 0 ]]; then echo "No files were given to lint." >&2 echo "Usage: $0 file1 file2 ..." >&2 exit 1 @@ -19,4 +19,63 @@ if ! type "${cros}" >&/dev/null; then exit 1 fi -"${cros}" lint -- $@ +lint_args=( "$@" ) + +# Trys to lint our sources. If `cros` tooling isn't properly found, returns. If +# anything else happens, this will exit the script with the exit code of +# `cros`. +try_lint() { + local output last_exit_code cros_binary + + cros_binary="$1" + + set +e + output="$("${cros_binary}" lint -- "${lint_args[@]}" 2>&1)" + last_exit_code="$?" + set -e + + # `cros` exits with 127 specifically if it failed due to not finding a Chrome + # OS checkout. + if [[ "${last_exit_code}" -ne 127 ]]; then + if [[ -n "${output}" ]]; then + echo "${output}" + fi + exit "${last_exit_code}" + fi +} + +try_lint "${cros}" + +# If the user's given us a root directory to fall back on, try that +if [[ -n "${CHROMEOS_ROOT_DIRECTORY:-}" ]]; then + user_cros="${CHROMEOS_ROOT_DIRECTORY}/chromite/bin/cros" + if [[ -x "${user_cros}" ]]; then + try_lint "${user_cros}" + fi +fi + +# So, `cros` outside of the chroot defers to other tools inside of Chromite. If +# `cros` couldn't find the real `cros` tool, we fall back to pylint on each +# Python file. It appears that `cros` uses depot_tools' pylint configuration, so +# this should get us most of the way there, and is probably the best we can +# reasonably expect to do for users who want to develop without the source +# tree. +echo "WARNING: No Chrome OS checkout detected, and no viable CrOS tree" >&2 +echo "found; falling back to python-only linting. If you have a Chrome OS" >&2 +echo "checkout, please either develop from inside of the source tree, or" >&2 +echo "set \$CHROMEOS_ROOT_DIRECTORY to the root of it." >&2 + +python_files=() +for file in "$@"; do + if [[ "${file}" == *.py ]]; then + python_files+=( "${file}" ) + fi +done + +if [[ "${#python_files[@]}" -eq 0 ]]; then + exit 0 +fi + +# We saw `cros` above, so assume that `pylint` is in our PATH (depot_tools +# packages it, and provides a reasonable default config). +pylint "${python_files[@]}" |