Skip to content

Instantly share code, notes, and snippets.

@bforchhammer
Created August 14, 2014 13:39
Show Gist options
  • Save bforchhammer/bef34378f47f3d90eae3 to your computer and use it in GitHub Desktop.
Save bforchhammer/bef34378f47f3d90eae3 to your computer and use it in GitHub Desktop.
Pre-commit hook for pep8 + pyflakes check (.git/hooks/pre-commit)
#!/usr/bin/python
import sys
import re
import subprocess
modified = re.compile('^(?:M|A)..(?P<name>.*\.py)')
def main():
p = subprocess.Popen(['git', 'status', '--porcelain'],
stdout=subprocess.PIPE)
out, err = p.communicate()
modifieds = []
for line in out.splitlines():
match = modified.match(line)
if (match):
modifieds.append(match.group('name'))
rrcode = 0
for file in modifieds:
p = subprocess.Popen(['pep8', '--ignore', 'E501', file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
if out or err:
sys.stdout.write(" * pep8:\n%s\n%s" % (out, err))
rrcode = rrcode | 1
retcode = subprocess.call(['pyflakes', file])
rrcode = retcode | rrcode
sys.exit(rrcode)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment