help.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import json
  2. import platform
  3. import sys
  4. from . import __version__ as pyjwt_version
  5. try:
  6. import cryptography
  7. except ModuleNotFoundError:
  8. cryptography = None # type: ignore
  9. def info():
  10. """
  11. Generate information for a bug report.
  12. Based on the requests package help utility module.
  13. """
  14. try:
  15. platform_info = {
  16. "system": platform.system(),
  17. "release": platform.release(),
  18. }
  19. except OSError:
  20. platform_info = {"system": "Unknown", "release": "Unknown"}
  21. implementation = platform.python_implementation()
  22. if implementation == "CPython":
  23. implementation_version = platform.python_version()
  24. elif implementation == "PyPy":
  25. implementation_version = (
  26. f"{sys.pypy_version_info.major}."
  27. f"{sys.pypy_version_info.minor}."
  28. f"{sys.pypy_version_info.micro}"
  29. )
  30. if sys.pypy_version_info.releaselevel != "final":
  31. implementation_version = "".join(
  32. [implementation_version, sys.pypy_version_info.releaselevel]
  33. )
  34. else:
  35. implementation_version = "Unknown"
  36. return {
  37. "platform": platform_info,
  38. "implementation": {
  39. "name": implementation,
  40. "version": implementation_version,
  41. },
  42. "cryptography": {"version": getattr(cryptography, "__version__", "")},
  43. "pyjwt": {"version": pyjwt_version},
  44. }
  45. def main():
  46. """Pretty-print the bug information as JSON."""
  47. print(json.dumps(info(), sort_keys=True, indent=2))
  48. if __name__ == "__main__":
  49. main()