Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/pip/_internal/commands/debug.py
811 views
1
# The following comment should be removed at some point in the future.
2
# mypy: disallow-untyped-defs=False
3
4
from __future__ import absolute_import
5
6
import locale
7
import logging
8
import os
9
import sys
10
11
import pip._vendor
12
from pip._vendor import pkg_resources
13
from pip._vendor.certifi import where
14
15
from pip import __file__ as pip_location
16
from pip._internal.cli import cmdoptions
17
from pip._internal.cli.base_command import Command
18
from pip._internal.cli.cmdoptions import make_target_python
19
from pip._internal.cli.status_codes import SUCCESS
20
from pip._internal.utils.logging import indent_log
21
from pip._internal.utils.misc import get_pip_version
22
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
23
24
if MYPY_CHECK_RUNNING:
25
from types import ModuleType
26
from typing import Any, List, Optional, Dict
27
from optparse import Values
28
29
logger = logging.getLogger(__name__)
30
31
32
def show_value(name, value):
33
# type: (str, Optional[str]) -> None
34
logger.info('{}: {}'.format(name, value))
35
36
37
def show_sys_implementation():
38
# type: () -> None
39
logger.info('sys.implementation:')
40
if hasattr(sys, 'implementation'):
41
implementation = sys.implementation # type: ignore
42
implementation_name = implementation.name
43
else:
44
implementation_name = ''
45
46
with indent_log():
47
show_value('name', implementation_name)
48
49
50
def create_vendor_txt_map():
51
# type: () -> Dict[str, str]
52
vendor_txt_path = os.path.join(
53
os.path.dirname(pip_location),
54
'_vendor',
55
'vendor.txt'
56
)
57
58
with open(vendor_txt_path) as f:
59
# Purge non version specifying lines.
60
# Also, remove any space prefix or suffixes (including comments).
61
lines = [line.strip().split(' ', 1)[0]
62
for line in f.readlines() if '==' in line]
63
64
# Transform into "module" -> version dict.
65
return dict(line.split('==', 1) for line in lines) # type: ignore
66
67
68
def get_module_from_module_name(module_name):
69
# type: (str) -> ModuleType
70
71
# Module name can be uppercase in vendor.txt for some reason...
72
module_name = module_name.lower()
73
# PATCH: setuptools is actually only pkg_resources.
74
if module_name == 'setuptools':
75
module_name = 'pkg_resources'
76
77
__import__(
78
'pip._vendor.{}'.format(module_name),
79
globals(),
80
locals(),
81
level=0
82
)
83
return getattr(pip._vendor, module_name)
84
85
86
def get_vendor_version_from_module(module_name):
87
# type: (str) -> str
88
89
module = get_module_from_module_name(module_name)
90
version = getattr(module, '__version__', None)
91
92
if not version:
93
# Try to find version in debundled module info
94
pkg_set = pkg_resources.WorkingSet(
95
[os.path.dirname(getattr(module, '__file__'))]
96
)
97
package = pkg_set.find(pkg_resources.Requirement.parse(module_name))
98
version = getattr(package, 'version', None)
99
100
return version
101
102
103
def show_actual_vendor_versions(vendor_txt_versions):
104
# type: (Dict[str, str]) -> None
105
# Logs the actual version and print extra info
106
# if there is a conflict or if the actual version could not be imported.
107
108
for module_name, expected_version in vendor_txt_versions.items():
109
extra_message = ''
110
actual_version = get_vendor_version_from_module(module_name)
111
if not actual_version:
112
extra_message = ' (Unable to locate actual module version, using'\
113
' vendor.txt specified version)'
114
actual_version = expected_version
115
elif actual_version != expected_version:
116
extra_message = ' (CONFLICT: vendor.txt suggests version should'\
117
' be {})'.format(expected_version)
118
119
logger.info(
120
'{name}=={actual}{extra}'.format(
121
name=module_name,
122
actual=actual_version,
123
extra=extra_message
124
)
125
)
126
127
128
def show_vendor_versions():
129
# type: () -> None
130
logger.info('vendored library versions:')
131
132
vendor_txt_versions = create_vendor_txt_map()
133
with indent_log():
134
show_actual_vendor_versions(vendor_txt_versions)
135
136
137
def show_tags(options):
138
# type: (Values) -> None
139
tag_limit = 10
140
141
target_python = make_target_python(options)
142
tags = target_python.get_tags()
143
144
# Display the target options that were explicitly provided.
145
formatted_target = target_python.format_given()
146
suffix = ''
147
if formatted_target:
148
suffix = ' (target: {})'.format(formatted_target)
149
150
msg = 'Compatible tags: {}{}'.format(len(tags), suffix)
151
logger.info(msg)
152
153
if options.verbose < 1 and len(tags) > tag_limit:
154
tags_limited = True
155
tags = tags[:tag_limit]
156
else:
157
tags_limited = False
158
159
with indent_log():
160
for tag in tags:
161
logger.info(str(tag))
162
163
if tags_limited:
164
msg = (
165
'...\n'
166
'[First {tag_limit} tags shown. Pass --verbose to show all.]'
167
).format(tag_limit=tag_limit)
168
logger.info(msg)
169
170
171
def ca_bundle_info(config):
172
levels = set()
173
for key, value in config.items():
174
levels.add(key.split('.')[0])
175
176
if not levels:
177
return "Not specified"
178
179
levels_that_override_global = ['install', 'wheel', 'download']
180
global_overriding_level = [
181
level for level in levels if level in levels_that_override_global
182
]
183
if not global_overriding_level:
184
return 'global'
185
186
if 'global' in levels:
187
levels.remove('global')
188
return ", ".join(levels)
189
190
191
class DebugCommand(Command):
192
"""
193
Display debug information.
194
"""
195
196
usage = """
197
%prog <options>"""
198
ignore_require_venv = True
199
200
def __init__(self, *args, **kw):
201
super(DebugCommand, self).__init__(*args, **kw)
202
203
cmd_opts = self.cmd_opts
204
cmdoptions.add_target_python_options(cmd_opts)
205
self.parser.insert_option_group(0, cmd_opts)
206
self.parser.config.load()
207
208
def run(self, options, args):
209
# type: (Values, List[Any]) -> int
210
logger.warning(
211
"This command is only meant for debugging. "
212
"Do not use this with automation for parsing and getting these "
213
"details, since the output and options of this command may "
214
"change without notice."
215
)
216
show_value('pip version', get_pip_version())
217
show_value('sys.version', sys.version)
218
show_value('sys.executable', sys.executable)
219
show_value('sys.getdefaultencoding', sys.getdefaultencoding())
220
show_value('sys.getfilesystemencoding', sys.getfilesystemencoding())
221
show_value(
222
'locale.getpreferredencoding', locale.getpreferredencoding(),
223
)
224
show_value('sys.platform', sys.platform)
225
show_sys_implementation()
226
227
show_value("'cert' config value", ca_bundle_info(self.parser.config))
228
show_value("REQUESTS_CA_BUNDLE", os.environ.get('REQUESTS_CA_BUNDLE'))
229
show_value("CURL_CA_BUNDLE", os.environ.get('CURL_CA_BUNDLE'))
230
show_value("pip._vendor.certifi.where()", where())
231
show_value("pip._vendor.DEBUNDLED", pip._vendor.DEBUNDLED)
232
233
show_vendor_versions()
234
235
show_tags(options)
236
237
return SUCCESS
238
239