|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import logging |
| 4 | +import sys |
| 5 | + |
| 6 | +from pip._internal.cli import cmdoptions |
| 7 | +from pip._internal.cli.base_command import Command |
| 8 | +from pip._internal.cli.cmdoptions import make_target_python |
| 9 | +from pip._internal.cli.status_codes import SUCCESS |
| 10 | +from pip._internal.utils.logging import indent_log |
| 11 | +from pip._internal.utils.misc import get_pip_version |
| 12 | +from pip._internal.utils.typing import MYPY_CHECK_RUNNING |
| 13 | +from pip._internal.wheel import format_tag |
| 14 | + |
| 15 | +if MYPY_CHECK_RUNNING: |
| 16 | + from typing import Any, List |
| 17 | + from optparse import Values |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +def show_value(name, value): |
| 23 | + # type: (str, str) -> None |
| 24 | + logger.info('{}: {}'.format(name, value)) |
| 25 | + |
| 26 | + |
| 27 | +def show_sys_implementation(): |
| 28 | + # type: () -> None |
| 29 | + logger.info('sys.implementation:') |
| 30 | + if hasattr(sys, 'implementation'): |
| 31 | + implementation = sys.implementation # type: ignore |
| 32 | + implementation_name = implementation.name |
| 33 | + else: |
| 34 | + implementation_name = '' |
| 35 | + |
| 36 | + with indent_log(): |
| 37 | + show_value('name', implementation_name) |
| 38 | + |
| 39 | + |
| 40 | +def show_tags(options): |
| 41 | + # type: (Values) -> None |
| 42 | + tag_limit = 10 |
| 43 | + |
| 44 | + target_python = make_target_python(options) |
| 45 | + tags = target_python.get_tags() |
| 46 | + |
| 47 | + # Display the target options that were explicitly provided. |
| 48 | + formatted_target = target_python.format_given() |
| 49 | + suffix = '' |
| 50 | + if formatted_target: |
| 51 | + suffix = ' (target: {})'.format(formatted_target) |
| 52 | + |
| 53 | + msg = 'Compatible tags: {}{}'.format(len(tags), suffix) |
| 54 | + logger.info(msg) |
| 55 | + |
| 56 | + if options.verbose < 1 and len(tags) > tag_limit: |
| 57 | + tags_limited = True |
| 58 | + tags = tags[:tag_limit] |
| 59 | + else: |
| 60 | + tags_limited = False |
| 61 | + |
| 62 | + with indent_log(): |
| 63 | + for tag in tags: |
| 64 | + logger.info(format_tag(tag)) |
| 65 | + |
| 66 | + if tags_limited: |
| 67 | + msg = ( |
| 68 | + '...\n' |
| 69 | + '[First {tag_limit} tags shown. Pass --verbose to show all.]' |
| 70 | + ).format(tag_limit=tag_limit) |
| 71 | + logger.info(msg) |
| 72 | + |
| 73 | + |
| 74 | +class DebugCommand(Command): |
| 75 | + """ |
| 76 | + Display debug information. |
| 77 | + """ |
| 78 | + |
| 79 | + name = 'debug' |
| 80 | + usage = """ |
| 81 | + %prog <options>""" |
| 82 | + summary = 'Show information useful for debugging.' |
| 83 | + ignore_require_venv = True |
| 84 | + |
| 85 | + def __init__(self, *args, **kw): |
| 86 | + super(DebugCommand, self).__init__(*args, **kw) |
| 87 | + |
| 88 | + cmd_opts = self.cmd_opts |
| 89 | + cmdoptions.add_target_python_options(cmd_opts) |
| 90 | + self.parser.insert_option_group(0, cmd_opts) |
| 91 | + |
| 92 | + def run(self, options, args): |
| 93 | + # type: (Values, List[Any]) -> int |
| 94 | + show_value('pip version', get_pip_version()) |
| 95 | + show_value('sys.version', sys.version) |
| 96 | + show_value('sys.executable', sys.executable) |
| 97 | + show_value('sys.platform', sys.platform) |
| 98 | + show_sys_implementation() |
| 99 | + |
| 100 | + show_tags(options) |
| 101 | + |
| 102 | + return SUCCESS |
0 commit comments