help.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. import chardet
  10. from . import __version__ as requests_version
  11. try:
  12. from urllib3.contrib import pyopenssl
  13. except ImportError:
  14. pyopenssl = None
  15. OpenSSL = None
  16. cryptography = None
  17. else:
  18. import OpenSSL
  19. import cryptography
  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 2.7.5 it will return
  24. {'name': 'CPython', 'version': '2.7.5'}.
  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 = '%s.%s.%s' % (sys.pypy_version_info.major,
  34. sys.pypy_version_info.minor,
  35. sys.pypy_version_info.micro)
  36. if sys.pypy_version_info.releaselevel != 'final':
  37. implementation_version = ''.join([
  38. implementation_version, sys.pypy_version_info.releaselevel
  39. ])
  40. elif implementation == 'Jython':
  41. implementation_version = platform.python_version() # Complete Guess
  42. elif implementation == 'IronPython':
  43. implementation_version = platform.python_version() # Complete Guess
  44. else:
  45. implementation_version = 'Unknown'
  46. return {'name': implementation, 'version': implementation_version}
  47. def info():
  48. """Generate information for a bug report."""
  49. try:
  50. platform_info = {
  51. 'system': platform.system(),
  52. 'release': platform.release(),
  53. }
  54. except IOError:
  55. platform_info = {
  56. 'system': 'Unknown',
  57. 'release': 'Unknown',
  58. }
  59. implementation_info = _implementation()
  60. urllib3_info = {'version': urllib3.__version__}
  61. chardet_info = {'version': chardet.__version__}
  62. pyopenssl_info = {
  63. 'version': None,
  64. 'openssl_version': '',
  65. }
  66. if OpenSSL:
  67. pyopenssl_info = {
  68. 'version': OpenSSL.__version__,
  69. 'openssl_version': '%x' % OpenSSL.SSL.OPENSSL_VERSION_NUMBER,
  70. }
  71. cryptography_info = {
  72. 'version': getattr(cryptography, '__version__', ''),
  73. }
  74. idna_info = {
  75. 'version': getattr(idna, '__version__', ''),
  76. }
  77. system_ssl = ssl.OPENSSL_VERSION_NUMBER
  78. system_ssl_info = {
  79. 'version': '%x' % system_ssl if system_ssl is not None else ''
  80. }
  81. return {
  82. 'platform': platform_info,
  83. 'implementation': implementation_info,
  84. 'system_ssl': system_ssl_info,
  85. 'using_pyopenssl': pyopenssl is not None,
  86. 'pyOpenSSL': pyopenssl_info,
  87. 'urllib3': urllib3_info,
  88. 'chardet': chardet_info,
  89. 'cryptography': cryptography_info,
  90. 'idna': idna_info,
  91. 'requests': {
  92. 'version': requests_version,
  93. },
  94. }
  95. def main():
  96. """Pretty-print the bug information as JSON."""
  97. print(json.dumps(info(), sort_keys=True, indent=2))
  98. if __name__ == '__main__':
  99. main()