Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added pep8 git pre commit hook

  • Loading branch information...
commit 7fae3cb5d00472f9794d187fb0a767fd85dcfd80 1 parent 6975ca6
@metachris authored
Showing with 46 additions and 4 deletions.
  1. +10 −4 Readme.md
  2. +36 −0 python/pep8-git-pre-commit-hook
View
14 Readme.md
@@ -1,16 +1,22 @@
-#Boilerplate Collection
+# Boilerplate Collection
Boilerplates are basic setups with best practices. Just take the parts you like.
-###Python
+### Python
* [Python Script](https://github.com/metachris/boilerplates/tree/master/python)
* [Tornado Website](https://github.com/metachris/tornado-boilerplate)
* [App Engine Website](https://github.com/metachris/appengine-boilerplate)
-###Dotfiles
+### PEP8 Pre Commit Hook for Git
+
+This [git pre-commit hook](https://github.com/metachris/boilerplates/tree/master/python/pep8-git-pre-commit-hook) checks for pep8 compatibility on committing, and aborts the commit if errors are found. Can be skipped by applying the `--no-verify` flag to `git commit`. This requires the tool `pep8` which can be installed via package managers and homebrew.
+
+To use the commit hook in all projects on this system, copy it into the global git hook-templates directory (eg. `/usr/share/git-core/templates/hooks/`) as a file called `pre-commit` and set the permissions to executable. Afterwards you can enable this hook in existing repositories by calling `git init`. New repositories will use it by default.
-[Boilerplate dotfiles](https://github.com/metachris/boilerplates/tree/master/dotfiles) is a collection of command line goodness for OSX and Linux. Includes `.bash_profile`, `.aliases`, `.exports`, and others.
+### Dotfiles
+
+[Boilerplate dotfiles](https://github.com/metachris/boilerplates/tree/master/dotfiles) is a collection of command line goodness for OSX and Linux. Includes `.bash_profile`, `.aliases`, `.exports`, and others.
View
36 python/pep8-git-pre-commit-hook
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+from __future__ import with_statement
+import os
+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():
+ files = (file for file in system('git', 'diff', '--name-only', '--staged', '--diff-filter=AM').splitlines() if file.endswith('.py'))
+
+ tempdir = tempfile.mkdtemp()
+ for name in files:
+ filename = os.path.join(tempdir, name)
+ filepath = os.path.dirname(filename)
+ if not os.path.exists(filepath):
+ os.makedirs(filepath)
+ with file(filename, 'w') as f:
+ system('git', 'show', ':' + name, stdout=f)
+ output = system('pep8', '.', cwd=tempdir)
+ shutil.rmtree(tempdir)
+ if output:
+ print output,
+ sys.exit(1)
+
+
+if __name__ == '__main__':
+ main()
Please sign in to comment.
Something went wrong with that request. Please try again.