Skip to content

Instantly share code, notes, and snippets.

@Lemmons
Last active August 29, 2015 14:25
Show Gist options
  • Save Lemmons/48a76c5fe2b8ca2ec758 to your computer and use it in GitHub Desktop.
Save Lemmons/48a76c5fe2b8ca2ec758 to your computer and use it in GitHub Desktop.
A cool git pre-commit hook for python (auto pylints and isorts)
#!/usr/bin/env python
# encoding: utf-8
from __future__ import absolute_import, print_function, unicode_literals
import os
import subprocess
import sys
FS_ENCODING = sys.getfilesystemencoding()
def check_linter(cmd, files, **kwargs):
if not files:
return
print('Running %s' % cmd[0])
return subprocess.check_output(cmd + files, stderr=subprocess.STDOUT, **kwargs).decode(FS_ENCODING)
def filter_ext(extension, files, exclude=None):
files = [f for f in files if f.endswith(extension)]
if exclude is not None:
files = [i for i in files if exclude not in i]
return files
def lint_files(changed_files):
changed_files = [i.strip() for i in changed_files.splitlines() if '/external/' not in i]
changed_extensions = {ext for root, ext in map(os.path.splitext, changed_files)}
if '.py' in changed_extensions:
py_files = filter_ext('.py', changed_files, exclude='alembic/versions')
check_linter(['pylint', '--reports=no', '--rcfile=tests/pylint.cfg'], py_files)
check_linter(['isort', '-c', '-ns', '__init__.py'], py_files)
# if '.js' in changed_extensions:
# check_linter(['jshint'], filter_ext('.js', changed_files, exclude='.min.'))
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'.split())
changed_files = changed_files.decode(FS_ENCODING)
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:
output = exc.output.decode(FS_ENCODING)
print('\t', '\n\t'.join(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