Skip to content

Instantly share code, notes, and snippets.



Forked from steder/
Created Nov 29, 2011
What would you like to do?
Git Pre-commit Hook For Python now with extra Python!
#!/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)
return p.returncode,,
_, files_modified, _= run("git diff-index --name-only HEAD")
file_modified = filed_modified.strip().split()
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)
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