Skip to content

Commit

Permalink
Create script for checking lint locally (iree-org#7733)
Browse files Browse the repository at this point in the history
Add a simple local bash script for locally checking all the lint we
check on presubmit. This is far from perfect, but I think better than
nothing. Problems it has:

1. It edits the working directory and index
2. I have no idea how cross-platform it is
3. It assumes that script shebang lines will be enough to find the
   appropriate executable, which may frequently not be true (especially
   for python)
  • Loading branch information
GMNGeoffrey authored Nov 24, 2021
1 parent 64bda35 commit 916c6f9
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Keep this in sync with scripts/lint.sh

name: Lint

on: [pull_request]
Expand Down
122 changes: 122 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash

# Copyright 2021 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Runs all the lint checks that we run on GitHub locally. Skips checks if the
# relevant tool doesn't exist.

# Keep this in sync with .github/workflows/lint.yml

# WARNING: this script *makes changes* to the working directory and the index.

set -uo pipefail

FINAL_RET=0
LATEST_RET=0

function update_ret() {
LATEST_RET="$?"
if [[ "${LATEST_RET}" -gt "${FINAL_RET}" ]]; then
FINAL_RET="${LATEST_RET}"
fi
}

# Update the exit code after every command
function enable_update_ret() {
trap update_ret DEBUG
}

function disable_update_ret() {
trap - DEBUG
}

function exists() {
command -v "${1}" > /dev/null
}


echo "***** Uncommitted changes *****"
git add -A
git diff HEAD --exit-code

if [[ $? -ne 0 ]]; then
echo "Found uncomitted changes in working directory. This script requires" \
"all changes to be committed. All changes have been added to the" \
"index. Please commit or clean all changes and try again."
exit 1
fi

enable_update_ret

echo "***** Bazel -> CMake *****"
./build_tools/bazel_to_cmake/bazel_to_cmake.py
./build_tools/bazel_to_cmake/bazel_to_cmake.py --root_dir=integrations/tensorflow/e2e
git add -A
git diff HEAD --exit-code
trap - DEBUG

echo "***** buildifier *****"
# Don't fail script if condition is false
disable_update_ret
if exists buildifier; then
enable_update_ret
./scripts/run_buildifier.sh
git diff --exit-code
else
enable_update_ret
echo "'buildifier' not found. Skipping check"
fi

echo "***** yapf *****"
# Don't fail script if condition is false
disable_update_ret
if exists yapf > /dev/null; then
enable_update_ret
git diff -U0 main | ./third_party/format_diff/format_diff.py yapf -i
else
enable_update_ret
echo "'yapf' not found. Skipping check"
fi

echo "***** pytype *****"
# Don't fail script if condition is false
disable_update_ret
if exists pytype; then
enable_update_ret
./build_tools/pytype/check_diff.sh
else
enable_update_ret
echo "'pytype' not found. Skipping check"
fi

echo "***** clang-format *****"
# Don't fail script if condition is false
disable_update_ret
if exists git-clang-format; then
enable_update_ret
git-clang-format --style=file
git diff --exit-code
else
enable_update_ret
echo "'git-clang-format' not found. Skipping check"
fi

echo "***** submodules *****"
./scripts/git/submodule_versions.py check

echo "***** tabs *****"
./scripts/check_tabs.sh

echo "***** yamllint *****"
echo "'yamllint' check not yet implemented. Skipping check"

if [[ "${FINAL_RET}" -ne 0 ]]; then
echo "Encountered failures. Check error messages and changes to the working" \
"directory and git index (which may contain fixes) and try again."
fi

exit "${FINAL_RET}"
Empty file modified third_party/format_diff/format_diff.py
100644 → 100755
Empty file.

0 comments on commit 916c6f9

Please sign in to comment.