help.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Module containing bug report helper(s)."""
  2. import json
  3. import platform
  4. import ssl
  5. import sys
  6. from pip._vendor import idna
  7. from pip._vendor import urllib3
  8. from . import __version__ as requests_version
  9. charset_normalizer = None
  10. chardet = None
  11. try:
  12. from pip._vendor.urllib3.contrib import pyopenssl
  13. except ImportError:
  14. pyopenssl = None
  15. OpenSSL = None
  16. cryptography = None
  17. else:
  18. import cryptography
  19. import OpenSSL
  20. def _implementation():
  21. """Return a dict with the Python implementation and version.
  22. Provide both the name and the version of the Python implementation
  23. currently running. For example, on CPython 3.10.3 it will return
  24. {'name': 'CPython', 'version': '3.10.3'}.
  25. This function works best on CPython and PyPy: in particular, it probably
  26. doesn't work for Jython or IronPython. Future investigation should be done
  27. to work out the correct shape of the code for those platforms.
  28. """
  29. implementation = platform.python_implementation()
  30. if implementation == "CPython":
  31. implementation_version = platform.python_version()
  32. elif implementation == "PyPy":
  33. implementation_version = "{}.{}.{}".format(
  34. sys.pypy_version_info.major,
  35. sys.pypy_version_info.minor,
  36. sys.pypy_version_info.micro,
  37. )
  38. if sys.pypy_version_info.releaselevel != "final":
  39. implementation_version = "".join(
  40. [implementation_version, sys.pypy_version_info.releaselevel]
  41. )
  42. elif implementation == "Jython":
  43. implementation_version = platform.python_version() # Complete Guess
  44. elif implementation == "IronPython":
  45. implementation_version = platform.python_version() # Complete Guess
  46. else:
  47. implementation_version = "Unknown"
  48. return {"name": implementation, "version": implementation_version}
  49. def info():
  50. """Generate information for a bug report."""
  51. try:
  52. platform_info = {
  53. "system": platform.system(),
  54. "release": platform.release(),
  55. }
  56. except OSError:
  57. platform_info = {
  58. "system": "Unknown",
  59. "release": "Unknown",
  60. }
  61. implementation_info = _implementation()
  62. urllib3_info = {"version": urllib3.__version__}
  63. charset_normalizer_info = {"version": None}
  64. chardet_info = {"version": None}
  65. if charset_normalizer:
  66. charset_normalizer_info = {"version": charset_normalizer.__version__}
  67. if chardet:
  68. chardet_info = {"version": chardet.__version__}
  69. pyopenssl_info = {
  70. "version": None,
  71. "openssl_version": "",
  72. }
  73. if OpenSSL:
  74. pyopenssl_info = {
  75. "version": OpenSSL.__version__,
  76. "openssl_version": f"{OpenSSL.SSL.OPENSSL_VERSION_NUMBER:x}",
  77. }
  78. cryptography_info = {
  79. "version": getattr(cryptography, "__version__", ""),
  80. }
  81. idna_info = {
  82. "version": getattr(idna, "__version__", ""),
  83. }
  84. system_ssl = ssl.OPENSSL_VERSION_NUMBER
  85. system_ssl_info = {"version": f"{system_ssl:x}" if system_ssl is not None else ""}
  86. return {
  87. "platform": platform_info,
  88. "implementation": implementation_info,
  89. "system_ssl": system_ssl_info,
  90. "using_pyopenssl": pyopenssl is not None,
  91. "using_charset_normalizer": chardet is None,
  92. "pyOpenSSL": pyopenssl_info,
  93. "urllib3": urllib3_info,
  94. "chardet": chardet_info,
  95. "charset_normalizer": charset_normalizer_info,
  96. "cryptography": cryptography_info,
  97. "idna": idna_info,
  98. "requests": {
  99. "version": requests_version,
  100. },
  101. }
  102. def main():
  103. """Pretty-print the bug information as JSON."""
  104. print(json.dumps(info(), sort_keys=True, indent=2))
  105. if __name__ == "__main__":
  106. main()