pre-commit 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. from __future__ import absolute_import
  3. import os
  4. import sys
  5. from glob import glob
  6. from subprocess import Popen
  7. from sentry.lint.engine import check_files, get_js_files
  8. text_type = type(u'')
  9. # git usurbs your bin path for hooks and will always run system python
  10. if 'VIRTUAL_ENV' in os.environ:
  11. site_packages = glob(
  12. '%s/lib/*/site-packages' % os.environ['VIRTUAL_ENV'])[0]
  13. sys.path.insert(0, site_packages)
  14. def js_format(file_list=None):
  15. """
  16. We only format JavaScript code as part of this pre-commit hook. It is not part
  17. of the lint engine.
  18. """
  19. project_root = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
  20. prettier_path = os.path.join(project_root, 'node_modules', '.bin', 'prettier')
  21. if not os.path.exists(prettier_path):
  22. from click import echo
  23. echo('!! Skipping JavaScript formatting because prettier is not installed.')
  24. return False
  25. js_file_list = get_js_files(file_list)
  26. has_errors = False
  27. if file_list:
  28. status = Popen([prettier_path, '--write', '--single-quote', '--bracket-spacing=false', '--print-width=90']
  29. + js_file_list).wait()
  30. has_errors = status != 0
  31. return has_errors
  32. def main():
  33. from flake8.hooks import run
  34. gitcmd = "git diff-index --cached --name-only HEAD"
  35. _, files_modified, _ = run(gitcmd)
  36. files_modified = [
  37. text_type(f)
  38. for f in files_modified
  39. if os.path.exists(f)
  40. ]
  41. # Prettier formatting must take place before linting
  42. js_format(files_modified)
  43. return check_files(files_modified)
  44. if __name__ == '__main__':
  45. sys.exit(main())