Path: blob/master/venv/Lib/site-packages/pip/_internal/vcs/mercurial.py
811 views
# The following comment should be removed at some point in the future.1# mypy: disallow-untyped-defs=False23from __future__ import absolute_import45import logging6import os78from pip._vendor.six.moves import configparser910from pip._internal.exceptions import BadCommand, InstallationError11from pip._internal.utils.misc import display_path12from pip._internal.utils.subprocess import make_command13from pip._internal.utils.temp_dir import TempDirectory14from pip._internal.utils.typing import MYPY_CHECK_RUNNING15from pip._internal.utils.urls import path_to_url16from pip._internal.vcs.versioncontrol import (17VersionControl,18find_path_to_setup_from_repo_root,19vcs,20)2122if MYPY_CHECK_RUNNING:23from pip._internal.utils.misc import HiddenText24from pip._internal.vcs.versioncontrol import RevOptions252627logger = logging.getLogger(__name__)282930class Mercurial(VersionControl):31name = 'hg'32dirname = '.hg'33repo_name = 'clone'34schemes = (35'hg', 'hg+file', 'hg+http', 'hg+https', 'hg+ssh', 'hg+static-http',36)3738@staticmethod39def get_base_rev_args(rev):40return [rev]4142def export(self, location, url):43# type: (str, HiddenText) -> None44"""Export the Hg repository at the url to the destination location"""45with TempDirectory(kind="export") as temp_dir:46self.unpack(temp_dir.path, url=url)4748self.run_command(49['archive', location], show_stdout=False, cwd=temp_dir.path50)5152def fetch_new(self, dest, url, rev_options):53# type: (str, HiddenText, RevOptions) -> None54rev_display = rev_options.to_display()55logger.info(56'Cloning hg %s%s to %s',57url,58rev_display,59display_path(dest),60)61self.run_command(make_command('clone', '--noupdate', '-q', url, dest))62self.run_command(63make_command('update', '-q', rev_options.to_args()),64cwd=dest,65)6667def switch(self, dest, url, rev_options):68# type: (str, HiddenText, RevOptions) -> None69repo_config = os.path.join(dest, self.dirname, 'hgrc')70config = configparser.RawConfigParser()71try:72config.read(repo_config)73config.set('paths', 'default', url.secret)74with open(repo_config, 'w') as config_file:75config.write(config_file)76except (OSError, configparser.NoSectionError) as exc:77logger.warning(78'Could not switch Mercurial repository to %s: %s', url, exc,79)80else:81cmd_args = make_command('update', '-q', rev_options.to_args())82self.run_command(cmd_args, cwd=dest)8384def update(self, dest, url, rev_options):85# type: (str, HiddenText, RevOptions) -> None86self.run_command(['pull', '-q'], cwd=dest)87cmd_args = make_command('update', '-q', rev_options.to_args())88self.run_command(cmd_args, cwd=dest)8990@classmethod91def get_remote_url(cls, location):92url = cls.run_command(93['showconfig', 'paths.default'],94show_stdout=False, cwd=location).strip()95if cls._is_local_repository(url):96url = path_to_url(url)97return url.strip()9899@classmethod100def get_revision(cls, location):101"""102Return the repository-local changeset revision number, as an integer.103"""104current_revision = cls.run_command(105['parents', '--template={rev}'],106show_stdout=False, cwd=location).strip()107return current_revision108109@classmethod110def get_requirement_revision(cls, location):111"""112Return the changeset identification hash, as a 40-character113hexadecimal string114"""115current_rev_hash = cls.run_command(116['parents', '--template={node}'],117show_stdout=False, cwd=location).strip()118return current_rev_hash119120@classmethod121def is_commit_id_equal(cls, dest, name):122"""Always assume the versions don't match"""123return False124125@classmethod126def get_subdirectory(cls, location):127"""128Return the path to setup.py, relative to the repo root.129Return None if setup.py is in the repo root.130"""131# find the repo root132repo_root = cls.run_command(133['root'], show_stdout=False, cwd=location).strip()134if not os.path.isabs(repo_root):135repo_root = os.path.abspath(os.path.join(location, repo_root))136return find_path_to_setup_from_repo_root(location, repo_root)137138@classmethod139def get_repository_root(cls, location):140loc = super(Mercurial, cls).get_repository_root(location)141if loc:142return loc143try:144r = cls.run_command(145['root'],146cwd=location,147show_stdout=False,148on_returncode='raise',149log_failed_cmd=False,150)151except BadCommand:152logger.debug("could not determine if %s is under hg control "153"because hg is not available", location)154return None155except InstallationError:156return None157return os.path.normpath(r.rstrip('\r\n'))158159160vcs.register(Mercurial)161162163