| 1 | #!/bin/bashscip-shell filesystem . . `/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=`gitscip-shell system . . git. diff-index --cached HEAD 2>&1 | sedscip-shell system . . sed. 's/^:.* //' | uniqscip-shell system . . uniq. | cutscip-shell system . . cut. -b100-500` |
| 10 | if [scip-shell system . . `[`. -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 | testscip-shell system . . test. -e $f || continue |
| 23 | case $f in |
| 24 | *.py) |
| 25 | PY_FILES+=" $f" |
| 26 | ;; |
| 27 | *) |
| 28 | if headscip-shell system . . head. -1 $f | grepscip-shell system . . grep. '^#!/bin/sh' > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 29 | SH_FILES+=" $f" |
| 30 | elif headscip-shell system . . head. -1 $f | grepscip-shell system . . grep. '^#!/bin/bash' > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 31 | BASH_FILES+=" $f" |
| 32 | elif headscip-shell system . . head. -1 $f | grepscip-shell system . . grep. '^#!.*python' > /dev/nullscip-shell filesystem . . `/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 | errscip-shell shell shell-project 0.0.0 err().() { |
| 50 | echoscip-shell system . . echo. >&2 ERROR: "$@" |
| 51 | exit 1 |
| 52 | } |
| 53 | |
| 54 | warnscip-shell shell shell-project 0.0.0 warn().() { |
| 55 | echoscip-shell system . . echo. >&2 WARNING: "$@" |
| 56 | } |
| 57 | |
| 58 | cmd_existsscip-shell shell shell-project 0.0.0 cmd_exists().() { |
| 59 | command -v $1 1>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 60 | } |
| 61 | |
| 62 | find_commandscip-shell shell shell-project 0.0.0 find_command().() { |
| 63 | for name in "$@"; do |
| 64 | for suff in "3" "-3" "-python3" ""; do |
| 65 | cmd=${name}${suff} |
| 66 | if cmd_existsscip-shell shell shell-project 0.0.0 cmd_exists(). $cmd; then |
| 67 | echoscip-shell system . . echo. $cmd |
| 68 | return 0 |
| 69 | fi |
| 70 | done |
| 71 | done |
| 72 | warnscip-shell shell shell-project 0.0.0 warn(). "$1 is not installed, using dummy placeholder!" |
| 73 | echoscip-shell system . . echo. : |
| 74 | } |
| 75 | |
| 76 | DASH=$(find_commandscip-shell shell shell-project 0.0.0 find_command(). dash) |
| 77 | PYFLAKES=$(find_commandscip-shell shell shell-project 0.0.0 find_command(). pyflakes) |
| 78 | PEP8=$(find_commandscip-shell shell shell-project 0.0.0 find_command(). pycodestyle pep8) |
| 79 | |
| 80 | if [scip-shell system . . `[`. "$PY_FILES" != " " ]; then |
| 81 | if ! $PYFLAKES $PY_FILES; then |
| 82 | errscip-shell shell shell-project 0.0.0 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 | errscip-shell shell shell-project 0.0.0 err(). "pep8 tests failed!" |
| 89 | fi |
| 90 | fi |
| 91 | |
| 92 | for f in $SH_FILES; do |
| 93 | if ! $DASH -n $f; then |
| 94 | errscip-shell shell shell-project 0.0.0 err(). "dash tests failed!" |
| 95 | fi |
| 96 | done |
| 97 | |
| 98 | for f in $BASH_FILES; do |
| 99 | if ! bashscip-shell system . . bash. -n $f; then |
| 100 | errscip-shell shell shell-project 0.0.0 err(). "bash tests failed!" |
| 101 | fi |
| 102 | done |
| 103 | |
| 104 | exit 0 |