-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·87 lines (75 loc) · 2.39 KB
/
pre-commit
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
"""
Forked from https://gist.github.com/810399
"""
from __future__ import with_statement, print_function
import os
import re
import shutil
import subprocess
import sys
import tempfile
# don't fill in both of these
select_codes = []
ignore_codes = []
# Add things like "--max-line-length=120" below
overrides = []
def system(*args, **kwargs):
kwargs.setdefault('stdout', subprocess.PIPE)
proc = subprocess.Popen(args, **kwargs)
out, err = proc.communicate()
return out
def main():
modified = re.compile('^[AM]+\s+(?P<name>.*\.py$)', re.MULTILINE)
files = system('git', 'status', '--porcelain').decode("utf-8")
files = modified.findall(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)
tempdir2 = tempfile.mkdtemp()
for name in files:
filename2 = os.path.join(tempdir2, name)
filepath2 = os.path.dirname(filename2)
if not os.path.exists(filepath2):
os.makedirs(filepath2)
with open(filename2, 'w') as f:
system('git', 'show', ':' + name, stdout=f)
args = ['pycodestyle']
if select_codes and ignore_codes:
print(u'Error: select and ignore codes are mutually exclusive')
sys.exit(1)
elif select_codes:
args.extend(('--select', ','.join(select_codes)))
elif ignore_codes:
args.extend(('--ignore', ','.join(ignore_codes)))
args.extend(overrides)
args.append('.')
output = system(*args, cwd=tempdir)
shutil.rmtree(tempdir)
args = ['pydocstyle']
args.append('.')
output2 = system(*args, cwd=tempdir2)
shutil.rmtree(tempdir2)
error_msg = (
"{} docstring style violations have been detected. "
"Please fix them or force the commit with "
"git commit --no-verify."
)
if output:
print(error_msg.format("PEP8"))
print(output.decode("utf-8"),)
if output2:
print(error_msg.format("PEP257"))
print(output2.decode("utf-8"),)
sys.exit(1)
elif output2:
print(error_msg.format("PEP257"))
print(output2.decode("utf-8"),)
sys.exit(1)
if __name__ == '__main__':
main()