2016-11-04 21:04:37 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-10-26 13:47:12 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2018-04-13 02:24:16 +00:00
|
|
|
NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE = \
|
|
|
|
"Your code is not fully pycodestyle compliant and contains"\
|
2014-10-26 13:47:12 +00:00
|
|
|
" the following coding style issues:\n\n"
|
|
|
|
|
2018-04-13 02:24:16 +00:00
|
|
|
NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST = \
|
2014-10-26 13:47:12 +00:00
|
|
|
"Please fix these errors and commit again, you can do so "\
|
|
|
|
"from the root directory automatically like this, assuming the whole "\
|
|
|
|
"file is to be commited:"
|
|
|
|
|
2018-04-13 02:24:16 +00:00
|
|
|
NO_PYCODESTYLE_MESSAGE = \
|
|
|
|
"You should install the pycodestyle style checker to be able"\
|
2014-10-26 13:47:12 +00:00
|
|
|
" to commit in this repo.\nIt allows us to garantee that "\
|
2018-04-13 02:24:16 +00:00
|
|
|
"anything that is commited respects the pycodestyle coding style "\
|
2014-10-26 13:47:12 +00:00
|
|
|
"standard.\nYou can install it:\n"\
|
2018-04-13 02:24:16 +00:00
|
|
|
" * on ubuntu, debian: $sudo apt-get install pycodestyle \n"\
|
|
|
|
" * on fedora: #yum install python3-pycodestyle \n"\
|
|
|
|
" * on arch: #pacman -S python-pycodestyle \n"\
|
|
|
|
" * or `pip install --user pycodestyle`"
|
2014-10-26 13:47:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def system(*args, **kwargs):
|
|
|
|
kwargs.setdefault('stdout', subprocess.PIPE)
|
|
|
|
proc = subprocess.Popen(args, **kwargs)
|
|
|
|
out, err = proc.communicate()
|
|
|
|
if type(out) == bytes:
|
|
|
|
out = out.decode()
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def copy_files_to_tmp_dir(files):
|
|
|
|
tempdir = tempfile.mkdtemp()
|
|
|
|
for name in files:
|
|
|
|
filename = os.path.join(tempdir, name)
|
|
|
|
filepath = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(filepath):
|
|
|
|
os.makedirs(filepath)
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
system('git', 'show', ':' + name, stdout=f)
|
|
|
|
|
|
|
|
return tempdir
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
modified_files = system('git', 'diff-index', '--cached',
|
|
|
|
'--name-only', 'HEAD', '--diff-filter=ACMR').split("\n")[:-1]
|
|
|
|
non_compliant_files = []
|
|
|
|
output_message = None
|
|
|
|
|
|
|
|
for modified_file in modified_files:
|
|
|
|
try:
|
2014-11-25 14:30:42 +00:00
|
|
|
if not modified_file.endswith(".py"):
|
|
|
|
continue
|
2019-01-14 21:55:35 +00:00
|
|
|
pycodestyle_errors = system('pycodestyle', '--repeat', '--ignore', 'E501,E128,W605,W503', modified_file)
|
2018-04-13 02:24:16 +00:00
|
|
|
if pycodestyle_errors:
|
2014-10-26 13:47:12 +00:00
|
|
|
if output_message is None:
|
2018-04-13 02:24:16 +00:00
|
|
|
output_message = NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE
|
|
|
|
output_message += pycodestyle_errors
|
2014-10-26 13:47:12 +00:00
|
|
|
non_compliant_files.append(modified_file)
|
2018-04-13 02:24:16 +00:00
|
|
|
except OSError as e:
|
|
|
|
output_message = NO_PYCODESTYLE_MESSAGE
|
2014-10-26 13:47:12 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if output_message:
|
2016-11-04 21:04:37 +00:00
|
|
|
print(output_message)
|
2014-10-26 13:47:12 +00:00
|
|
|
if non_compliant_files:
|
2018-04-13 02:24:16 +00:00
|
|
|
print(NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST)
|
2014-10-26 13:47:12 +00:00
|
|
|
for non_compliant_file in non_compliant_files:
|
2016-11-04 21:04:37 +00:00
|
|
|
print("autopep8 -i ", non_compliant_file, "; git add ",
|
2018-04-13 02:24:16 +00:00
|
|
|
non_compliant_file)
|
2016-11-04 21:04:37 +00:00
|
|
|
print("git commit")
|
2014-10-26 13:47:12 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|