bazaar.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import logging
  2. from typing import List, Optional, Tuple
  3. from pip._internal.utils.misc import HiddenText, display_path
  4. from pip._internal.utils.subprocess import make_command
  5. from pip._internal.utils.urls import path_to_url
  6. from pip._internal.vcs.versioncontrol import (
  7. AuthInfo,
  8. RemoteNotFoundError,
  9. RevOptions,
  10. VersionControl,
  11. vcs,
  12. )
  13. logger = logging.getLogger(__name__)
  14. class Bazaar(VersionControl):
  15. name = 'bzr'
  16. dirname = '.bzr'
  17. repo_name = 'branch'
  18. schemes = (
  19. 'bzr+http', 'bzr+https', 'bzr+ssh', 'bzr+sftp', 'bzr+ftp',
  20. 'bzr+lp', 'bzr+file'
  21. )
  22. @staticmethod
  23. def get_base_rev_args(rev):
  24. # type: (str) -> List[str]
  25. return ['-r', rev]
  26. def fetch_new(self, dest, url, rev_options):
  27. # type: (str, HiddenText, RevOptions) -> None
  28. rev_display = rev_options.to_display()
  29. logger.info(
  30. 'Checking out %s%s to %s',
  31. url,
  32. rev_display,
  33. display_path(dest),
  34. )
  35. cmd_args = (
  36. make_command('branch', '-q', rev_options.to_args(), url, dest)
  37. )
  38. self.run_command(cmd_args)
  39. def switch(self, dest, url, rev_options):
  40. # type: (str, HiddenText, RevOptions) -> None
  41. self.run_command(make_command('switch', url), cwd=dest)
  42. def update(self, dest, url, rev_options):
  43. # type: (str, HiddenText, RevOptions) -> None
  44. cmd_args = make_command('pull', '-q', rev_options.to_args())
  45. self.run_command(cmd_args, cwd=dest)
  46. @classmethod
  47. def get_url_rev_and_auth(cls, url):
  48. # type: (str) -> Tuple[str, Optional[str], AuthInfo]
  49. # hotfix the URL scheme after removing bzr+ from bzr+ssh:// readd it
  50. url, rev, user_pass = super().get_url_rev_and_auth(url)
  51. if url.startswith('ssh://'):
  52. url = 'bzr+' + url
  53. return url, rev, user_pass
  54. @classmethod
  55. def get_remote_url(cls, location):
  56. # type: (str) -> str
  57. urls = cls.run_command(
  58. ['info'], show_stdout=False, stdout_only=True, cwd=location
  59. )
  60. for line in urls.splitlines():
  61. line = line.strip()
  62. for x in ('checkout of branch: ',
  63. 'parent branch: '):
  64. if line.startswith(x):
  65. repo = line.split(x)[1]
  66. if cls._is_local_repository(repo):
  67. return path_to_url(repo)
  68. return repo
  69. raise RemoteNotFoundError
  70. @classmethod
  71. def get_revision(cls, location):
  72. # type: (str) -> str
  73. revision = cls.run_command(
  74. ['revno'], show_stdout=False, stdout_only=True, cwd=location,
  75. )
  76. return revision.splitlines()[-1]
  77. @classmethod
  78. def is_commit_id_equal(cls, dest, name):
  79. # type: (str, Optional[str]) -> bool
  80. """Always assume the versions don't match"""
  81. return False
  82. vcs.register(Bazaar)