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