Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Git pre-commit hook which runs isort and flake8 on Python files (tested with Python 2.7 and Python 3.3)

View pre-commit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/usr/bin/env PYTHONIOENCODING=utf-8 python
# encoding: utf-8
"""Git pre-commit hook which lints Python, JavaScript, SASS and CSS"""
 
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)
check_linter(['frosted'], py_files)
check_linter(['isort', '-c'], py_files)
 
if '.js' in changed_extensions:
check_linter(['jshint'], filter_ext('.js', changed_files, exclude='.min.'))
 
if '.scss' in changed_extensions:
check_linter(['sass', '--compass', '--no-cache', '--check'], filter_ext('.scss', changed_files))
 
if '.css' in changed_extensions:
check_linter(['csslint'], filter_ext('.css', 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
Something went wrong with that request. Please try again.