mercurial.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  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 switch(self, dest, url, rev_options):
  25. repo_config = os.path.join(dest, self.dirname, 'hgrc')
  26. config = configparser.SafeConfigParser()
  27. try:
  28. config.read(repo_config)
  29. config.set('paths', 'default', url)
  30. with open(repo_config, 'w') as config_file:
  31. config.write(config_file)
  32. except (OSError, configparser.NoSectionError) as exc:
  33. logger.warning(
  34. 'Could not switch Mercurial repository to %s: %s', url, exc,
  35. )
  36. else:
  37. cmd_args = ['update', '-q'] + rev_options.to_args()
  38. self.run_command(cmd_args, cwd=dest)
  39. def update(self, dest, rev_options):
  40. self.run_command(['pull', '-q'], cwd=dest)
  41. cmd_args = ['update', '-q'] + rev_options.to_args()
  42. self.run_command(cmd_args, cwd=dest)
  43. def obtain(self, dest):
  44. url, rev = self.get_url_rev()
  45. rev_options = self.make_rev_options(rev)
  46. if self.check_destination(dest, url, rev_options):
  47. rev_display = rev_options.to_display()
  48. logger.info(
  49. 'Cloning hg %s%s to %s',
  50. url,
  51. rev_display,
  52. display_path(dest),
  53. )
  54. self.run_command(['clone', '--noupdate', '-q', url, dest])
  55. cmd_args = ['update', '-q'] + rev_options.to_args()
  56. self.run_command(cmd_args, cwd=dest)
  57. def get_url(self, location):
  58. url = self.run_command(
  59. ['showconfig', 'paths.default'],
  60. show_stdout=False, cwd=location).strip()
  61. if self._is_local_repository(url):
  62. url = path_to_url(url)
  63. return url.strip()
  64. def get_revision(self, location):
  65. current_revision = self.run_command(
  66. ['parents', '--template={rev}'],
  67. show_stdout=False, cwd=location).strip()
  68. return current_revision
  69. def get_revision_hash(self, location):
  70. current_rev_hash = self.run_command(
  71. ['parents', '--template={node}'],
  72. show_stdout=False, cwd=location).strip()
  73. return current_rev_hash
  74. def get_src_requirement(self, dist, location):
  75. repo = self.get_url(location)
  76. if not repo.lower().startswith('hg:'):
  77. repo = 'hg+' + repo
  78. egg_project_name = dist.egg_name().split('-', 1)[0]
  79. if not repo:
  80. return None
  81. current_rev_hash = self.get_revision_hash(location)
  82. return '%s@%s#egg=%s' % (repo, current_rev_hash, egg_project_name)
  83. def is_commit_id_equal(self, dest, name):
  84. """Always assume the versions don't match"""
  85. return False
  86. vcs.register(Mercurial)