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.
 
 
 
 

133 lines
4.3 KiB

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