mercurial.py 5.0 KB

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