-
Notifications
You must be signed in to change notification settings - Fork 14
Styling Conventions
Eric G edited this page Jan 31, 2019
·
15 revisions
A.R.T uses a pre-commit hook to check whether the changes you have made are PEP 8 compliant
To have this pre-commit hook, you need to do the following. (This assumes you have installed the project and its requirements
-
pip install pycodestyle
-
Within your project, navigate to your git hooks folder and make a new file called
pre-commit
. It has to be named this way so that git runs this hook runs first before giving you the editor to write a commit message
cd .git/hooks
touch pre-commit
- Using your text editor, copy and paste the following code
touched_python_files=$(git diff --cached --name-only ':(exclude)api/migrations/' | grep -E '\.py$')
touched_shell_scripts=$(git diff --cached --name-only | grep -E '\.sh$')
if [ -n "$touched_python_files" ]; then
echo "========== Checking PEP8 Conformance ==========\n"
output=$(flake8 ${touched_python_files[@]})
if [ -n "$output" ]; then
echo "========== There are some PEP8 violations! Kindly fix them! Aborting commit... ==========\n"
flake8 ${touched_python_files[@]}
exit 1
fi
echo "========== Your changes are PEP8 compliant =========="
echo "========== Checking styling ==========\n"
black_output=$(black -S --check --diff --exclude="migrations|.venv" ${touched_python_files[@]})
if [ -n "$black_output" ]; then
echo "========== There are some styling violations! Kindly fix them! Aborting commit... ==========\n"
black -S --exclude="migrations|.venv" ${touched_python_files[@]}
exit 1
fi
echo "========== Your changes are styled properly =========="
echo "========== Checking imports ==========\n"
output=$(isort -rc --check-only --diff --atomic ${touched_python_files[@]})
if [ -n "$output" ]; then
echo "========== Some imports are wrongly done! Kindly fix them! Aborting commit... ==========\n"
isort --check-only -q ${touched_python_files[@]}
exit 1
fi
echo "========== Imports done properly =========="
fi
if [ -n "$touched_shell_scripts" ]; then
echo "========== Shellcheck ==========\n"
output=$(shellcheck ${touched_shell_scripts[@]})
if [ -n "$output" ]; then
echo "========== There are some shell styling violations! Kindly fix them! Aborting commit... ==========\n"
for file in $(find . -type f -name "*.sh"); do shellcheck --format=gcc $file; done;
exit 1
fi
echo "========== Your shell scripts are good to go =========="
fi
The code above simply checks which python files you have changed and runs pycodestyle to see if the are PEP 8 compliant.
-
Save and close your text editor.
-
Then make the file executable
chmod +x pre-commit
-
Test it out by
- making a PEP 8 violation, doing a
git add
and trying to commit by doing agit commit
You should see the following output if you have PEP 8 violations.
(art-b) user@ [~/path/to/art/art-backend/art]$ git commit
========== Checking PEP8 Conformance ==========
========== There are some PEP8 violations! Kindly fix them! Aborting commit... ==========
art/art/serializers.py:9:1: E303 too many blank lines (4)
art/art/serializers.py:12:64: E231 missing whitespace after ','
art/art/serializers.py:12:80: E501 line too long (80 > 79 characters)
art/art/serializers.py:12:80: E502 the backslash is redundant between brackets
art/art/serializers.py:13:10: E128 continuation line under-indented for visual indent
art/art/serializers.py:13:73: E502 the backslash is redundant between brackets
art/art/serializers.py:14:10: E128 continuation line under-indented for visual indent
art/art/serializers.py:23:80: E501 line too long (98 > 79 characters)
art/art/serializers.py:30:80: E501 line too long (92 > 79 characters)
art/art/serializers.py:31:43: E231 missing whitespace after ','
art/art/serializers.py:33:1: W293 blank line contains whitespace
art/art/serializers.py:36:1: W391 blank line at end of file
(art-b) user@ [~/path/to/art/art-backend/art]$