pre-commit 974 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from glob import glob
  5. text_type = str
  6. # git usurbs your bin path for hooks and will always run system python
  7. if "VIRTUAL_ENV" in os.environ:
  8. # If pre-commit is not installed outside of the virtualenv, glob will return []
  9. try:
  10. site_packages = glob("%s/lib/*/site-packages" % os.environ["VIRTUAL_ENV"])[0]
  11. sys.path.insert(0, site_packages)
  12. except IndexError:
  13. pass
  14. def main():
  15. try:
  16. from sentry.lint.engine import get_modified_files, run
  17. except ModuleNotFoundError as e:
  18. if "VIRTUAL_ENV" not in os.environ:
  19. sys.stderr.write(
  20. "ERROR: You're executing outside of the venv. Try this command: direnv allow\n"
  21. )
  22. sys.exit(1)
  23. raise (e)
  24. files_modified = [text_type(f) for f in get_modified_files(os.getcwd()) if os.path.exists(f)]
  25. return run(files_modified)
  26. if __name__ == "__main__":
  27. sys.exit(main())