summaryrefslogtreecommitdiff
path: root/.git-pre-commit
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2015-07-09 11:43:48 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2015-07-09 11:43:48 +0200
commit192aa78c90253bede27b2745d4e80aeed79a3aef (patch)
tree3288344a8695ac4fd53162546fb051229c921e4d /.git-pre-commit
parenta6cb7f927f40bd129f31493c37dfe7d0d6f6a070 (diff)
parent73f54f47a7d4eac329c27af71c7fc5ad432d2e84 (diff)
downloadpsutil-get_open_files_thread.tar.gz
Merge branch 'master' into get_open_files_threadget_open_files_thread
Diffstat (limited to '.git-pre-commit')
-rwxr-xr-x[-rw-r--r--].git-pre-commit72
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()