Skip to content

Instantly share code, notes, and snippets.

@juanmhidalgo
Created April 18, 2018 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanmhidalgo/71446e4f6dc6b7a74a6990f6b7238488 to your computer and use it in GitHub Desktop.
Save juanmhidalgo/71446e4f6dc6b7a74a6990f6b7238488 to your computer and use it in GitHub Desktop.
Pre commit with flake8 and isort
#!/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)
[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
@juanmhidalgo
Copy link
Author

  1. Place pre-commit in .git/hooks/pre-commit and give execution rights chmod +x .git/hooks/pre-commit
  2. Place setup.cfg in the project's root folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment