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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. @classmethod
  55. def get_remote_url(cls, location):
  56. url = cls.run_command(
  57. ['showconfig', 'paths.default'],
  58. show_stdout=False, cwd=location).strip()
  59. if cls._is_local_repository(url):
  60. url = path_to_url(url)
  61. return url.strip()
  62. @classmethod
  63. def get_revision(cls, location):
  64. current_revision = cls.run_command(
  65. ['parents', '--template={rev}'],
  66. show_stdout=False, cwd=location).strip()
  67. return current_revision
  68. @classmethod
  69. def get_revision_hash(cls, location):
  70. current_rev_hash = cls.run_command(
  71. ['parents', '--template={node}'],
  72. show_stdout=False, cwd=location).strip()
  73. return current_rev_hash
  74. @classmethod
  75. def get_src_requirement(cls, location, project_name):
  76. repo = cls.get_remote_url(location)
  77. if not repo.lower().startswith('hg:'):
  78. repo = 'hg+' + repo
  79. current_rev_hash = cls.get_revision_hash(location)
  80. return make_vcs_requirement_url(repo, current_rev_hash, project_name)
  81. def is_commit_id_equal(self, dest, name):
  82. """Always assume the versions don't match"""
  83. return False
  84. vcs.register(Mercurial)