sysinfo.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # encoding: utf-8
  2. """
  3. Utilities for getting information about IPython and the system it's running in.
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2008-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import os
  15. import platform
  16. import pprint
  17. import sys
  18. import subprocess
  19. from pathlib import Path
  20. from IPython.core import release
  21. from IPython.utils import _sysinfo, encoding
  22. #-----------------------------------------------------------------------------
  23. # Code
  24. #-----------------------------------------------------------------------------
  25. def pkg_commit_hash(pkg_path: str) -> tuple[str, str]:
  26. """Get short form of commit hash given directory `pkg_path`
  27. We get the commit hash from (in order of preference):
  28. * IPython.utils._sysinfo.commit
  29. * git output, if we are in a git repository
  30. If these fail, we return a not-found placeholder tuple
  31. Parameters
  32. ----------
  33. pkg_path : str
  34. directory containing package
  35. only used for getting commit from active repo
  36. Returns
  37. -------
  38. hash_from : str
  39. Where we got the hash from - description
  40. hash_str : str
  41. short form of hash
  42. """
  43. # Try and get commit from written commit text file
  44. if _sysinfo.commit:
  45. return "installation", _sysinfo.commit
  46. # maybe we are in a repository
  47. proc = subprocess.Popen('git rev-parse --short HEAD'.split(' '),
  48. stdout=subprocess.PIPE,
  49. stderr=subprocess.PIPE,
  50. cwd=pkg_path)
  51. repo_commit, _ = proc.communicate()
  52. if repo_commit:
  53. return 'repository', repo_commit.strip().decode('ascii')
  54. return '(none found)', '<not found>'
  55. def pkg_info(pkg_path: str) -> dict:
  56. """Return dict describing the context of this package
  57. Parameters
  58. ----------
  59. pkg_path : str
  60. path containing __init__.py for package
  61. Returns
  62. -------
  63. context : dict
  64. with named parameters of interest
  65. """
  66. src, hsh = pkg_commit_hash(pkg_path)
  67. return dict(
  68. ipython_version=release.version,
  69. ipython_path=pkg_path,
  70. commit_source=src,
  71. commit_hash=hsh,
  72. sys_version=sys.version,
  73. sys_executable=sys.executable,
  74. sys_platform=sys.platform,
  75. platform=platform.platform(),
  76. os_name=os.name,
  77. default_encoding=encoding.DEFAULT_ENCODING,
  78. )
  79. def get_sys_info() -> dict:
  80. """Return useful information about IPython and the system, as a dict."""
  81. path = Path(__file__, "..").resolve().parent
  82. return pkg_info(str(path))
  83. def sys_info():
  84. """Return useful information about IPython and the system, as a string.
  85. Examples
  86. --------
  87. ::
  88. In [2]: print(sys_info())
  89. {'commit_hash': '144fdae', # random
  90. 'commit_source': 'repository',
  91. 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
  92. 'ipython_version': '0.11.dev',
  93. 'os_name': 'posix',
  94. 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
  95. 'sys_executable': '/usr/bin/python',
  96. 'sys_platform': 'linux2',
  97. 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
  98. """
  99. return pprint.pformat(get_sys_info())
  100. def num_cpus():
  101. """DEPRECATED
  102. Return the effective number of CPUs in the system as an integer.
  103. This cross-platform function makes an attempt at finding the total number of
  104. available CPUs in the system, as returned by various underlying system and
  105. python calls.
  106. If it can't find a sensible answer, it returns 1 (though an error *may* make
  107. it return a large positive number that's actually incorrect).
  108. """
  109. import warnings
  110. warnings.warn(
  111. "`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.",
  112. DeprecationWarning,
  113. stacklevel=2,
  114. )
  115. return os.cpu_count() or 1