bazaar.py 3.8 KB

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