mercurial.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. import tempfile
  5. from pip.utils import display_path, rmtree
  6. from pip.vcs import vcs, VersionControl
  7. from pip.download import path_to_url
  8. from pip._vendor.six.moves import configparser
  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 export(self, location):
  16. """Export the Hg repository at the url to the destination location"""
  17. temp_dir = tempfile.mkdtemp('-export', 'pip-')
  18. self.unpack(temp_dir)
  19. try:
  20. self.run_command(
  21. ['archive', location], show_stdout=False, cwd=temp_dir)
  22. finally:
  23. rmtree(temp_dir)
  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. self.run_command(['update', '-q'] + rev_options, cwd=dest)
  38. def update(self, dest, rev_options):
  39. self.run_command(['pull', '-q'], cwd=dest)
  40. self.run_command(['update', '-q'] + rev_options, cwd=dest)
  41. def obtain(self, dest):
  42. url, rev = self.get_url_rev()
  43. if rev:
  44. rev_options = [rev]
  45. rev_display = ' (to revision %s)' % rev
  46. else:
  47. rev_options = []
  48. rev_display = ''
  49. if self.check_destination(dest, url, rev_options, rev_display):
  50. logger.info(
  51. 'Cloning hg %s%s to %s',
  52. url,
  53. rev_display,
  54. display_path(dest),
  55. )
  56. self.run_command(['clone', '--noupdate', '-q', url, dest])
  57. self.run_command(['update', '-q'] + rev_options, cwd=dest)
  58. def get_url(self, location):
  59. url = self.run_command(
  60. ['showconfig', 'paths.default'],
  61. show_stdout=False, cwd=location).strip()
  62. if self._is_local_repository(url):
  63. url = path_to_url(url)
  64. return url.strip()
  65. def get_revision(self, location):
  66. current_revision = self.run_command(
  67. ['parents', '--template={rev}'],
  68. show_stdout=False, cwd=location).strip()
  69. return current_revision
  70. def get_revision_hash(self, location):
  71. current_rev_hash = self.run_command(
  72. ['parents', '--template={node}'],
  73. show_stdout=False, cwd=location).strip()
  74. return current_rev_hash
  75. def get_src_requirement(self, dist, location):
  76. repo = self.get_url(location)
  77. if not repo.lower().startswith('hg:'):
  78. repo = 'hg+' + repo
  79. egg_project_name = dist.egg_name().split('-', 1)[0]
  80. if not repo:
  81. return None
  82. current_rev_hash = self.get_revision_hash(location)
  83. return '%s@%s#egg=%s' % (repo, current_rev_hash, egg_project_name)
  84. def check_version(self, dest, rev_options):
  85. """Always assume the versions don't match"""
  86. return False
  87. vcs.register(Mercurial)