forked from nedbat/pydoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoctor.py
238 lines (192 loc) · 7.19 KB
/
doctor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/pydoctor/blob/master/NOTICE.txt
"""Show useful things about a Python installation.
Can be used without installation:
wget -qO - http://bit.ly/pydoctor | python -
"""
from __future__ import print_function, unicode_literals
import contextlib
import locale
import os
import os.path
import re
import platform
import sys
DOCTOR_VERSION = 5
SECTIONS = []
SECTION_MAP = {}
def section(name):
"""Declare a function for handling a section of the diagnosis."""
SECTIONS.append(name)
def decorator(func):
SECTION_MAP[name] = func
return func
return decorator
print_ = print
INDENT = 0
def print(*stuff):
"""Our own indent-aware print function."""
if INDENT:
print_(" "*INDENT, end="")
print_(*stuff)
@contextlib.contextmanager
def indent():
"""Indent nested prints by a certain amount."""
global INDENT
old_indent = INDENT
INDENT += 4
try:
yield None
finally:
INDENT = old_indent
def more_about_file(filename, seen=None):
"""Print more information about a file.
`seen` is a set of filename already seen, for dealing with symlink loops.
"""
seen = seen or set()
with indent():
if not os.path.exists(filename):
print("which doesn't exist")
elif os.path.islink(filename):
link = os.readlink(filename)
print("which is a symlink to: {0!r}".format(link))
if link in seen:
print("which we have seen already")
seen.add(link)
alink = os.path.abspath(os.path.join(os.path.dirname(filename), link))
if alink != link:
with indent():
print("which resolves to: {0!r}".format(alink))
if alink in seen:
print("which we have seen already")
return
seen.add(alink)
link = alink
more_about_file(link, seen)
elif os.path.isdir(filename):
print("which is a directory with {0} entries".format(len(os.listdir(filename))))
else:
print("which is a file with length {0}".format(os.path.getsize(filename)))
def might_be_a_file(text):
"""This text might be a file, and if it seems like it is, show more info."""
if "/" in text or "\\" in text:
more_about_file(text)
@section("version")
def show_version():
"""The version of Python, and other details."""
print("Python version:\n {0}".format(sys.version.replace("\n", "\n ")))
print("Python implementation: {0!r}".format(platform.python_implementation()))
print("Python executable: {0!r}".format(sys.executable))
more_about_file(sys.executable)
print("Python prefix: {0!r}".format(sys.prefix))
more_about_file(sys.prefix)
if hasattr(sys, "real_prefix"):
print("This is a virtualenv.")
with indent():
print("The real prefix is: {0!r}".format(sys.real_prefix))
more_about_file(sys.real_prefix)
else:
print("This is not a virtualenv.")
@section("os")
def show_os():
"""Details of the operating system."""
print("Current directory: {0!r}".format(os.getcwd()))
more_about_file(os.getcwd())
print("Platform: {0!r}".format(platform.platform()))
print("uname: {0!r}".format(platform.uname()))
re_envs = r"^(COVER|NOSE|PEX|PIP|PY|VIRTUALENV|WORKON)"
label = "Environment variables matching {0}".format(re_envs)
envs = [ename for ename in os.environ if re.match(re_envs, ename)]
if envs:
print("{0}:".format(label))
with indent():
for env in sorted(envs):
val = os.environ[env]
print("{0} = {1!r}".format(env, val))
might_be_a_file(val)
if os.pathsep in val:
with indent():
print("looks like a path:")
with indent():
for pathval in val.split(os.pathsep):
print("{0!r}".format(pathval))
might_be_a_file(pathval)
else:
print("{0}: none".format(label))
print("$PATH:")
with indent():
for p in os.environ['PATH'].split(os.pathsep):
print(repr(p))
more_about_file(p or ".")
@section("sizes")
def show_sizes():
"""Sizes of integers and pointers."""
if sys.maxsize == 2**63-1:
indicates = "indicating 64-bit"
elif sys.maxsize == 2**31-1:
indicates = "indicating 32-bit"
else:
indicates = "not sure what that means"
print("sys.maxsize: {0!r}, {1}".format(sys.maxsize, indicates))
if hasattr(sys, "maxint"):
print("sys.maxint: {0!r}".format(sys.maxint))
else:
print("sys.maxint doesn't exist")
@section("encoding")
def show_encoding():
"""Information about encodings and Unicode."""
if sys.version_info < (3, 3):
if sys.maxunicode == 1114111:
indicates = "indicating a wide Unicode build"
elif sys.maxunicode == 65535:
indicates = "indicating a narrow Unicode build"
else:
indicates = "as all Python >=3.3 have"
print("sys.maxunicode: {0!r}, {1}".format(sys.maxunicode, indicates))
print("sys.getdefaultencoding(): {0!r}".format(sys.getdefaultencoding()))
print("sys.getfilesystemencoding(): {0!r}".format(sys.getfilesystemencoding()))
print("locale.getpreferredencoding(): {0!r}".format(locale.getpreferredencoding()))
print("sys.stdin.encoding: {0!r}".format(sys.stdin.encoding))
print("sys.stdout.encoding: {0!r}".format(sys.stdout.encoding))
@section("locale")
def show_locale():
"""Information about locale."""
print("locale.getpreferredencoding(): {0!r}".format(locale.getpreferredencoding()))
for name in dir(locale):
if name.startswith("LC_") and name != "LC_ALL":
val = locale.getlocale(getattr(locale, name))
print("locale.getlocale({0}): {1!r}".format(name, val))
@section("path")
def show_path():
"""Details of the path used to find imports."""
print("sys.path:")
with indent():
for p in sys.path:
print(repr(p))
more_about_file(p or ".")
def main(words):
"""Run the doctor!"""
print("doctor.py version {0}".format(DOCTOR_VERSION))
print("sys.argv: {0!r}".format(sys.argv))
if "help" in words or "--help" in words:
print("doctor.py [ SECTION ... ]")
print("Sections are:")
with indent():
for section in SECTIONS:
fn = SECTION_MAP.get(section)
print("{0:10s} {1}".format(section, fn.__doc__ or ""))
print("{0:10s} {1}".format("all", "All of the above"))
return
if not words or words == ["all"]:
words = SECTIONS
for word in words:
fn = SECTION_MAP.get(word)
if not fn:
print("*** Don't understand {0!r}".format(word))
else:
print("\n--- {0} {1}".format(word, "-"*(40-len(word))))
if fn.__doc__:
print("# {0}".format(fn.__doc__))
fn()
if __name__ == "__main__":
main(sys.argv[1:])