Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/commands/index.py
4804 views
import logging1from optparse import Values2from typing import Any, Iterable, List, Optional, Union34from pip._vendor.packaging.version import LegacyVersion, Version56from pip._internal.cli import cmdoptions7from pip._internal.cli.req_command import IndexGroupCommand8from pip._internal.cli.status_codes import ERROR, SUCCESS9from pip._internal.commands.search import print_dist_installation_info10from pip._internal.exceptions import CommandError, DistributionNotFound, PipError11from pip._internal.index.collector import LinkCollector12from pip._internal.index.package_finder import PackageFinder13from pip._internal.models.selection_prefs import SelectionPreferences14from pip._internal.models.target_python import TargetPython15from pip._internal.network.session import PipSession16from pip._internal.utils.misc import write_output1718logger = logging.getLogger(__name__)192021class IndexCommand(IndexGroupCommand):22"""23Inspect information available from package indexes.24"""2526usage = """27%prog versions <package>28"""2930def add_options(self) -> None:31cmdoptions.add_target_python_options(self.cmd_opts)3233self.cmd_opts.add_option(cmdoptions.ignore_requires_python())34self.cmd_opts.add_option(cmdoptions.pre())35self.cmd_opts.add_option(cmdoptions.no_binary())36self.cmd_opts.add_option(cmdoptions.only_binary())3738index_opts = cmdoptions.make_option_group(39cmdoptions.index_group,40self.parser,41)4243self.parser.insert_option_group(0, index_opts)44self.parser.insert_option_group(0, self.cmd_opts)4546def run(self, options: Values, args: List[str]) -> int:47handlers = {48"versions": self.get_available_package_versions,49}5051logger.warning(52"pip index is currently an experimental command. "53"It may be removed/changed in a future release "54"without prior warning."55)5657# Determine action58if not args or args[0] not in handlers:59logger.error(60"Need an action (%s) to perform.",61", ".join(sorted(handlers)),62)63return ERROR6465action = args[0]6667# Error handling happens here, not in the action-handlers.68try:69handlers[action](options, args[1:])70except PipError as e:71logger.error(e.args[0])72return ERROR7374return SUCCESS7576def _build_package_finder(77self,78options: Values,79session: PipSession,80target_python: Optional[TargetPython] = None,81ignore_requires_python: Optional[bool] = None,82) -> PackageFinder:83"""84Create a package finder appropriate to the index command.85"""86link_collector = LinkCollector.create(session, options=options)8788# Pass allow_yanked=False to ignore yanked versions.89selection_prefs = SelectionPreferences(90allow_yanked=False,91allow_all_prereleases=options.pre,92ignore_requires_python=ignore_requires_python,93)9495return PackageFinder.create(96link_collector=link_collector,97selection_prefs=selection_prefs,98target_python=target_python,99use_deprecated_html5lib="html5lib" in options.deprecated_features_enabled,100)101102def get_available_package_versions(self, options: Values, args: List[Any]) -> None:103if len(args) != 1:104raise CommandError("You need to specify exactly one argument")105106target_python = cmdoptions.make_target_python(options)107query = args[0]108109with self._build_session(options) as session:110finder = self._build_package_finder(111options=options,112session=session,113target_python=target_python,114ignore_requires_python=options.ignore_requires_python,115)116117versions: Iterable[Union[LegacyVersion, Version]] = (118candidate.version for candidate in finder.find_all_candidates(query)119)120121if not options.pre:122# Remove prereleases123versions = (124version for version in versions if not version.is_prerelease125)126versions = set(versions)127128if not versions:129raise DistributionNotFound(130"No matching distribution found for {}".format(query)131)132133formatted_versions = [str(ver) for ver in sorted(versions, reverse=True)]134latest = formatted_versions[0]135136write_output("{} ({})".format(query, latest))137write_output("Available versions: {}".format(", ".join(formatted_versions)))138print_dist_installation_info(query, latest)139140141