bazaar.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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",
  20. "bzr+https",
  21. "bzr+ssh",
  22. "bzr+sftp",
  23. "bzr+ftp",
  24. "bzr+lp",
  25. "bzr+file",
  26. )
  27. @staticmethod
  28. def get_base_rev_args(rev: str) -> List[str]:
  29. return ["-r", rev]
  30. def fetch_new(
  31. self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int
  32. ) -> None:
  33. rev_display = rev_options.to_display()
  34. logger.info(
  35. "Checking out %s%s to %s",
  36. url,
  37. rev_display,
  38. display_path(dest),
  39. )
  40. if verbosity <= 0:
  41. flag = "--quiet"
  42. elif verbosity == 1:
  43. flag = ""
  44. else:
  45. flag = f"-{'v'*verbosity}"
  46. cmd_args = make_command(
  47. "checkout", "--lightweight", flag, rev_options.to_args(), url, dest
  48. )
  49. self.run_command(cmd_args)
  50. def switch(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
  51. self.run_command(make_command("switch", url), cwd=dest)
  52. def update(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
  53. output = self.run_command(
  54. make_command("info"), show_stdout=False, stdout_only=True, cwd=dest
  55. )
  56. if output.startswith("Standalone "):
  57. # Older versions of pip used to create standalone branches.
  58. # Convert the standalone branch to a checkout by calling "bzr bind".
  59. cmd_args = make_command("bind", "-q", url)
  60. self.run_command(cmd_args, cwd=dest)
  61. cmd_args = make_command("update", "-q", rev_options.to_args())
  62. self.run_command(cmd_args, cwd=dest)
  63. @classmethod
  64. def get_url_rev_and_auth(cls, url: str) -> Tuple[str, Optional[str], AuthInfo]:
  65. # hotfix the URL scheme after removing bzr+ from bzr+ssh:// re-add it
  66. url, rev, user_pass = super().get_url_rev_and_auth(url)
  67. if url.startswith("ssh://"):
  68. url = "bzr+" + url
  69. return url, rev, user_pass
  70. @classmethod
  71. def get_remote_url(cls, location: str) -> str:
  72. urls = cls.run_command(
  73. ["info"], show_stdout=False, stdout_only=True, cwd=location
  74. )
  75. for line in urls.splitlines():
  76. line = line.strip()
  77. for x in ("checkout of branch: ", "parent branch: "):
  78. if line.startswith(x):
  79. repo = line.split(x)[1]
  80. if cls._is_local_repository(repo):
  81. return path_to_url(repo)
  82. return repo
  83. raise RemoteNotFoundError
  84. @classmethod
  85. def get_revision(cls, location: str) -> str:
  86. revision = cls.run_command(
  87. ["revno"],
  88. show_stdout=False,
  89. stdout_only=True,
  90. cwd=location,
  91. )
  92. return revision.splitlines()[-1]
  93. @classmethod
  94. def is_commit_id_equal(cls, dest: str, name: Optional[str]) -> bool:
  95. """Always assume the versions don't match"""
  96. return False
  97. vcs.register(Bazaar)