Skip to content

Instantly share code, notes, and snippets.

@aaugustin
Created October 29, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaugustin/e2c0369c89685fb5fae7 to your computer and use it in GitHub Desktop.
Save aaugustin/e2c0369c89685fb5fae7 to your computer and use it in GitHub Desktop.
#!/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