7 NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE = \
8 "Your code is not fully pycodestyle compliant and contains"\
9 " the following coding style issues:\n\n"
11 NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST = \
12 "Please fix these errors and commit again, you can do so "\
13 "from the root directory automatically like this, assuming the whole "\
14 "file is to be commited:"
16 NO_PYCODESTYLE_MESSAGE = \
17 "You should install the pycodestyle style checker to be able"\
18 " to commit in this repo.\nIt allows us to garantee that "\
19 "anything that is commited respects the pycodestyle coding style "\
20 "standard.\nYou can install it:\n"\
21 " * on ubuntu, debian: $sudo apt-get install pycodestyle \n"\
22 " * on fedora: #yum install python3-pycodestyle \n"\
23 " * on arch: #pacman -S python-pycodestyle \n"\
24 " * or `pip install --user pycodestyle`"
27 def system(*args, **kwargs):
28 kwargs.setdefault('stdout', subprocess.PIPE)
29 proc = subprocess.Popen(args, **kwargs)
30 out, err = proc.communicate()
31 if isinstance(out, bytes):
36 def copy_files_to_tmp_dir(files):
37 tempdir = tempfile.mkdtemp()
39 filename = os.path.join(tempdir, name)
40 filepath = os.path.dirname(filename)
41 if not os.path.exists(filepath):
43 with open(filename, 'w') as f:
44 system('git', 'show', ':' + name, stdout=f)
50 modified_files = system('git', 'diff-index', '--cached',
51 '--name-only', 'HEAD', '--diff-filter=ACMR').split("\n")[:-1]
52 non_compliant_files = []
55 for modified_file in modified_files:
57 if not modified_file.endswith(".py"):
59 pycodestyle_errors = system('pycodestyle', '--repeat', '--ignore', 'E402,E501,E128,W605,W503', modified_file)
60 if pycodestyle_errors:
61 if output_message is None:
62 output_message = NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE
63 output_message += pycodestyle_errors
64 non_compliant_files.append(modified_file)
66 output_message = NO_PYCODESTYLE_MESSAGE
71 if non_compliant_files:
72 print(NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST)
73 for non_compliant_file in non_compliant_files:
74 print("autopep8 -i --max-line-length 120", non_compliant_file, "; git add ",
80 if __name__ == '__main__':