pre-commit 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from glob import glob
  5. # If we're using Python 2, that means that direnv has not been activated
  6. if sys.version_info.major < 3:
  7. sys.stderr.write(
  8. "ERROR: You're running an invalid Python version. This is likely because you have pulled\n"
  9. "new changes that modify .envrc, and you must execute: `direnv allow`\n"
  10. "To re-enter the sentry Python virtual environment\n"
  11. )
  12. sys.exit(1)
  13. try:
  14. import sentry_sdk
  15. if not os.environ.get("SENTRY_DEVENV_NO_REPORT"):
  16. sentry_sdk.init(dsn="https://9bdb053cb8274ea69231834d1edeec4c@o1.ingest.sentry.io/5723503")
  17. except ModuleNotFoundError:
  18. sys.stdout.write(
  19. "WARNING: Sentry SDK not installed, thus, errors will not be reported. Run: make install-py-dev\n"
  20. )
  21. text_type = str
  22. # git usurps your bin path for hooks and will always run system python
  23. if "VIRTUAL_ENV" in os.environ:
  24. # If pre-commit is not installed outside of the virtualenv, glob will return []
  25. try:
  26. site_packages = glob("%s/lib/*/site-packages" % os.environ["VIRTUAL_ENV"])[0]
  27. sys.path.insert(0, site_packages)
  28. except IndexError:
  29. pass
  30. def main():
  31. try:
  32. from sentry.lint.engine import get_modified_files, run
  33. except ModuleNotFoundError:
  34. if "VIRTUAL_ENV" not in os.environ:
  35. sys.stderr.write(
  36. "ERROR: You're executing outside of the venv. Try this command: direnv allow\n"
  37. )
  38. sys.exit(1)
  39. sys.stdout.write("ERROR: You may be able to fix this by executing: make install-py-dev.\n")
  40. raise
  41. files_modified = [text_type(f) for f in get_modified_files(os.getcwd()) if os.path.exists(f)]
  42. return run(files_modified)
  43. if __name__ == "__main__":
  44. sys.exit(main())