##// END OF EJS Templates
Deprecate `utils.sysinfo.num_cpus`...
Nikita Kniazev -
Show More
@@ -1,166 +1,142 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Utilities for getting information about IPython and the system it's running in.
3 Utilities for getting information about IPython and the system it's running in.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import os
17 import os
18 import platform
18 import platform
19 import pprint
19 import pprint
20 import sys
20 import sys
21 import subprocess
21 import subprocess
22
22
23 from IPython.core import release
23 from IPython.core import release
24 from IPython.utils import _sysinfo, encoding
24 from IPython.utils import _sysinfo, encoding
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Code
27 # Code
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 def pkg_commit_hash(pkg_path):
30 def pkg_commit_hash(pkg_path):
31 """Get short form of commit hash given directory `pkg_path`
31 """Get short form of commit hash given directory `pkg_path`
32
32
33 We get the commit hash from (in order of preference):
33 We get the commit hash from (in order of preference):
34
34
35 * IPython.utils._sysinfo.commit
35 * IPython.utils._sysinfo.commit
36 * git output, if we are in a git repository
36 * git output, if we are in a git repository
37
37
38 If these fail, we return a not-found placeholder tuple
38 If these fail, we return a not-found placeholder tuple
39
39
40 Parameters
40 Parameters
41 ----------
41 ----------
42 pkg_path : str
42 pkg_path : str
43 directory containing package
43 directory containing package
44 only used for getting commit from active repo
44 only used for getting commit from active repo
45
45
46 Returns
46 Returns
47 -------
47 -------
48 hash_from : str
48 hash_from : str
49 Where we got the hash from - description
49 Where we got the hash from - description
50 hash_str : str
50 hash_str : str
51 short form of hash
51 short form of hash
52 """
52 """
53 # Try and get commit from written commit text file
53 # Try and get commit from written commit text file
54 if _sysinfo.commit:
54 if _sysinfo.commit:
55 return "installation", _sysinfo.commit
55 return "installation", _sysinfo.commit
56
56
57 # maybe we are in a repository
57 # maybe we are in a repository
58 proc = subprocess.Popen('git rev-parse --short HEAD'.split(' '),
58 proc = subprocess.Popen('git rev-parse --short HEAD'.split(' '),
59 stdout=subprocess.PIPE,
59 stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE,
60 stderr=subprocess.PIPE,
61 cwd=pkg_path)
61 cwd=pkg_path)
62 repo_commit, _ = proc.communicate()
62 repo_commit, _ = proc.communicate()
63 if repo_commit:
63 if repo_commit:
64 return 'repository', repo_commit.strip().decode('ascii')
64 return 'repository', repo_commit.strip().decode('ascii')
65 return '(none found)', '<not found>'
65 return '(none found)', '<not found>'
66
66
67
67
68 def pkg_info(pkg_path):
68 def pkg_info(pkg_path):
69 """Return dict describing the context of this package
69 """Return dict describing the context of this package
70
70
71 Parameters
71 Parameters
72 ----------
72 ----------
73 pkg_path : str
73 pkg_path : str
74 path containing __init__.py for package
74 path containing __init__.py for package
75
75
76 Returns
76 Returns
77 -------
77 -------
78 context : dict
78 context : dict
79 with named parameters of interest
79 with named parameters of interest
80 """
80 """
81 src, hsh = pkg_commit_hash(pkg_path)
81 src, hsh = pkg_commit_hash(pkg_path)
82 return dict(
82 return dict(
83 ipython_version=release.version,
83 ipython_version=release.version,
84 ipython_path=pkg_path,
84 ipython_path=pkg_path,
85 commit_source=src,
85 commit_source=src,
86 commit_hash=hsh,
86 commit_hash=hsh,
87 sys_version=sys.version,
87 sys_version=sys.version,
88 sys_executable=sys.executable,
88 sys_executable=sys.executable,
89 sys_platform=sys.platform,
89 sys_platform=sys.platform,
90 platform=platform.platform(),
90 platform=platform.platform(),
91 os_name=os.name,
91 os_name=os.name,
92 default_encoding=encoding.DEFAULT_ENCODING,
92 default_encoding=encoding.DEFAULT_ENCODING,
93 )
93 )
94
94
95 def get_sys_info():
95 def get_sys_info():
96 """Return useful information about IPython and the system, as a dict."""
96 """Return useful information about IPython and the system, as a dict."""
97 p = os.path
97 p = os.path
98 path = p.realpath(p.dirname(p.abspath(p.join(__file__, '..'))))
98 path = p.realpath(p.dirname(p.abspath(p.join(__file__, '..'))))
99 return pkg_info(path)
99 return pkg_info(path)
100
100
101 def sys_info():
101 def sys_info():
102 """Return useful information about IPython and the system, as a string.
102 """Return useful information about IPython and the system, as a string.
103
103
104 Examples
104 Examples
105 --------
105 --------
106 ::
106 ::
107
107
108 In [2]: print(sys_info())
108 In [2]: print(sys_info())
109 {'commit_hash': '144fdae', # random
109 {'commit_hash': '144fdae', # random
110 'commit_source': 'repository',
110 'commit_source': 'repository',
111 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
111 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
112 'ipython_version': '0.11.dev',
112 'ipython_version': '0.11.dev',
113 'os_name': 'posix',
113 'os_name': 'posix',
114 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
114 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
115 'sys_executable': '/usr/bin/python',
115 'sys_executable': '/usr/bin/python',
116 'sys_platform': 'linux2',
116 'sys_platform': 'linux2',
117 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
117 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
118 """
118 """
119 return pprint.pformat(get_sys_info())
119 return pprint.pformat(get_sys_info())
120
120
121 def _num_cpus_unix():
122 """Return the number of active CPUs on a Unix system."""
123 return os.sysconf("SC_NPROCESSORS_ONLN")
124
125
126 def _num_cpus_darwin():
127 """Return the number of active CPUs on a Darwin system."""
128 p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
129 return p.stdout.read()
130
131
132 def _num_cpus_windows():
133 """Return the number of active CPUs on a Windows system."""
134 return os.environ.get("NUMBER_OF_PROCESSORS")
135
136
121
137 def num_cpus():
122 def num_cpus():
138 """Return the effective number of CPUs in the system as an integer.
123 """DEPRECATED
139
140 This cross-platform function makes an attempt at finding the total number of
141 available CPUs in the system, as returned by various underlying system and
142 python calls.
143
124
144 If it can't find a sensible answer, it returns 1 (though an error *may* make
125 Return the effective number of CPUs in the system as an integer.
145 it return a large positive number that's actually incorrect).
146 """
147
126
148 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
127 This cross-platform function makes an attempt at finding the total number of
149 # for the names of the keys we needed to look up for this function. This
128 available CPUs in the system, as returned by various underlying system and
150 # code was inspired by their equivalent function.
129 python calls.
151
130
152 ncpufuncs = {'Linux':_num_cpus_unix,
131 If it can't find a sensible answer, it returns 1 (though an error *may* make
153 'Darwin':_num_cpus_darwin,
132 it return a large positive number that's actually incorrect).
154 'Windows':_num_cpus_windows
133 """
155 }
134 import warnings
156
157 ncpufunc = ncpufuncs.get(platform.system(),
158 # default to unix version (Solaris, AIX, etc)
159 _num_cpus_unix)
160
135
161 try:
136 warnings.warn(
162 ncpus = max(1,int(ncpufunc()))
137 "`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.",
163 except:
138 DeprecationWarning,
164 ncpus = 1
139 stacklevel=2,
165 return ncpus
140 )
166
141
142 return os.cpu_count() or 1
@@ -1,16 +1,22 b''
1 # coding: utf-8
1 # coding: utf-8
2 """Test suite for our sysinfo utilities."""
2 """Test suite for our sysinfo utilities."""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import json
7 import json
8 import pytest
8
9
9 from IPython.utils import sysinfo
10 from IPython.utils import sysinfo
10
11
11
12
12 def test_json_getsysinfo():
13 def test_json_getsysinfo():
13 """
14 """
14 test that it is easily jsonable and don't return bytes somewhere.
15 test that it is easily jsonable and don't return bytes somewhere.
15 """
16 """
16 json.dumps(sysinfo.get_sys_info())
17 json.dumps(sysinfo.get_sys_info())
18
19
20 def test_num_cpus():
21 with pytest.deprecated_call():
22 sysinfo.num_cpus()
General Comments 0
You need to be logged in to leave comments. Login now