| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Simple pre-commit hook to check that there are no errors in the sdkmanager |
| 4 | # source files. |
| 5 | |
| 6 | # Redirect output to stderr. |
| 7 | exec 1>&2 |
| 8 | |
| 9 | files=`git diff-index --cached HEAD 2>&1 | sed 's/^:.* //' | uniq | cut -b100-500` |
| 10 | if [ -z "$files" ]; then |
| 11 | PY_FILES="*.py" |
| 12 | SH_FILES="hooks/pre-commit" |
| 13 | BASH_FILES="completion/bash-completion" |
| 14 | else |
| 15 | # if actually committing right now, then only run on the files |
| 16 | # that are going to be committed at this moment |
| 17 | PY_FILES= |
| 18 | SH_FILES= |
| 19 | BASH_FILES= |
| 20 | |
| 21 | for f in $files; do |
| 22 | test -e $f || continue |
| 23 | case $f in |
| 24 | *.py) |
| 25 | PY_FILES+=" $f" |
| 26 | ;; |
| 27 | *) |
| 28 | if head -1 $f | grep '^#!/bin/sh' > /dev/null 2>&1; then |
| 29 | SH_FILES+=" $f" |
| 30 | elif head -1 $f | grep '^#!/bin/bash' > /dev/null 2>&1; then |
| 31 | BASH_FILES+=" $f" |
| 32 | elif head -1 $f | grep '^#!.*python' > /dev/null 2>&1; then |
| 33 | PY_FILES+=" $f" |
| 34 | fi |
| 35 | ;; |
| 36 | esac |
| 37 | done |
| 38 | fi |
| 39 | |
| 40 | # We ignore the following PEP8 warnings |
| 41 | # * E501: line too long (82 > 79 characters) |
| 42 | # - Recommended for readability but not enforced |
| 43 | # - Some lines are awkward to wrap around a char limit |
| 44 | # * W503: line break before binary operator |
| 45 | # - Quite pedantic |
| 46 | |
| 47 | PEP8_IGNORE="E501,W503" |
| 48 | |
| 49 | err() { |
| 50 | echo >&2 ERROR: "$@" |
| 51 | exit 1 |
| 52 | } |
| 53 | |
| 54 | warn() { |
| 55 | echo >&2 WARNING: "$@" |
| 56 | } |
| 57 | |
| 58 | cmd_exists() { |
| 59 | command -v $1 1>/dev/null |
| 60 | } |
| 61 | |
| 62 | find_command() { |
| 63 | for name in "$@"; do |
| 64 | for suff in "3" "-3" "-python3" ""; do |
| 65 | cmd=${name}${suff} |
| 66 | if cmd_exists $cmd; then |
| 67 | echo $cmd |
| 68 | return 0 |
| 69 | fi |
| 70 | done |
| 71 | done |
| 72 | warn "$1 is not installed, using dummy placeholder!" |
| 73 | echo : |
| 74 | } |
| 75 | |
| 76 | DASH=$(find_command dash) |
| 77 | PYFLAKES=$(find_command pyflakes) |
| 78 | PEP8=$(find_command pycodestyle pep8) |
| 79 | |
| 80 | if [ "$PY_FILES" != " " ]; then |
| 81 | if ! $PYFLAKES $PY_FILES; then |
| 82 | err "pyflakes tests failed!" |
| 83 | fi |
| 84 | # The tests use a little hack in order to cleanly import the sdkmanager |
| 85 | # package locally like a regular package. pep8 doesn't see that, so this |
| 86 | # makes pep8 skip E402 on the test files that need that hack. |
| 87 | if ! $PEP8 --ignore=$PEP8_IGNORE,E402 $PY_FILES; then |
| 88 | err "pep8 tests failed!" |
| 89 | fi |
| 90 | fi |
| 91 | |
| 92 | for f in $SH_FILES; do |
| 93 | if ! $DASH -n $f; then |
| 94 | err "dash tests failed!" |
| 95 | fi |
| 96 | done |
| 97 | |
| 98 | for f in $BASH_FILES; do |
| 99 | if ! bash -n $f; then |
| 100 | err "bash tests failed!" |
| 101 | fi |
| 102 | done |
| 103 | |
| 104 | exit 0 |