AutoFramework/venv/Lib/site-packages/pip/_internal/vcs/mercurial.py

164 lines
5.1 KiB
Python
Raw Normal View History

2022-07-17 01:48:29 +08:00
import configparser
import logging
import os
2022-07-21 08:44:10 +08:00
from typing import List, Optional, Tuple
2022-07-17 01:48:29 +08:00
from pip._internal.exceptions import BadCommand, InstallationError
from pip._internal.utils.misc import HiddenText, display_path
from pip._internal.utils.subprocess import make_command
from pip._internal.utils.urls import path_to_url
from pip._internal.vcs.versioncontrol import (
RevOptions,
VersionControl,
2022-07-21 08:44:10 +08:00
find_path_to_project_root_from_repo_root,
2022-07-17 01:48:29 +08:00
vcs,
)
logger = logging.getLogger(__name__)
class Mercurial(VersionControl):
2022-07-21 08:44:10 +08:00
name = "hg"
dirname = ".hg"
repo_name = "clone"
2022-07-17 01:48:29 +08:00
schemes = (
2022-07-21 08:44:10 +08:00
"hg+file",
"hg+http",
"hg+https",
"hg+ssh",
"hg+static-http",
2022-07-17 01:48:29 +08:00
)
@staticmethod
2022-07-21 08:44:10 +08:00
def get_base_rev_args(rev: str) -> List[str]:
2022-07-17 01:48:29 +08:00
return [rev]
2022-07-21 08:44:10 +08:00
def fetch_new(
self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int
) -> None:
2022-07-17 01:48:29 +08:00
rev_display = rev_options.to_display()
logger.info(
2022-07-21 08:44:10 +08:00
"Cloning hg %s%s to %s",
2022-07-17 01:48:29 +08:00
url,
rev_display,
display_path(dest),
)
2022-07-21 08:44:10 +08:00
if verbosity <= 0:
flags: Tuple[str, ...] = ("--quiet",)
elif verbosity == 1:
flags = ()
elif verbosity == 2:
flags = ("--verbose",)
else:
flags = ("--verbose", "--debug")
self.run_command(make_command("clone", "--noupdate", *flags, url, dest))
2022-07-17 01:48:29 +08:00
self.run_command(
2022-07-21 08:44:10 +08:00
make_command("update", *flags, rev_options.to_args()),
2022-07-17 01:48:29 +08:00
cwd=dest,
)
2022-07-21 08:44:10 +08:00
def switch(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
repo_config = os.path.join(dest, self.dirname, "hgrc")
2022-07-17 01:48:29 +08:00
config = configparser.RawConfigParser()
try:
config.read(repo_config)
2022-07-21 08:44:10 +08:00
config.set("paths", "default", url.secret)
with open(repo_config, "w") as config_file:
2022-07-17 01:48:29 +08:00
config.write(config_file)
except (OSError, configparser.NoSectionError) as exc:
2022-07-21 08:44:10 +08:00
logger.warning("Could not switch Mercurial repository to %s: %s", url, exc)
2022-07-17 01:48:29 +08:00
else:
2022-07-21 08:44:10 +08:00
cmd_args = make_command("update", "-q", rev_options.to_args())
2022-07-17 01:48:29 +08:00
self.run_command(cmd_args, cwd=dest)
2022-07-21 08:44:10 +08:00
def update(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
self.run_command(["pull", "-q"], cwd=dest)
cmd_args = make_command("update", "-q", rev_options.to_args())
2022-07-17 01:48:29 +08:00
self.run_command(cmd_args, cwd=dest)
@classmethod
2022-07-21 08:44:10 +08:00
def get_remote_url(cls, location: str) -> str:
2022-07-17 01:48:29 +08:00
url = cls.run_command(
2022-07-21 08:44:10 +08:00
["showconfig", "paths.default"],
2022-07-17 01:48:29 +08:00
show_stdout=False,
stdout_only=True,
cwd=location,
).strip()
if cls._is_local_repository(url):
url = path_to_url(url)
return url.strip()
@classmethod
2022-07-21 08:44:10 +08:00
def get_revision(cls, location: str) -> str:
2022-07-17 01:48:29 +08:00
"""
Return the repository-local changeset revision number, as an integer.
"""
current_revision = cls.run_command(
2022-07-21 08:44:10 +08:00
["parents", "--template={rev}"],
2022-07-17 01:48:29 +08:00
show_stdout=False,
stdout_only=True,
cwd=location,
).strip()
return current_revision
@classmethod
2022-07-21 08:44:10 +08:00
def get_requirement_revision(cls, location: str) -> str:
2022-07-17 01:48:29 +08:00
"""
Return the changeset identification hash, as a 40-character
hexadecimal string
"""
current_rev_hash = cls.run_command(
2022-07-21 08:44:10 +08:00
["parents", "--template={node}"],
2022-07-17 01:48:29 +08:00
show_stdout=False,
stdout_only=True,
cwd=location,
).strip()
return current_rev_hash
@classmethod
2022-07-21 08:44:10 +08:00
def is_commit_id_equal(cls, dest: str, name: Optional[str]) -> bool:
2022-07-17 01:48:29 +08:00
"""Always assume the versions don't match"""
return False
@classmethod
2022-07-21 08:44:10 +08:00
def get_subdirectory(cls, location: str) -> Optional[str]:
2022-07-17 01:48:29 +08:00
"""
2022-07-21 08:44:10 +08:00
Return the path to Python project root, relative to the repo root.
Return None if the project root is in the repo root.
2022-07-17 01:48:29 +08:00
"""
# find the repo root
repo_root = cls.run_command(
2022-07-21 08:44:10 +08:00
["root"], show_stdout=False, stdout_only=True, cwd=location
2022-07-17 01:48:29 +08:00
).strip()
if not os.path.isabs(repo_root):
repo_root = os.path.abspath(os.path.join(location, repo_root))
2022-07-21 08:44:10 +08:00
return find_path_to_project_root_from_repo_root(location, repo_root)
2022-07-17 01:48:29 +08:00
@classmethod
2022-07-21 08:44:10 +08:00
def get_repository_root(cls, location: str) -> Optional[str]:
2022-07-17 01:48:29 +08:00
loc = super().get_repository_root(location)
if loc:
return loc
try:
r = cls.run_command(
2022-07-21 08:44:10 +08:00
["root"],
2022-07-17 01:48:29 +08:00
cwd=location,
show_stdout=False,
stdout_only=True,
2022-07-21 08:44:10 +08:00
on_returncode="raise",
2022-07-17 01:48:29 +08:00
log_failed_cmd=False,
)
except BadCommand:
2022-07-21 08:44:10 +08:00
logger.debug(
"could not determine if %s is under hg control "
"because hg is not available",
location,
)
2022-07-17 01:48:29 +08:00
return None
except InstallationError:
return None
2022-07-21 08:44:10 +08:00
return os.path.normpath(r.rstrip("\r\n"))
2022-07-17 01:48:29 +08:00
vcs.register(Mercurial)