[PATCH 02/10] Run the pre-commit hook on content staged for commit
David Shea
dshea at redhat.com
Wed Sep 25 18:21:46 UTC 2013
Changed the pylint Module output so that it's more clear what file is
being checked. Added an option to runpylint.sh to manually set the
filename to display.
---
scripts/githooks/pre-commit | 47 ++++++++++++++++++++++++++++++++-------------
tests/pylint/runpylint.sh | 16 ++++++++++++++-
2 files changed, 49 insertions(+), 14 deletions(-)
diff --git a/scripts/githooks/pre-commit b/scripts/githooks/pre-commit
index 1528793..a3307b9 100755
--- a/scripts/githooks/pre-commit
+++ b/scripts/githooks/pre-commit
@@ -7,6 +7,7 @@
"""
import os
import sys
+import tempfile
from subprocess import check_output, CalledProcessError
def is_python(filename):
@@ -14,11 +15,11 @@ def is_python(filename):
return True
try:
- with open(filename) as testfile:
- if testfile.readline().startswith("#!/usr/bin/python"):
- return True
- except IOError:
- pass
+ s = check_output(["git", "show", (":%s" % filename)]).splitlines()
+ if s and s[0].startswith("#!/usr/bin/python"):
+ return True
+ except CalledProcessError:
+ sys.exit(1)
return False
@@ -37,24 +38,44 @@ except CalledProcessError:
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 is_python(path):
- pylint_files.append(path)
+ fields = gf.split()
+
+ # If the file is being removed, ignore it
+ if fields[4] == 'D':
+ continue
+ elif is_python(fields[-1]):
+ pylint_files.append(fields[-1])
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
+# Make a list of tuples of (filename, tempfile) where the tempfile
+# contains the file as staged for commit.
+pylint_commits = []
+for pylint_file in pylint_files:
+ commit = tempfile.NamedTemporaryFile()
+ try:
+ commit.write(check_output(["git", "show", (":%s" % pylint_file)]))
+ except CalledProcessError:
+ sys.exit(1)
+ commit.flush()
+ pylint_commits.append((pylint_file, commit))
+
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
+success = True
+for commit in pylint_commits:
+ try:
+ check_output(["./tests/pylint/runpylint.sh", "--displayname", commit[0], commit[1].name])
+ except CalledProcessError as e:
+ print e.output
+ success = False
+
+if not success:
sys.exit(1)
sys.exit(0)
diff --git a/tests/pylint/runpylint.sh b/tests/pylint/runpylint.sh
index 1396d33..c943900 100755
--- a/tests/pylint/runpylint.sh
+++ b/tests/pylint/runpylint.sh
@@ -47,16 +47,21 @@ DISABLED_ERR_OPTIONS="--disable=E1103"
DISABLED_WARN_OPTIONS="--disable=W0110,W0141,W0142,W0223,W0403,W0511,W0603,W0604,W0613,W0614"
usage () {
- echo "usage: `basename $0` [--strict] [--help] [files...]"
+ echo "usage: `basename $0` [--strict] [--help] [--displayname NAME] [files...]"
exit $1
}
FILES=
+DISPLAYNAME=
while [ $# -gt 0 ]; do
case $1 in
--strict)
NON_STRICT_OPTIONS=
;;
+ --displayname)
+ DISPLAYNAME="$2"
+ shift
+ ;;
--help)
usage 0
;;
@@ -86,6 +91,12 @@ if [ -z "$FILES" ]; then
FILES="$(find "${top_srcdir}"/{anaconda,pyanaconda,tests,widgets,utils,scripts} -type f \( -name '*.py' -o -exec awk -e 'NR==1 { if ($0 ~ /^#!\/usr\/bin\/python/) exit 0; else exit 1; }' -e 'END { if (NR == 0) exit 1; }' {} \; \) -print)"
fi
for i in $FILES; do
+ if [ -z "$DISPLAYNAME" ]; then
+ _DISPLAYNAME="$i"
+ else
+ _DISPLAYNAME="$DISPLAYNAME"
+ fi
+
if [ -n "$(echo "$i" | grep 'pyanaconda/packaging/dnfpayload.py$')" ]; then
continue
fi
@@ -104,6 +115,9 @@ for i in $FILES; do
# I0011 is the informational "Locally disabling ...." message
if [ -n "$(echo "$pylint_output" | fgrep -v '************* Module ' |\
grep -v '^I0011:')" ]; then
+ # Replace the Module line with the actual filename
+ pylint_output="$(echo "$pylint_output" | sed "s|\* Module .*|* Module $_DISPLAYNAME|")"
+
if [ "$pylint_log" -ne 0 ]; then
echo "$pylint_output" >> pylint-log
else
--
1.8.3.1
More information about the anaconda-patches
mailing list