Skip to content

Instantly share code, notes, and snippets.

@pbhandari
Last active July 24, 2019 01:17
Show Gist options
  • Save pbhandari/c51dc0282f6187528f775e593265aa89 to your computer and use it in GitHub Desktop.
Save pbhandari/c51dc0282f6187528f775e593265aa89 to your computer and use it in GitHub Desktop.
pre commit hook for running pylint
#!/bin/sh
# Make sure that we're in the right virtualenv (otherwise pylint isn't going to work properly)
DESIRED_VENV=compass
if [ -z $VIRTUAL_ENV ] || [ "`basename $VIRTUAL_ENV`" != "$DESIRED_VENV" ]; then
echo You\'re not in the $DESIRED_VENV virtualenv, \`workon $DESIRED_VENV\` to fix.
exit 1
fi
# Warn if pylintrc can't be found at the top-level directory
# (really more of a Praj thing than anything else)
RCFILE="`git rev-parse --show-toplevel`"/.pylintrc
[ -e $RCFILE ] || echo "Cannot find $RCFILE"
# get a list of all .py files that have changed since my previous commit
FILES=`git diff-index --cached HEAD | awk '/.py/ { printf $6" " }'`
if [ -z "$FILES" ]; then
exit 0
fi
COMMAND="pylint --rcfile=$RCFILE $FILES"
OUTPUT="`$COMMAND 2>&1`" # saving the output for parsing later
# print out the command which was run, along with output.
echo $COMMAND
echo "$OUTPUT"
# if the output contains any errors, exit with a fail
echo "$OUTPUT" | awk '/^E:/ { exit 1 }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment