Update python hook with the new pycodestyle

This commit is contained in:
Thibault Saunier 2018-04-12 23:24:16 -03:00
parent 8382fddbe9
commit c82ba4ac72

View file

@ -4,25 +4,24 @@ import subprocess
import sys import sys
import tempfile import tempfile
NOT_PEP8_COMPLIANT_MESSAGE_PRE = \ NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE = \
"Your code is not fully pep8 compliant and contains"\ "Your code is not fully pycodestyle compliant and contains"\
" the following coding style issues:\n\n" " the following coding style issues:\n\n"
NOT_PEP8_COMPLIANT_MESSAGE_POST = \ NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST = \
"Please fix these errors and commit again, you can do so "\ "Please fix these errors and commit again, you can do so "\
"from the root directory automatically like this, assuming the whole "\ "from the root directory automatically like this, assuming the whole "\
"file is to be commited:" "file is to be commited:"
NO_PEP8_MESSAGE = \ NO_PYCODESTYLE_MESSAGE = \
"You should install the pep8 style checker to be able"\ "You should install the pycodestyle style checker to be able"\
" to commit in this repo.\nIt allows us to garantee that "\ " to commit in this repo.\nIt allows us to garantee that "\
"anything that is commited respects the pep8 coding style "\ "anything that is commited respects the pycodestyle coding style "\
"standard.\nYou can install it:\n"\ "standard.\nYou can install it:\n"\
" * on ubuntu, debian: $sudo apt-get install pep8 \n"\ " * on ubuntu, debian: $sudo apt-get install pycodestyle \n"\
" * on fedora: #yum install python-pep8 \n"\ " * on fedora: #yum install python3-pycodestyle \n"\
" * on arch: #pacman -S pep8-python3 \n"\ " * on arch: #pacman -S python-pycodestyle \n"\
" * or add the official pep8 from http://www.python.org/dev/peps/pep-0008/"\ " * or `pip install --user pycodestyle`"
" in your $PATH"
def system(*args, **kwargs): def system(*args, **kwargs):
@ -57,20 +56,20 @@ def main():
try: try:
if not modified_file.endswith(".py"): if not modified_file.endswith(".py"):
continue continue
pep8_errors = system('pep8', '--repeat', '--ignore', 'E501,E128', modified_file) pycodestyle_errors = system('pycodestyle', '--repeat', '--ignore', 'E501,E128', modified_file)
if pep8_errors: if pycodestyle_errors:
if output_message is None: if output_message is None:
output_message = NOT_PEP8_COMPLIANT_MESSAGE_PRE output_message = NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE
output_message += pep8_errors output_message += pycodestyle_errors
non_compliant_files.append(modified_file) non_compliant_files.append(modified_file)
except OSError: except OSError as e:
output_message = NO_PEP8_MESSAGE output_message = NO_PYCODESTYLE_MESSAGE
break break
if output_message: if output_message:
print(output_message) print(output_message)
if non_compliant_files: if non_compliant_files:
print(NOT_PEP8_COMPLIANT_MESSAGE_POST) print(NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST)
for non_compliant_file in non_compliant_files: for non_compliant_file in non_compliant_files:
print("autopep8 -i ", non_compliant_file, "; git add ", print("autopep8 -i ", non_compliant_file, "; git add ",
non_compliant_file) non_compliant_file)