Created
October 29, 2015 20:48
-
-
Save aaugustin/e2c0369c89685fb5fae7 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 python3.4 | |
"""Git pre-commit hook""" | |
import os | |
import subprocess | |
import sys | |
def run_linter(cmd, files): | |
return subprocess.check_output(cmd + files, | |
stderr=subprocess.STDOUT, universal_newlines=True) | |
def lint_files(changed_files): | |
py_files = [file for file in changed_files if file.endswith('.py')] | |
if py_files: | |
run_linter(['flake8'], py_files) | |
run_linter(['isort', '-c'], py_files) | |
if __name__ == "__main__": | |
os.chdir(os.path.join(os.path.dirname(__file__), '..', '..')) | |
changed_files = subprocess.check_output( | |
'git diff --cached --name-only --diff-filter=ACM', | |
shell=True, universal_newlines=True) | |
changed_files = [file.strip() for file in changed_files.splitlines()] | |
try: | |
lint_files(changed_files) | |
except subprocess.CalledProcessError as exc: | |
print('Quality check failed:', file=sys.stderr) | |
print(' '.join(exc.cmd), file=sys.stderr) | |
if exc.output: | |
print('\t', '\n\t'.join(exc.output.splitlines()), sep='', file=sys.stderr) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment