help.py 3.9 KB

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