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