-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpre-commit
executable file
·37 lines (29 loc) · 1010 Bytes
/
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
#!/usr/bin/env python3
"""
Forked from https://gist.github.com/810399
"""
import re
import subprocess
import sys
import pycodestyle
def system(cmd, **kwargs):
kwargs.setdefault('stdout', subprocess.PIPE)
proc = subprocess.Popen(cmd, **kwargs)
return proc.communicate()[0]
def main():
pycheck = re.compile('.*.py')
git_args = ['git', '--no-pager', 'diff', '--cached']
diffs = pycodestyle.parse_udiff(system(git_args).decode("utf-8"))
files = diffs.keys()
files = [filename for filename in files if pycheck.match(filename)]
checker = pycodestyle.StyleGuide(
parse_argv=False,
config_file="setup.cfg",
reporter=pycodestyle.DiffReport, selected_lines=diffs)
report = checker.check_files(paths=files)
if report.total_errors != 0:
print('''PEP8 style violations have been detected. Please fix them
or force the commit with "git commit --no-verify".''')
sys.exit(1)
if __name__ == '__main__':
main()