forked from Kozea/.git_hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·56 lines (47 loc) · 1.69 KB
/
pre-commit
File metadata and controls
executable file
·56 lines (47 loc) · 1.69 KB
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
#!/usr/bin/env python2
from __future__ import with_statement
import os
import shutil
import subprocess
import sys
import tempfile
import re
try:
from pygments.console import colorize
except ImportError:
colorize = lambda x, y: y
def system(*args, **kwargs):
kwargs.setdefault('stdout', subprocess.PIPE)
proc = subprocess.Popen(args, **kwargs)
out, err = proc.communicate()
return out
def main():
files = (file for file in system(
'git', 'diff', '--name-only', '--staged', '--diff-filter=AM'
).splitlines() if file.endswith('.py'))
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 file(filename, 'w') as f:
system('git', 'show', ':' + name, stdout=f)
output = system('pep8', '.', cwd=tempdir)
shutil.rmtree(tempdir)
if output:
output = re.sub(r'(.*):(\d+):(\d+)', colorize('blue', r'\1') + " " +
colorize('darkblue', r'\2') + ":" +
colorize('darkgreen', r'\3'), output)
output = re.sub(r'(E\d{3}) (.*)', colorize('red', r'\1') +
" " + colorize('darkred', r'\2'), output)
output = re.sub(r'(W\d{3}) (.*)', colorize('yellow', r'\1') +
" " + colorize('darkyellow', r'\2'), output)
print colorize('red',
'-----------------------------------\n'
'Commit refused - pep8 not respected\n'
'-----------------------------------\n')
print output
sys.exit(1)
if __name__ == '__main__':
main()