Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/vcs/mercurial.py
4804 views
import configparser1import logging2import os3from typing import List, Optional, Tuple45from pip._internal.exceptions import BadCommand, InstallationError6from pip._internal.utils.misc import HiddenText, display_path7from pip._internal.utils.subprocess import make_command8from pip._internal.utils.urls import path_to_url9from pip._internal.vcs.versioncontrol import (10RevOptions,11VersionControl,12find_path_to_project_root_from_repo_root,13vcs,14)1516logger = logging.getLogger(__name__)171819class Mercurial(VersionControl):20name = "hg"21dirname = ".hg"22repo_name = "clone"23schemes = (24"hg+file",25"hg+http",26"hg+https",27"hg+ssh",28"hg+static-http",29)3031@staticmethod32def get_base_rev_args(rev: str) -> List[str]:33return [rev]3435def fetch_new(36self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int37) -> None:38rev_display = rev_options.to_display()39logger.info(40"Cloning hg %s%s to %s",41url,42rev_display,43display_path(dest),44)45if verbosity <= 0:46flags: Tuple[str, ...] = ("--quiet",)47elif verbosity == 1:48flags = ()49elif verbosity == 2:50flags = ("--verbose",)51else:52flags = ("--verbose", "--debug")53self.run_command(make_command("clone", "--noupdate", *flags, url, dest))54self.run_command(55make_command("update", *flags, rev_options.to_args()),56cwd=dest,57)5859def switch(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:60repo_config = os.path.join(dest, self.dirname, "hgrc")61config = configparser.RawConfigParser()62try:63config.read(repo_config)64config.set("paths", "default", url.secret)65with open(repo_config, "w") as config_file:66config.write(config_file)67except (OSError, configparser.NoSectionError) as exc:68logger.warning("Could not switch Mercurial repository to %s: %s", url, exc)69else:70cmd_args = make_command("update", "-q", rev_options.to_args())71self.run_command(cmd_args, cwd=dest)7273def update(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:74self.run_command(["pull", "-q"], cwd=dest)75cmd_args = make_command("update", "-q", rev_options.to_args())76self.run_command(cmd_args, cwd=dest)7778@classmethod79def get_remote_url(cls, location: str) -> str:80url = cls.run_command(81["showconfig", "paths.default"],82show_stdout=False,83stdout_only=True,84cwd=location,85).strip()86if cls._is_local_repository(url):87url = path_to_url(url)88return url.strip()8990@classmethod91def get_revision(cls, location: str) -> str:92"""93Return the repository-local changeset revision number, as an integer.94"""95current_revision = cls.run_command(96["parents", "--template={rev}"],97show_stdout=False,98stdout_only=True,99cwd=location,100).strip()101return current_revision102103@classmethod104def get_requirement_revision(cls, location: str) -> str:105"""106Return the changeset identification hash, as a 40-character107hexadecimal string108"""109current_rev_hash = cls.run_command(110["parents", "--template={node}"],111show_stdout=False,112stdout_only=True,113cwd=location,114).strip()115return current_rev_hash116117@classmethod118def is_commit_id_equal(cls, dest: str, name: Optional[str]) -> bool:119"""Always assume the versions don't match"""120return False121122@classmethod123def get_subdirectory(cls, location: str) -> Optional[str]:124"""125Return the path to Python project root, relative to the repo root.126Return None if the project root is in the repo root.127"""128# find the repo root129repo_root = cls.run_command(130["root"], show_stdout=False, stdout_only=True, cwd=location131).strip()132if not os.path.isabs(repo_root):133repo_root = os.path.abspath(os.path.join(location, repo_root))134return find_path_to_project_root_from_repo_root(location, repo_root)135136@classmethod137def get_repository_root(cls, location: str) -> Optional[str]:138loc = super().get_repository_root(location)139if loc:140return loc141try:142r = cls.run_command(143["root"],144cwd=location,145show_stdout=False,146stdout_only=True,147on_returncode="raise",148log_failed_cmd=False,149)150except BadCommand:151logger.debug(152"could not determine if %s is under hg control "153"because hg is not available",154location,155)156return None157except InstallationError:158return None159return os.path.normpath(r.rstrip("\r\n"))160161162vcs.register(Mercurial)163164165