1#!/usr/bin/env python 2 3import glob 4import os 5import sys 6 7os.environ['PYFLAKES_NODOCTEST'] = '1' 8 9# pycodestyle.py uses sys.argv to find setup.cfg 10sys.argv = [os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)] 11 12# git usurbs your bin path for hooks and will always run system python 13if 'VIRTUAL_ENV' in os.environ: 14 site_packages = glob.glob( 15 '%s/lib/*/site-packages' % os.environ['VIRTUAL_ENV'])[0] 16 sys.path.insert(0, site_packages) 17 18 19def main(): 20 from flake8.api.legacy import get_style_guide 21 from flake8.hooks import run 22 23 gitcmd = "git diff-index --cached --name-only HEAD" 24 25 _, files_modified, _ = run(gitcmd) 26 27 try: 28 text_type = unicode 29 except NameError: 30 text_type = str 31 32 files_modified = [text_type(x) for x in files_modified] 33 34 # remove non-py files and files which no longer exist 35 files_modified = filter( 36 lambda x: x.endswith('.py') and os.path.exists(x), 37 files_modified) 38 39 flake8_style = get_style_guide(parse_argv=True) 40 report = flake8_style.check_files(files_modified) 41 42 return report.total_errors != 0 43 44if __name__ == '__main__': 45 sys.exit(main()) 46