help.py 3.4 KB

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