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.

101 lines
3.4 KiB

4 years ago
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. from pip._vendor.six.moves import configparser
  5. from pip._internal.download import path_to_url
  6. from pip._internal.utils.misc import display_path, make_vcs_requirement_url
  7. from pip._internal.utils.temp_dir import TempDirectory
  8. from pip._internal.vcs import VersionControl, vcs
  9. logger = logging.getLogger(__name__)
  10. class Mercurial(VersionControl):
  11. name = 'hg'
  12. dirname = '.hg'
  13. repo_name = 'clone'
  14. schemes = ('hg', 'hg+http', 'hg+https', 'hg+ssh', 'hg+static-http')
  15. def get_base_rev_args(self, rev):
  16. return [rev]
  17. def export(self, location):
  18. """Export the Hg repository at the url to the destination location"""
  19. with TempDirectory(kind="export") as temp_dir:
  20. self.unpack(temp_dir.path)
  21. self.run_command(
  22. ['archive', location], show_stdout=False, cwd=temp_dir.path
  23. )
  24. def fetch_new(self, dest, url, rev_options):
  25. rev_display = rev_options.to_display()
  26. logger.info(
  27. 'Cloning hg %s%s to %s',
  28. url,
  29. rev_display,
  30. display_path(dest),
  31. )
  32. self.run_command(['clone', '--noupdate', '-q', url, dest])
  33. cmd_args = ['update', '-q'] + rev_options.to_args()
  34. self.run_command(cmd_args, cwd=dest)
  35. def switch(self, dest, url, rev_options):
  36. repo_config = os.path.join(dest, self.dirname, 'hgrc')
  37. config = configparser.SafeConfigParser()
  38. try:
  39. config.read(repo_config)
  40. config.set('paths', 'default', url)
  41. with open(repo_config, 'w') as config_file:
  42. config.write(config_file)
  43. except (OSError, configparser.NoSectionError) as exc:
  44. logger.warning(
  45. 'Could not switch Mercurial repository to %s: %s', url, exc,
  46. )
  47. else:
  48. cmd_args = ['update', '-q'] + rev_options.to_args()
  49. self.run_command(cmd_args, cwd=dest)
  50. def update(self, dest, url, rev_options):
  51. self.run_command(['pull', '-q'], cwd=dest)
  52. cmd_args = ['update', '-q'] + rev_options.to_args()
  53. self.run_command(cmd_args, cwd=dest)
  54. def get_url(self, location):
  55. url = self.run_command(
  56. ['showconfig', 'paths.default'],
  57. show_stdout=False, cwd=location).strip()
  58. if self._is_local_repository(url):
  59. url = path_to_url(url)
  60. return url.strip()
  61. def get_revision(self, location):
  62. current_revision = self.run_command(
  63. ['parents', '--template={rev}'],
  64. show_stdout=False, cwd=location).strip()
  65. return current_revision
  66. def get_revision_hash(self, location):
  67. current_rev_hash = self.run_command(
  68. ['parents', '--template={node}'],
  69. show_stdout=False, cwd=location).strip()
  70. return current_rev_hash
  71. def get_src_requirement(self, dist, location):
  72. repo = self.get_url(location)
  73. if not repo.lower().startswith('hg:'):
  74. repo = 'hg+' + repo
  75. current_rev_hash = self.get_revision_hash(location)
  76. egg_project_name = dist.egg_name().split('-', 1)[0]
  77. return make_vcs_requirement_url(repo, current_rev_hash,
  78. egg_project_name)
  79. def is_commit_id_equal(self, dest, name):
  80. """Always assume the versions don't match"""
  81. return False
  82. vcs.register(Mercurial)