pre-commit 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import sys
  5. os.environ['PYFLAKES_NODOCTEST'] = '1'
  6. # pep8.py uses sys.argv to find setup.cfg
  7. sys.argv = [os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)]
  8. # git usurbs your bin path for hooks and will always run system python
  9. if 'VIRTUAL_ENV' in os.environ:
  10. site_packages = glob.glob(
  11. '%s/lib/*/site-packages' % os.environ['VIRTUAL_ENV'])[0]
  12. sys.path.insert(0, site_packages)
  13. def py_lint(files_modified):
  14. from flake8.main import DEFAULT_CONFIG
  15. from flake8.engine import get_style_guide
  16. # remove non-py files and files which no longer exist
  17. files_modified = filter(lambda x: x.endswith('.py'), files_modified)
  18. flake8_style = get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG)
  19. report = flake8_style.check_files(files_modified)
  20. return report.total_errors != 0
  21. def js_lint(files_modified):
  22. has_errors = False
  23. if os.system('node_modules/.bin/jshint src/sentry'):
  24. has_errors = True
  25. return has_errors
  26. def main():
  27. from flake8.hooks import run
  28. gitcmd = "git diff-index --cached --name-only HEAD"
  29. _, files_modified, _ = run(gitcmd)
  30. files_modified = filter(lambda x: os.path.exists(x), files_modified)
  31. if any((py_lint(files_modified), js_lint(files_modified))):
  32. return 1
  33. return 0
  34. if __name__ == '__main__':
  35. sys.exit(main())