Last active
December 13, 2015 20:28
-
-
Save Tilka/4970334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import subprocess | |
import tempfile | |
def system(args, **kwargs): | |
environ = os.environ.copy() | |
if 'repo' in kwargs: | |
environ['GIT_DIR'] = kwargs['repo'] | |
if 'work' in kwargs: | |
environ['GIT_WORK_TREE'] = kwargs['work'] | |
try: | |
proc = subprocess.Popen(args, stdout=subprocess.PIPE, env=environ) | |
return proc.communicate() | |
except Exception, e: | |
return e.message | |
def get_changed_files(base, commit, **kw): | |
(results, code) = system(('git', 'diff', '--name-only', "%s..%s" % (base, commit)), **kw) | |
return results.strip().split('\n') | |
def get_new_file(filename, commit): | |
(results, code) = system(('git', 'show', '%s:%s' % (commit, filename))) | |
return results | |
def pep8(filename, contents): | |
with tempfile.NamedTemporaryFile(delete=False) as f: | |
f.write(contents) | |
fname = f.name | |
results, code = system(('pep8', '-r', '--ignore=E501', fname)) | |
os.unlink(fname) | |
return results.strip().replace(fname, filename) | |
repo = os.getcwd() | |
basedir = os.path.join(repo, "..") | |
line = sys.stdin.read() | |
(base, commit, ref) = line.strip().split() | |
modified = get_changed_files(base, commit) | |
flag = True | |
for fname in modified: | |
if fname[-3:] == '.py': | |
print >> sys.stderr, "[PEP8]", fname | |
contents = get_new_file(fname, commit) | |
errmsg = pep8(fname, contents) | |
print errmsg | |
if len(errmsg): | |
flag = False | |
if not flag: | |
sys.exit(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment