Created
April 18, 2018 20:29
-
-
Save juanmhidalgo/71446e4f6dc6b7a74a6990f6b7238488 to your computer and use it in GitHub Desktop.
Pre commit with flake8 and isort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
reset='\033[0m' | |
bold='\033[1m' | |
dim='\033[2m' | |
bg_black='\033[40m' | |
yellow='\033[33m' | |
COUNTER=0 | |
COUNTER_SORT=0 | |
COUNTER_FLAKE=0 | |
FILES=`git diff --cached --name-only --diff-filter=ACM` | |
TO_SORT="" | |
echo | |
echo -e "${dim}######################${reset}" | |
echo -e "${bold}CHECKING flake errors${reset}" | |
echo -e "${dim}######################${reset}" | |
echo | |
while read -r fname; do | |
if [[ $fname == *.py ]] | |
then | |
isort -q -c $fname &> /dev/null | |
if [ $? -eq 1 ]; then | |
TO_SORT+=" $fname" | |
((COUNTER_SORT++)) | |
fi | |
flake8 $fname | |
COUNTER_FLAKE=$(expr $COUNTER_FLAKE + $?) | |
fi | |
done <<< "$FILES" | |
if [ "$TO_SORT" != "" ]; then | |
echo | |
echo -e "${dim}######################${reset}" | |
echo -e "${bold}TO FIX IMPORTS RUN:${reset}" | |
echo -e "${dim}######################${reset}" | |
echo -e -n "${bg_black}${yellow}isort " | |
echo -n $TO_SORT | |
echo -e -n "${reset}" | |
echo | |
fi | |
exit $(expr $COUNTER_SORT + $COUNTER_FLAKE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[flake8] | |
ignore = D401, D403, D101, E501, D106, D102, D101, D105, D401 | |
exclude=*/migrations/*,env/*,*/templates/*,*.cfg,*.txt,*.js,ansible/*,*.sh,.git,__pycache__,docs/source/conf.py,old,build,dist | |
max-line-length=160 | |
[isort] | |
combine_as_imports = true | |
include_trailing_comma = true | |
line_length = 160 | |
multi_line_output = 5 | |
not_skip = __init__.py | |
known_django=django,rest_framework,django_filters,django_countries | |
sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.git/hooks/pre-commit
and give execution rightschmod +x .git/hooks/pre-commit