You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mercurial.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import logging
  5. import os
  6. from pip._vendor.six.moves import configparser
  7. from pip._internal.exceptions import BadCommand, InstallationError
  8. from pip._internal.utils.misc import display_path
  9. from pip._internal.utils.subprocess import make_command
  10. from pip._internal.utils.temp_dir import TempDirectory
  11. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  12. from pip._internal.utils.urls import path_to_url
  13. from pip._internal.vcs.versioncontrol import (
  14. VersionControl,
  15. find_path_to_setup_from_repo_root,
  16. vcs,
  17. )
  18. if MYPY_CHECK_RUNNING:
  19. from pip._internal.utils.misc import HiddenText
  20. from pip._internal.vcs.versioncontrol import RevOptions
  21. logger = logging.getLogger(__name__)
  22. class Mercurial(VersionControl):
  23. name = 'hg'
  24. dirname = '.hg'
  25. repo_name = 'clone'
  26. schemes = (
  27. 'hg', 'hg+file', 'hg+http', 'hg+https', 'hg+ssh', 'hg+static-http',
  28. )
  29. @staticmethod
  30. def get_base_rev_args(rev):
  31. return [rev]
  32. def export(self, location, url):
  33. # type: (str, HiddenText) -> None
  34. """Export the Hg repository at the url to the destination location"""
  35. with TempDirectory(kind="export") as temp_dir:
  36. self.unpack(temp_dir.path, url=url)
  37. self.run_command(
  38. ['archive', location], show_stdout=False, cwd=temp_dir.path
  39. )
  40. def fetch_new(self, dest, url, rev_options):
  41. # type: (str, HiddenText, RevOptions) -> None
  42. rev_display = rev_options.to_display()
  43. logger.info(
  44. 'Cloning hg %s%s to %s',
  45. url,
  46. rev_display,
  47. display_path(dest),
  48. )
  49. self.run_command(make_command('clone', '--noupdate', '-q', url, dest))
  50. self.run_command(
  51. make_command('update', '-q', rev_options.to_args()),
  52. cwd=dest,
  53. )
  54. def switch(self, dest, url, rev_options):
  55. # type: (str, HiddenText, RevOptions) -> None
  56. repo_config = os.path.join(dest, self.dirname, 'hgrc')
  57. config = configparser.RawConfigParser()
  58. try:
  59. config.read(repo_config)
  60. config.set('paths', 'default', url.secret)
  61. with open(repo_config, 'w') as config_file:
  62. config.write(config_file)
  63. except (OSError, configparser.NoSectionError) as exc:
  64. logger.warning(
  65. 'Could not switch Mercurial repository to %s: %s', url, exc,
  66. )
  67. else:
  68. cmd_args = make_command('update', '-q', rev_options.to_args())
  69. self.run_command(cmd_args, cwd=dest)
  70. def update(self, dest, url, rev_options):
  71. # type: (str, HiddenText, RevOptions) -> None
  72. self.run_command(['pull', '-q'], cwd=dest)
  73. cmd_args = make_command('update', '-q', rev_options.to_args())
  74. self.run_command(cmd_args, cwd=dest)
  75. @classmethod
  76. def get_remote_url(cls, location):
  77. url = cls.run_command(
  78. ['showconfig', 'paths.default'],
  79. show_stdout=False, cwd=location).strip()
  80. if cls._is_local_repository(url):
  81. url = path_to_url(url)
  82. return url.strip()
  83. @classmethod
  84. def get_revision(cls, location):
  85. """
  86. Return the repository-local changeset revision number, as an integer.
  87. """
  88. current_revision = cls.run_command(
  89. ['parents', '--template={rev}'],
  90. show_stdout=False, cwd=location).strip()
  91. return current_revision
  92. @classmethod
  93. def get_requirement_revision(cls, location):
  94. """
  95. Return the changeset identification hash, as a 40-character
  96. hexadecimal string
  97. """
  98. current_rev_hash = cls.run_command(
  99. ['parents', '--template={node}'],
  100. show_stdout=False, cwd=location).strip()
  101. return current_rev_hash
  102. @classmethod
  103. def is_commit_id_equal(cls, dest, name):
  104. """Always assume the versions don't match"""
  105. return False
  106. @classmethod
  107. def get_subdirectory(cls, location):
  108. """
  109. Return the path to setup.py, relative to the repo root.
  110. Return None if setup.py is in the repo root.
  111. """
  112. # find the repo root
  113. repo_root = cls.run_command(
  114. ['root'], show_stdout=False, cwd=location).strip()
  115. if not os.path.isabs(repo_root):
  116. repo_root = os.path.abspath(os.path.join(location, repo_root))
  117. return find_path_to_setup_from_repo_root(location, repo_root)
  118. @classmethod
  119. def controls_location(cls, location):
  120. if super(Mercurial, cls).controls_location(location):
  121. return True
  122. try:
  123. cls.run_command(
  124. ['identify'],
  125. cwd=location,
  126. show_stdout=False,
  127. on_returncode='raise',
  128. log_failed_cmd=False)
  129. return True
  130. except (BadCommand, InstallationError):
  131. return False
  132. vcs.register(Mercurial)