Created
September 13, 2011 15:11
-
-
Save steder/1214061 to your computer and use it in GitHub Desktop.
Git Pre-commit Hook For Python now with extra Python!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- mode: python -*- | |
from subprocess import Popen, PIPE | |
import sys | |
syntax_checker = "pyflakes" | |
def run(command): | |
p = Popen(command.split(), stdout=PIPE, stderr=PIPE) | |
p.wait() | |
return p.returncode, p.stdout.read().strip().split(), p.stderr.read() | |
_, files_modified, _= run("git diff-index --name-only --cached HEAD") | |
for fname in files_modified: | |
if fname.endswith(".py"): | |
print >>sys.stderr, "Checking syntax on %s: ... "%(fname,), | |
exit_code, _, errors = run("%s %s"%(syntax_checker, fname)) | |
if exit_code != 0: | |
print >>sys.stderr, "\rChecking syntax on %s: FAILED! \n%s"%(fname, errors) | |
sys.exit(exit_code) | |
else: | |
print >>sys.stderr, "\rChecking syntax on %s: OK!"%(fname,) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @mtigas, I've updated the gist.