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
|
#!/usr/bin/env python
# This gets executed on 'git commit' and rejects the commit in case the
# submitted code does not pass validation.
# Install it with "make install-git-hooks"
import os
import subprocess
import sys
def main():
out = subprocess.check_output("git diff --cached --name-only", shell=True)
files = [x for x in out.split('\n') if x.endswith('.py') and
os.path.exists(x)]
for path in files:
with open(path) as f:
data = f.read()
# pdb
if "pdb.set_trace" in data:
for lineno, line in enumerate(data.split('\n'), 1):
line = line.rstrip()
if "pdb.set_trace" in line:
print("%s: %s" % (lineno, line))
sys.exit(
"commit aborted: you forgot a pdb in your python code")
# bare except clause
if "except:" in data:
for lineno, line in enumerate(data.split('\n'), 1):
line = line.rstrip()
if "except:" in line and not line.endswith("# NOQA"):
print("%s: %s" % (lineno, line))
sys.exit("commit aborted: bare except clause")
# flake8
failed = False
for path in files:
ret = subprocess.call("python -m flake8 %s" % path, shell=True)
if ret != 0:
failed = True
if failed:
sys.exit("commit aborted: python code is not flake8-compliant")
main()
|