|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script checks only that the modified files follow pep8 |
| 4 | + |
| 5 | +# Fail on error |
| 6 | +set -e |
| 7 | + |
| 8 | +# Exit on pipe fail |
| 9 | +set -o pipefail |
| 10 | + |
| 11 | +PYLINT=${PYLINT:-pylint} |
| 12 | +BLACK=${BLACK:-black} |
| 13 | + |
| 14 | +command -v git >/dev/null 2>&1 || { echo >&2 "git is missing"; exit 1; } |
| 15 | +command -v ${PYLINT} >/dev/null 2>&1 || { echo >&2 "${PYLINT} is missing"; exit 1; } |
| 16 | +command -v ${BLACK} >/dev/null 2>&1 || { echo >&2 "${BLACK} is missing"; exit 1; } |
| 17 | + |
| 18 | +# check if stdout is a terminal. |
| 19 | +if test -t 1; then |
| 20 | + # see if it supports colors. |
| 21 | + ncolors=$(tput colors) |
| 22 | + if test -n "$ncolors" && test $ncolors -ge 8; then |
| 23 | + normal="$(tput sgr0)" |
| 24 | + red="$(tput setaf 1)" |
| 25 | + green="$(tput setaf 2)" |
| 26 | + fi |
| 27 | +fi |
| 28 | + |
| 29 | +# pylint returns a bitmask as status code: |
| 30 | +# |
| 31 | +# 0 no error |
| 32 | +# 1 fatal message issued |
| 33 | +# 2 error message issued |
| 34 | +# 4 warning message issued |
| 35 | +# 8 refactor message issued |
| 36 | +# 16 convention message issued |
| 37 | +# 32 usage error |
| 38 | + |
| 39 | +# grep will exit with 1 if no lines are found |
| 40 | +FILES=$(git --no-pager diff --diff-filter=d --name-only origin/master | grep -v "old/" | grep -E ".py\$" || exit 0) |
| 41 | +if [ -z "${FILES}" ] ; then |
| 42 | + exit 0 |
| 43 | +fi |
| 44 | + |
| 45 | +${BLACK} --check --fast ${FILES} |
| 46 | + |
| 47 | +${PYLINT} --persistent=no ${FILES} || { |
| 48 | + pylint_status=$? |
| 49 | + exitcode=0 |
| 50 | + if (( ${pylint_status} \& 1 )); then |
| 51 | + echo fatal messsage >&2 |
| 52 | + exitcode=2 |
| 53 | + fi |
| 54 | + if (( ${pylint_status} \& 2 )); then |
| 55 | + echo error messsage >&2 |
| 56 | + exitcode=2 |
| 57 | + fi |
| 58 | + if (( ${pylint_status} \& 4 )); then |
| 59 | + echo warning messsage >&2 |
| 60 | + exitcode=2 |
| 61 | + fi |
| 62 | + if (( ${pylint_status} \& 8 )); then |
| 63 | + echo refactor messsage >&2 |
| 64 | + fi |
| 65 | + if (( ${pylint_status} \& 16 )); then |
| 66 | + echo convention messsage >&2 |
| 67 | + fi |
| 68 | + # This should not happen! |
| 69 | + if (( ${pylint_status} \& 32 )); then |
| 70 | + echo usage error >&2 |
| 71 | + exitcode=2 |
| 72 | + fi |
| 73 | + exit ${exitcode} |
| 74 | +} |
0 commit comments