|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Treat undefined variables and non-zero exits in pipes as errors. |
| 4 | +set -uo pipefail |
| 5 | + |
| 6 | +# Ensure that the "**" glob operator is applied recursively. |
| 7 | +shopt -s globstar |
| 8 | + |
| 9 | +# Break on first error. |
| 10 | +set -e |
| 11 | + |
| 12 | +# Parse input arguments. |
| 13 | +diff_only=0 |
| 14 | +for i in "$@"; do |
| 15 | + case $i in |
| 16 | + --diff ) |
| 17 | + diff_only=1 |
| 18 | + shift;; |
| 19 | + |
| 20 | + *) |
| 21 | + echo "Unknown option: $i"; |
| 22 | + exit 1;; |
| 23 | + esac |
| 24 | +done |
| 25 | + |
| 26 | +# Make sure the current working directory for this script is the root directory. |
| 27 | +cd "$(git -C "$(dirname "${0}")" rev-parse --show-toplevel )" |
| 28 | + |
| 29 | +# Get all python files or directories that need to be linted. |
| 30 | +lintable_locations="graphql_compiler/" |
| 31 | +# pylint doesn't support linting directories that aren't packages: |
| 32 | +# https://github.com/PyCQA/pylint/issues/352 |
| 33 | +# Use **/*.py to supply all python files for individual linting. |
| 34 | +pylint_lintable_locations="graphql_compiler/**/*.py" |
| 35 | +if [ "$diff_only" -eq 1 ] ; then |
| 36 | + # Quotes don't need to be escaped because they nest with $( ). |
| 37 | + lintable_locations="$(git diff --name-only master... | grep "\.*\.py$")" |
| 38 | + pylint_lintable_locations="$lintable_locations" |
| 39 | +fi |
| 40 | + |
| 41 | +# Continue on error to allow ignoring certain linters. |
| 42 | +# Errors are manually aggregated at the end. |
| 43 | +set +e |
| 44 | + |
| 45 | +echo -e '*** Running isort... ***\n' |
| 46 | +isort --check-only --recursive graphql_compiler/ |
| 47 | +isort_exit_code=$? |
| 48 | +echo -e "\n*** End of isort run; exit: $isort_exit_code ***\n" |
| 49 | + |
| 50 | +echo -e '*** Running flake8... ***\n' |
| 51 | +flake8 $lintable_locations |
| 52 | +flake_exit_code=$? |
| 53 | +echo -e "\n*** End of flake8 run, exit: $flake_exit_code ***\n" |
| 54 | + |
| 55 | +echo -e '\n*** Running pydocstyle... ***\n' |
| 56 | +pydocstyle --config=.pydocstyle $lintable_locations |
| 57 | +pydocstyle_exit_code=$? |
| 58 | +pydocstyle --config=.pydocstyle_test $lintable_locations |
| 59 | +pydocstyle_test_exit_code=$? |
| 60 | +echo -e "\n*** End of pydocstyle run, exit: $pydocstyle_exit_code ***\n" |
| 61 | + |
| 62 | +echo -e '\n*** Running pylint... ***\n' |
| 63 | +pylint $pylint_lintable_locations |
| 64 | +pylint_exit_code=$? |
| 65 | +echo -e "\n*** End of pylint run, exit: $pylint_exit_code ***\n" |
| 66 | + |
| 67 | +echo -e '\n*** Running bandit... ***\n' |
| 68 | +bandit -r $lintable_locations |
| 69 | +bandit_exit_code=$? |
| 70 | +echo -e "\n*** End of bandit run, exit: $bandit_exit_code ***\n" |
| 71 | + |
| 72 | +if [[ ("$flake_exit_code" != "0") || |
| 73 | + ("$pydocstyle_exit_code" != "0") || |
| 74 | + ("$pydocstyle_test_exit_code" != "0") || |
| 75 | + ("$pylint_exit_code" != "0") || |
| 76 | + ("$bandit_exit_code" != "0") || |
| 77 | + ("$isort_exit_code" != "0") ]]; then |
| 78 | + echo -e "\n*** Lint failed. ***\n" |
| 79 | + echo -e "isort exit: $isort_exit_code" |
| 80 | + echo -e "flake8 exit: $flake_exit_code" |
| 81 | + echo -e "pydocstyle exit: $pydocstyle_exit_code" |
| 82 | + echo -e "pydocstyle test exit: $pydocstyle_test_exit_code" |
| 83 | + echo -e "pylint exit: $pylint_exit_code" |
| 84 | + echo -e "bandit exit: $bandit_exit_code" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +echo -e "\n*** Lint successful. ***\n" |
0 commit comments