Browse Source

Added other script: a Python PEP8 pre-commit hook by lentil

*   https://gist.github.com/lentil/810399/
Alois Mahdal 11 years ago
parent
commit
f03a4d3604
1 changed files with 39 additions and 0 deletions
  1. 39
    0
      other/git-hooks/pre-commit

+ 39
- 0
other/git-hooks/pre-commit View File

@@ -0,0 +1,39 @@
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
+
10
+
11
+def system(*args, **kwargs):
12
+    kwargs.setdefault('stdout', subprocess.PIPE)
13
+    proc = subprocess.Popen(args, **kwargs)
14
+    out, err = proc.communicate()
15
+    return out
16
+
17
+
18
+def main():
19
+    modified = re.compile('^[AM]+\s+(?P<name>.*\.py)', re.MULTILINE)
20
+    files = system('git', 'status', '--porcelain')
21
+    files = modified.findall(files)
22
+
23
+    tempdir = tempfile.mkdtemp()
24
+    for name in files:
25
+        filename = os.path.join(tempdir, name)
26
+        filepath = os.path.dirname(filename)
27
+        if not os.path.exists(filepath):
28
+            os.makedirs(filepath)
29
+        with file(filename, 'w') as f:
30
+            system('git', 'show', ':' + name, stdout=f)
31
+    output = system('pep8', '.', cwd=tempdir)
32
+    shutil.rmtree(tempdir)
33
+    if output:
34
+        print output,
35
+        sys.exit(1)
36
+
37
+
38
+if __name__ == '__main__':
39
+    main()