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.

112 lines
3.6 KiB

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