summaryrefslogtreecommitdiff
path: root/zephyr
diff options
context:
space:
mode:
authorJack Rosenthal <jrosenth@chromium.org>2021-09-30 15:31:00 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-30 23:03:45 +0000
commit6541eebcf6b4424076466d10b147a9302d1856f7 (patch)
tree1811e6088e071735ec3259442d9daedd23e908b2 /zephyr
parent6cc74040193c715842941ea3c21ce6a7274b9329 (diff)
downloadchrome-ec-6541eebcf6b4424076466d10b147a9302d1856f7.tar.gz
zephyr: zmake: Add pre-upload formatter suggestions
Zmake requires three (fairly standard) formatting and linting tools to preform validation on the source. At present, we run these as a part of run_tests.sh and report it during the commit queue run, which can be inconvenient to find out a CL needs trivial formatting changes after you've already sent it to the CQ. Running these formatting tools in pre-sumbit isn't exactly trivial: some people run repo upload from outside of the chroot and we won't necessarily have the formatting/linting tools installed, or if we do, they could be different versions that make slightly different suggestions. Therefore, we wrap the pre-upload logic like so: - If no zmake changes were made, simply exit successfully. - If we did make zmake changes, and we happen to be running in the chroot, run the formatting tools and suggest fixes if necessary. - Finally, if there's zmake changes but we're out of the chroot, report a warning and fail the pre-upload checks. BUG=b:192389533 BRANCH=none TEST=(in chroot) try to upload a CL with bad formatting, get yelled at during pre-upload TEST=(out of chroot) upload a CL with zmake changes, get told to run in the chroot TEST=(out of chroot) try and upload without zmake changes, didn't notice any pre-upload warnings Signed-off-by: Jack Rosenthal <jrosenth@chromium.org> Change-Id: If307b9ce9f968b5b09ad8aeb27f4b9621e813036 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3198244 Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
Diffstat (limited to 'zephyr')
-rwxr-xr-xzephyr/zmake/pre-upload.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/zephyr/zmake/pre-upload.sh b/zephyr/zmake/pre-upload.sh
new file mode 100755
index 0000000000..02bef2a6b0
--- /dev/null
+++ b/zephyr/zmake/pre-upload.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+# Copyright 2021 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.
+set -e
+
+AFFECTED_FILES=()
+
+for path in "$@"; do
+ case "${path}" in
+ *zephyr/zmake/*.py )
+ AFFECTED_FILES+=("${path}")
+ ;;
+ esac
+done
+
+if [ "${#AFFECTED_FILES}" -eq 0 ]; then
+ # No zmake changes made, do nothing.
+ exit 0
+fi
+
+EXIT_STATUS=0
+
+# Wraps a black/isort command and reports how to fix it.
+wrap_fix_msg() {
+ local cmd="$1"
+ shift
+
+ if ! "${cmd}" "$@"; then
+ cat <<EOF >&2
+Looks like zmake's ${cmd} formatter detected that formatting changes
+need applied. Fix by running this command from the zephyr/zmake
+directory and amending your changes:
+
+ ${cmd} .
+
+EOF
+ EXIT_STATUS=1
+ fi
+}
+
+# We only want to run black, flake8, and isort inside of the chroot,
+# as these are formatting tools which we want the specific versions
+# provided by the chroot.
+if [ -f /etc/cros_chroot_version ]; then
+ cd "$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
+ wrap_fix_msg black --check --diff "${AFFECTED_FILES[@]}"
+ wrap_fix_msg isort --check "${AFFECTED_FILES[@]}"
+ flake8 "${AFFECTED_FILES[@]}" || EXIT_STATUS=1
+ exit "${EXIT_STATUS}"
+else
+ cat <<EOF >&2
+WARNING: It looks like you made zmake changes, but I'm running outside
+of the chroot, and can't run zmake's auto-formatters.
+
+It is recommended that you run repo upload from inside the chroot, or
+you may see formatting errors during your CQ run.
+EOF
+ exit 1
+fi