sysinfo.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 IPython.core import release
  20. from IPython.utils import py3compat, _sysinfo, encoding
  21. #-----------------------------------------------------------------------------
  22. # Code
  23. #-----------------------------------------------------------------------------
  24. def pkg_commit_hash(pkg_path):
  25. """Get short form of commit hash given directory `pkg_path`
  26. We get the commit hash from (in order of preference):
  27. * IPython.utils._sysinfo.commit
  28. * git output, if we are in a git repository
  29. If these fail, we return a not-found placeholder tuple
  30. Parameters
  31. ----------
  32. pkg_path : str
  33. directory containing package
  34. only used for getting commit from active repo
  35. Returns
  36. -------
  37. hash_from : str
  38. Where we got the hash from - description
  39. hash_str : str
  40. short form of hash
  41. """
  42. # Try and get commit from written commit text file
  43. if _sysinfo.commit:
  44. return "installation", _sysinfo.commit
  45. # maybe we are in a repository
  46. proc = subprocess.Popen('git rev-parse --short HEAD',
  47. stdout=subprocess.PIPE,
  48. stderr=subprocess.PIPE,
  49. cwd=pkg_path, shell=True)
  50. repo_commit, _ = proc.communicate()
  51. if repo_commit:
  52. return 'repository', repo_commit.strip().decode('ascii')
  53. return '(none found)', u'<not found>'
  54. def pkg_info(pkg_path):
  55. """Return dict describing the context of this package
  56. Parameters
  57. ----------
  58. pkg_path : str
  59. path containing __init__.py for package
  60. Returns
  61. -------
  62. context : dict
  63. with named parameters of interest
  64. """
  65. src, hsh = pkg_commit_hash(pkg_path)
  66. return dict(
  67. ipython_version=release.version,
  68. ipython_path=pkg_path,
  69. commit_source=src,
  70. commit_hash=hsh,
  71. sys_version=sys.version,
  72. sys_executable=sys.executable,
  73. sys_platform=sys.platform,
  74. platform=platform.platform(),
  75. os_name=os.name,
  76. default_encoding=encoding.DEFAULT_ENCODING,
  77. )
  78. def get_sys_info():
  79. """Return useful information about IPython and the system, as a dict."""
  80. p = os.path
  81. path = p.realpath(p.dirname(p.abspath(p.join(__file__, '..'))))
  82. return pkg_info(path)
  83. @py3compat.doctest_refactor_print
  84. def sys_info():
  85. """Return useful information about IPython and the system, as a string.
  86. Examples
  87. --------
  88. ::
  89. In [2]: print sys_info()
  90. {'commit_hash': '144fdae', # random
  91. 'commit_source': 'repository',
  92. 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
  93. 'ipython_version': '0.11.dev',
  94. 'os_name': 'posix',
  95. 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
  96. 'sys_executable': '/usr/bin/python',
  97. 'sys_platform': 'linux2',
  98. 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
  99. """
  100. return pprint.pformat(get_sys_info())
  101. def _num_cpus_unix():
  102. """Return the number of active CPUs on a Unix system."""
  103. return os.sysconf("SC_NPROCESSORS_ONLN")
  104. def _num_cpus_darwin():
  105. """Return the number of active CPUs on a Darwin system."""
  106. p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
  107. return p.stdout.read()
  108. def _num_cpus_windows():
  109. """Return the number of active CPUs on a Windows system."""
  110. return os.environ.get("NUMBER_OF_PROCESSORS")
  111. def num_cpus():
  112. """Return the effective number of CPUs in the system as an integer.
  113. This cross-platform function makes an attempt at finding the total number of
  114. available CPUs in the system, as returned by various underlying system and
  115. python calls.
  116. If it can't find a sensible answer, it returns 1 (though an error *may* make
  117. it return a large positive number that's actually incorrect).
  118. """
  119. # Many thanks to the Parallel Python project (http://www.parallelpython.com)
  120. # for the names of the keys we needed to look up for this function. This
  121. # code was inspired by their equivalent function.
  122. ncpufuncs = {'Linux':_num_cpus_unix,
  123. 'Darwin':_num_cpus_darwin,
  124. 'Windows':_num_cpus_windows
  125. }
  126. ncpufunc = ncpufuncs.get(platform.system(),
  127. # default to unix version (Solaris, AIX, etc)
  128. _num_cpus_unix)
  129. try:
  130. ncpus = max(1,int(ncpufunc()))
  131. except:
  132. ncpus = 1
  133. return ncpus