from __future__ import print_function
import os
import subprocess
import sys
def term_supports_colors():
try:
import curses
assert sys.stderr.isatty()
curses.setupterm()
assert curses.tigetnum("colors") > 0
except Exception:
return False
else:
return True
def hilite(s, ok=True, bold=False):
if not term_supports_colors():
return s
attr = []
if ok is None: pass
elif ok: attr.append('32')
else: attr.append('31')
if bold:
attr.append('1')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), s)
def exit(msg):
msg = hilite(msg, ok=False)
print(msg, file=sys.stderr)
sys.exit(1)
def sh(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise RuntimeError(stderr)
if stderr:
print(stderr, file=sys.stderr)
if stdout.endswith('\n'):
stdout = stdout[:-1]
return stdout
def main():
out = sh("git diff --cached --name-only")
py_files = [x for x in out.split('\n') if x.endswith('.py') and
os.path.exists(x)]
lineno = 0
for path in py_files:
with open(path) as f:
for line in f:
lineno += 1
if line.endswith(' '):
print("%s:%s %r" % (path, lineno, line))
return exit(
"commit aborted: space at end of line")
line = line.rstrip()
if "pdb.set_trace" in line:
print("%s:%s %s" % (path, lineno, line))
return exit(
"commit aborted: you forgot a pdb in your python code")
if "except:" in line and not line.endswith("# NOQA"):
print("%s:%s %s" % (path, lineno, line))
return exit("commit aborted: bare except clause")
if py_files:
try:
import flake8 except ImportError:
return exit("commit aborted: flake8 is not installed; "
"run 'make setup-dev-env'")
ret = subprocess.call(
"%s -m flake8 %s" % (sys.executable, " ".join(py_files)),
shell=True)
if ret != 0:
return exit("commit aborted: python code is not flake8 compliant")
main()