bazaar.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import logging
  5. import os
  6. from pip._vendor.six.moves.urllib import parse as urllib_parse
  7. from pip._internal.utils.misc import display_path, rmtree
  8. from pip._internal.utils.subprocess import make_command
  9. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  10. from pip._internal.utils.urls import path_to_url
  11. from pip._internal.vcs.versioncontrol import VersionControl, vcs
  12. if MYPY_CHECK_RUNNING:
  13. from typing import Optional, Tuple
  14. from pip._internal.utils.misc import HiddenText
  15. from pip._internal.vcs.versioncontrol import AuthInfo, RevOptions
  16. logger = logging.getLogger(__name__)
  17. class Bazaar(VersionControl):
  18. name = 'bzr'
  19. dirname = '.bzr'
  20. repo_name = 'branch'
  21. schemes = (
  22. 'bzr', 'bzr+http', 'bzr+https', 'bzr+ssh', 'bzr+sftp', 'bzr+ftp',
  23. 'bzr+lp',
  24. )
  25. def __init__(self, *args, **kwargs):
  26. super(Bazaar, self).__init__(*args, **kwargs)
  27. # This is only needed for python <2.7.5
  28. # Register lp but do not expose as a scheme to support bzr+lp.
  29. if getattr(urllib_parse, 'uses_fragment', None):
  30. urllib_parse.uses_fragment.extend(['lp'])
  31. @staticmethod
  32. def get_base_rev_args(rev):
  33. return ['-r', rev]
  34. def export(self, location, url):
  35. # type: (str, HiddenText) -> None
  36. """
  37. Export the Bazaar repository at the url to the destination location
  38. """
  39. # Remove the location to make sure Bazaar can export it correctly
  40. if os.path.exists(location):
  41. rmtree(location)
  42. url, rev_options = self.get_url_rev_options(url)
  43. self.run_command(
  44. make_command('export', location, url, rev_options.to_args()),
  45. show_stdout=False,
  46. )
  47. def fetch_new(self, dest, url, rev_options):
  48. # type: (str, HiddenText, RevOptions) -> None
  49. rev_display = rev_options.to_display()
  50. logger.info(
  51. 'Checking out %s%s to %s',
  52. url,
  53. rev_display,
  54. display_path(dest),
  55. )
  56. cmd_args = (
  57. make_command('branch', '-q', rev_options.to_args(), url, dest)
  58. )
  59. self.run_command(cmd_args)
  60. def switch(self, dest, url, rev_options):
  61. # type: (str, HiddenText, RevOptions) -> None
  62. self.run_command(make_command('switch', url), cwd=dest)
  63. def update(self, dest, url, rev_options):
  64. # type: (str, HiddenText, RevOptions) -> None
  65. cmd_args = make_command('pull', '-q', rev_options.to_args())
  66. self.run_command(cmd_args, cwd=dest)
  67. @classmethod
  68. def get_url_rev_and_auth(cls, url):
  69. # type: (str) -> Tuple[str, Optional[str], AuthInfo]
  70. # hotfix the URL scheme after removing bzr+ from bzr+ssh:// readd it
  71. url, rev, user_pass = super(Bazaar, cls).get_url_rev_and_auth(url)
  72. if url.startswith('ssh://'):
  73. url = 'bzr+' + url
  74. return url, rev, user_pass
  75. @classmethod
  76. def get_remote_url(cls, location):
  77. urls = cls.run_command(['info'], show_stdout=False, cwd=location)
  78. for line in urls.splitlines():
  79. line = line.strip()
  80. for x in ('checkout of branch: ',
  81. 'parent branch: '):
  82. if line.startswith(x):
  83. repo = line.split(x)[1]
  84. if cls._is_local_repository(repo):
  85. return path_to_url(repo)
  86. return repo
  87. return None
  88. @classmethod
  89. def get_revision(cls, location):
  90. revision = cls.run_command(
  91. ['revno'], show_stdout=False, cwd=location,
  92. )
  93. return revision.splitlines()[-1]
  94. @classmethod
  95. def is_commit_id_equal(cls, dest, name):
  96. """Always assume the versions don't match"""
  97. return False
  98. vcs.register(Bazaar)