diff options
Diffstat (limited to '.git-pre-commit')
| -rwxr-xr-x[-rw-r--r--] | .git-pre-commit | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/.git-pre-commit b/.git-pre-commit index da628495..3a6161f4 100644..100755 --- a/.git-pre-commit +++ b/.git-pre-commit @@ -1,25 +1,47 @@ -#!/bin/bash - -# This script gets executed on "git commit -am '...'". -# You can install it with "make install-git-hooks". - -# pdb -pdb=`git diff --cached --name-only | \ - grep -E '\.py$' | \ - xargs grep -n -H -E '(pdb\.set_trace\(\))'` -if [ ! -z "$pdb" ] ; then - echo "commit aborted: you forgot a pdb in your python code" - echo $pdb - exit 1 -fi - -# flake8 -flake8=`git diff --cached --name-only | grep -E '\.py$'` -if [ ! -z "$flake8" ] ; then - flake8=`echo "$flake8" | xargs flake8` - if [ ! -z "$flake8" ] ; then - echo "commit aborted: python code is not flake8-compliant" - echo $flake8 - exit 1 - fi -fi +#!/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("flake8 %s" % path, shell=True) + if ret != 0: + failed = True + if failed: + sys.exit("commit aborted: python code is not flake8-compliant") + +main() |
