Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PEP8 pre-commit hook in Python
#!/usr/bin/env python
from __future__ import with_statement
import os
import re
import shutil
import subprocess
import sys
import tempfile
def system(*args, **kwargs):
kwargs.setdefault('stdout', subprocess.PIPE)
proc = subprocess.Popen(args, **kwargs)
out, err = proc.communicate()
return out
def main():
modified = re.compile('^[AM]+\s+(?P<name>.*\.py)', re.MULTILINE)
files = system('git', 'status', '--porcelain')
files = modified.findall(files)
tempdir = tempfile.mkdtemp()
for name in files:
filename = os.path.join(tempdir, name)
filepath = os.path.dirname(filename)
if not os.path.exists(filepath):
with file(filename, 'w') as f:
system('git', 'show', ':' + name, stdout=f)
output = system('pep8', '.', cwd=tempdir)
if output:
print output,
if __name__ == '__main__':

kgn commented Jun 29, 2011

Thanks for this, it helped me get going with git hooks! After some digging and looking at other examples I found a cleaner way to get the modified and added files:


lentil commented Jun 30, 2011

kevincal commented May 5, 2012


FILES=$(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$)
if [ -n "$FILES" ]; then
pep8 -r $FILES

cowlicks commented Apr 1, 2014

#!/usr/bin/env bash

git diff --cached | pep8 --diff

dmwelch commented Aug 12, 2014

@cowlicks Short and sweet (+), but it doesn't handle new files. Combining @kevincal and your script would be optimal IMHO:

#!/usr/bin/env bash

FILES=($(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$))
for FILE in ${FILES[*]}; do
    pep8 --diff ${FILE}

umbrae commented Apr 8, 2015

For anyone else that stumbles onto this, @cowlicks looks to have the better approach. pep8 --diff takes input from stdin, and would indeed catch new files because in order to be committed they would need to be git added to begin with, which means they would show up in git diff --cached. The pre-2014 comments were all before pep8 had diff support, I believe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment