My dotfiles. Period.

pre-commit 971B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python
  2. from __future__ import with_statement
  3. import os
  4. import re
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. def system(*args, **kwargs):
  10. kwargs.setdefault('stdout', subprocess.PIPE)
  11. proc = subprocess.Popen(args, **kwargs)
  12. out, err = proc.communicate()
  13. return out
  14. def main():
  15. modified = re.compile('^[AM]+\s+(?P<name>.*\.py)', re.MULTILINE)
  16. files = system('git', 'status', '--porcelain')
  17. files = modified.findall(files)
  18. tempdir = tempfile.mkdtemp()
  19. for name in files:
  20. filename = os.path.join(tempdir, name)
  21. filepath = os.path.dirname(filename)
  22. if not os.path.exists(filepath):
  23. os.makedirs(filepath)
  24. with file(filename, 'w') as f:
  25. system('git', 'show', ':' + name, stdout=f)
  26. output = system('pep8', '.', cwd=tempdir)
  27. shutil.rmtree(tempdir)
  28. if output:
  29. print output,
  30. sys.exit(1)
  31. if __name__ == '__main__':
  32. main()