-
Notifications
You must be signed in to change notification settings - Fork 4
/
pre-commit.hook
executable file
·65 lines (54 loc) · 2.1 KB
/
pre-commit.hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
import tempfile
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]
output_message = None
tempdir = copy_files_to_tmp_dir(modified_files)
try:
pep8_errors = system('pep8', '--repeat', '--ignore', 'E501,E128', '.',
cwd=tempdir)
if pep8_errors:
output_message = "Your code is not fully pep8 compliant and contains"\
" the following coding style issues:\n\n" + pep8_errors +\
"\n"\
"We encourage you to use\n\n $autopep8 -i file/with/pep8/error.py\n\n"\
"\n"\
"Thanks for correcting them before commiting!\n"
except OSError:
output_message = "You should install the pep8 style checker to be able"\
" to commit in this repo.\nIt allows us to garantee that "\
"anything that is commited respects the pep8 coding style "\
"standard.\nYou can install it:\n"\
" * on ubuntu, debian: $sudo apt-get install pep8 \n"\
" * on fedora: #yum install python-pep8 \n"\
" * on arch: #pacman -S pep8-python3 \n"\
" * or add the official pep8 from http://www.python.org/dev/peps/pep-0008/"\
" in your $PATH"
shutil.rmtree(tempdir)
if output_message:
print(output_message, end=' ')
sys.exit(1)
if __name__ == '__main__':
main()