[PATCH] add pre-commit hook to run pylint

Brian C. Lane bcl at redhat.com
Thu Sep 12 21:59:12 UTC 2013


From: "Brian C. Lane" <bcl at redhat.com>

This hook will run the runpylint.sh test on just the files changed in
the commit.
---
 scripts/githooks/README     | 14 ++++++++++++++
 scripts/githooks/pre-commit | 47 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100755 scripts/githooks/pre-commit

diff --git a/scripts/githooks/README b/scripts/githooks/README
index a3087a2..92bccd7 100644
--- a/scripts/githooks/README
+++ b/scripts/githooks/README
@@ -10,3 +10,17 @@ commit-msg
     also want to create a ~/.rhbzauth file containing two shell
     variables:  RHBZ_USER and RHBZ_PASSWORD to allow automatic login
     when commiting patches.
+
+pre-commit
+    Run the runpylint.sh test on the files changed in the commit.
+
+    To use, copy this script to your .git/hooks directory and
+    edit the OTHER_MODULES_PATH to point to where you normally have
+    pykickstart and blivet checked out. Note that if the current
+    working versions of these differ too much the pylint checks may
+    fail.
+
+    Set the NOPYLINT= environmental variable while committing to skip
+    running pylint. eg.
+    NOPYLINT= git commit -a -m "I didn't break it"
+
diff --git a/scripts/githooks/pre-commit b/scripts/githooks/pre-commit
new file mode 100755
index 0000000..7ab2df7
--- /dev/null
+++ b/scripts/githooks/pre-commit
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+""" Run the Anaconda pylint tests on the files changed in this commit
+
+    Set NOPYLINT env variable to skip this. eg. NOPYLINT= git commit
+
+
+"""
+import os
+import sys
+from subprocess import check_output, CalledProcessError
+
+OTHER_MODULES_PATH = ".:../blivet/:../pykickstart/"
+
+if "NOPYLINT" in os.environ:
+    print "Skipping pre-commit pylint run"
+    sys.exit(0)
+
+# run pylint on all the python files changed by this commit
+try:
+    git_files = check_output("git diff-index --cached HEAD", shell=True)
+except CalledProcessError:
+    sys.exit(1)
+
+pylint_files = []
+# Lines look like: :100755 100755 2cf57974e13a2aae778e28f942a4d44bf6567409 6fe1b6caf32d565b2cdb6d1aee250aaddc6d3a04 M      tests/pylint/runpylint.sh
+for gf in git_files.splitlines():
+    path = gf.split()[-1]
+    if path == "anaconda" or path.endswith(".py"):
+        pylint_files.append(path)
+
+if not pylint_files:
+    sys.exit(0)
+pylint_files = " ".join(pylint_files)
+
+# Make sure pykickstart and blivet can be found
+# Note that if the checked out versions are too far off pylint may fail
+env = os.environ.copy()
+env["PYTHONPATH"] = OTHER_MODULES_PATH
+
+print "Running pylint on %s" % pylint_files
+try:
+    check_output("./tests/pylint/runpylint.sh %s" % pylint_files, shell=True, env=env)
+except CalledProcessError as e:
+    print e.output
+    sys.exit(1)
+
+sys.exit(0)
-- 
1.8.3.1



More information about the anaconda-patches mailing list