##// END OF EJS Templates
refactor: update type hints in utils/sysinfo.py
Martin Matějek -
Show More
@@ -1,143 +1,143 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 pathlib import Path
23 from pathlib import Path
24
24
25 from IPython.core import release
25 from IPython.core import release
26 from IPython.utils import _sysinfo, encoding
26 from IPython.utils import _sysinfo, encoding
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Code
29 # Code
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 def pkg_commit_hash(pkg_path):
32 def pkg_commit_hash(pkg_path: str) -> tuple[str, str]:
33 """Get short form of commit hash given directory `pkg_path`
33 """Get short form of commit hash given directory `pkg_path`
34
34
35 We get the commit hash from (in order of preference):
35 We get the commit hash from (in order of preference):
36
36
37 * IPython.utils._sysinfo.commit
37 * IPython.utils._sysinfo.commit
38 * git output, if we are in a git repository
38 * git output, if we are in a git repository
39
39
40 If these fail, we return a not-found placeholder tuple
40 If these fail, we return a not-found placeholder tuple
41
41
42 Parameters
42 Parameters
43 ----------
43 ----------
44 pkg_path : str
44 pkg_path : str
45 directory containing package
45 directory containing package
46 only used for getting commit from active repo
46 only used for getting commit from active repo
47
47
48 Returns
48 Returns
49 -------
49 -------
50 hash_from : str
50 hash_from : str
51 Where we got the hash from - description
51 Where we got the hash from - description
52 hash_str : str
52 hash_str : str
53 short form of hash
53 short form of hash
54 """
54 """
55 # Try and get commit from written commit text file
55 # Try and get commit from written commit text file
56 if _sysinfo.commit:
56 if _sysinfo.commit:
57 return "installation", _sysinfo.commit
57 return "installation", _sysinfo.commit
58
58
59 # maybe we are in a repository
59 # maybe we are in a repository
60 proc = subprocess.Popen('git rev-parse --short HEAD'.split(' '),
60 proc = subprocess.Popen('git rev-parse --short HEAD'.split(' '),
61 stdout=subprocess.PIPE,
61 stdout=subprocess.PIPE,
62 stderr=subprocess.PIPE,
62 stderr=subprocess.PIPE,
63 cwd=pkg_path)
63 cwd=pkg_path)
64 repo_commit, _ = proc.communicate()
64 repo_commit, _ = proc.communicate()
65 if repo_commit:
65 if repo_commit:
66 return 'repository', repo_commit.strip().decode('ascii')
66 return 'repository', repo_commit.strip().decode('ascii')
67 return '(none found)', '<not found>'
67 return '(none found)', '<not found>'
68
68
69
69
70 def pkg_info(pkg_path):
70 def pkg_info(pkg_path: str) -> dict:
71 """Return dict describing the context of this package
71 """Return dict describing the context of this package
72
72
73 Parameters
73 Parameters
74 ----------
74 ----------
75 pkg_path : str
75 pkg_path : str
76 path containing __init__.py for package
76 path containing __init__.py for package
77
77
78 Returns
78 Returns
79 -------
79 -------
80 context : dict
80 context : dict
81 with named parameters of interest
81 with named parameters of interest
82 """
82 """
83 src, hsh = pkg_commit_hash(pkg_path)
83 src, hsh = pkg_commit_hash(pkg_path)
84 return dict(
84 return dict(
85 ipython_version=release.version,
85 ipython_version=release.version,
86 ipython_path=pkg_path,
86 ipython_path=pkg_path,
87 commit_source=src,
87 commit_source=src,
88 commit_hash=hsh,
88 commit_hash=hsh,
89 sys_version=sys.version,
89 sys_version=sys.version,
90 sys_executable=sys.executable,
90 sys_executable=sys.executable,
91 sys_platform=sys.platform,
91 sys_platform=sys.platform,
92 platform=platform.platform(),
92 platform=platform.platform(),
93 os_name=os.name,
93 os_name=os.name,
94 default_encoding=encoding.DEFAULT_ENCODING,
94 default_encoding=encoding.DEFAULT_ENCODING,
95 )
95 )
96
96
97 def get_sys_info():
97 def get_sys_info() -> dict:
98 """Return useful information about IPython and the system, as a dict."""
98 """Return useful information about IPython and the system, as a dict."""
99 path = Path(__file__, "..").resolve().parent
99 path = Path(__file__, "..").resolve().parent
100 return pkg_info(str(path))
100 return pkg_info(str(path))
101
101
102 def sys_info():
102 def sys_info():
103 """Return useful information about IPython and the system, as a string.
103 """Return useful information about IPython and the system, as a string.
104
104
105 Examples
105 Examples
106 --------
106 --------
107 ::
107 ::
108
108
109 In [2]: print(sys_info())
109 In [2]: print(sys_info())
110 {'commit_hash': '144fdae', # random
110 {'commit_hash': '144fdae', # random
111 'commit_source': 'repository',
111 'commit_source': 'repository',
112 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
112 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
113 'ipython_version': '0.11.dev',
113 'ipython_version': '0.11.dev',
114 'os_name': 'posix',
114 'os_name': 'posix',
115 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
115 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
116 'sys_executable': '/usr/bin/python',
116 'sys_executable': '/usr/bin/python',
117 'sys_platform': 'linux2',
117 'sys_platform': 'linux2',
118 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
118 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
119 """
119 """
120 return pprint.pformat(get_sys_info())
120 return pprint.pformat(get_sys_info())
121
121
122
122
123 def num_cpus():
123 def num_cpus():
124 """DEPRECATED
124 """DEPRECATED
125
125
126 Return the effective number of CPUs in the system as an integer.
126 Return the effective number of CPUs in the system as an integer.
127
127
128 This cross-platform function makes an attempt at finding the total number of
128 This cross-platform function makes an attempt at finding the total number of
129 available CPUs in the system, as returned by various underlying system and
129 available CPUs in the system, as returned by various underlying system and
130 python calls.
130 python calls.
131
131
132 If it can't find a sensible answer, it returns 1 (though an error *may* make
132 If it can't find a sensible answer, it returns 1 (though an error *may* make
133 it return a large positive number that's actually incorrect).
133 it return a large positive number that's actually incorrect).
134 """
134 """
135 import warnings
135 import warnings
136
136
137 warnings.warn(
137 warnings.warn(
138 "`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.",
138 "`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.",
139 DeprecationWarning,
139 DeprecationWarning,
140 stacklevel=2,
140 stacklevel=2,
141 )
141 )
142
142
143 return os.cpu_count() or 1
143 return os.cpu_count() or 1
General Comments 0
You need to be logged in to leave comments. Login now