2015-11-25 01:25:01 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import logging
|
2013-10-11 17:28:32 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import re
|
2015-11-25 01:25:01 +00:00
|
|
|
|
|
|
|
# TODO: Get this into six.moves.urllib.parse
|
|
|
|
try:
|
|
|
|
from urllib import parse as urllib_parse
|
|
|
|
except ImportError:
|
|
|
|
import urlparse as urllib_parse
|
|
|
|
|
|
|
|
from pip.utils import rmtree, display_path
|
2013-10-11 17:28:32 +00:00
|
|
|
from pip.vcs import vcs, VersionControl
|
|
|
|
from pip.download import path_to_url
|
|
|
|
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
class Bazaar(VersionControl):
|
|
|
|
name = 'bzr'
|
|
|
|
dirname = '.bzr'
|
|
|
|
repo_name = 'branch'
|
2015-11-25 01:25:01 +00:00
|
|
|
schemes = (
|
|
|
|
'bzr', 'bzr+http', 'bzr+https', 'bzr+ssh', 'bzr+sftp', 'bzr+ftp',
|
|
|
|
'bzr+lp',
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def __init__(self, url=None, *args, **kwargs):
|
|
|
|
super(Bazaar, self).__init__(url, *args, **kwargs)
|
|
|
|
# Python >= 2.7.4, 3.3 doesn't have uses_fragment or non_hierarchical
|
|
|
|
# Register lp but do not expose as a scheme to support bzr+lp.
|
2015-11-25 01:25:01 +00:00
|
|
|
if getattr(urllib_parse, 'uses_fragment', None):
|
|
|
|
urllib_parse.uses_fragment.extend(['lp'])
|
|
|
|
urllib_parse.non_hierarchical.extend(['lp'])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def export(self, location):
|
2015-11-25 01:25:01 +00:00
|
|
|
"""
|
|
|
|
Export the Bazaar repository at the url to the destination location
|
|
|
|
"""
|
2013-10-11 17:28:32 +00:00
|
|
|
temp_dir = tempfile.mkdtemp('-export', 'pip-')
|
|
|
|
self.unpack(temp_dir)
|
|
|
|
if os.path.exists(location):
|
|
|
|
# Remove the location to make sure Bazaar can export it correctly
|
|
|
|
rmtree(location)
|
|
|
|
try:
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(['export', location], cwd=temp_dir,
|
|
|
|
show_stdout=False)
|
2013-10-11 17:28:32 +00:00
|
|
|
finally:
|
|
|
|
rmtree(temp_dir)
|
|
|
|
|
|
|
|
def switch(self, dest, url, rev_options):
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(['switch', url], cwd=dest)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def update(self, dest, rev_options):
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(['pull', '-q'] + rev_options, cwd=dest)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def obtain(self, dest):
|
|
|
|
url, rev = self.get_url_rev()
|
|
|
|
if rev:
|
|
|
|
rev_options = ['-r', rev]
|
|
|
|
rev_display = ' (to revision %s)' % rev
|
|
|
|
else:
|
|
|
|
rev_options = []
|
|
|
|
rev_display = ''
|
|
|
|
if self.check_destination(dest, url, rev_options, rev_display):
|
2015-11-25 01:25:01 +00:00
|
|
|
logger.info(
|
|
|
|
'Checking out %s%s to %s',
|
|
|
|
url,
|
|
|
|
rev_display,
|
|
|
|
display_path(dest),
|
|
|
|
)
|
|
|
|
self.run_command(['branch', '-q'] + rev_options + [url, dest])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def get_url_rev(self):
|
|
|
|
# hotfix the URL scheme after removing bzr+ from bzr+ssh:// readd it
|
|
|
|
url, rev = super(Bazaar, self).get_url_rev()
|
|
|
|
if url.startswith('ssh://'):
|
|
|
|
url = 'bzr+' + url
|
|
|
|
return url, rev
|
|
|
|
|
|
|
|
def get_url(self, location):
|
2015-11-25 01:25:01 +00:00
|
|
|
urls = self.run_command(['info'], show_stdout=False, cwd=location)
|
2013-10-11 17:28:32 +00:00
|
|
|
for line in urls.splitlines():
|
|
|
|
line = line.strip()
|
|
|
|
for x in ('checkout of branch: ',
|
|
|
|
'parent branch: '):
|
|
|
|
if line.startswith(x):
|
|
|
|
repo = line.split(x)[1]
|
|
|
|
if self._is_local_repository(repo):
|
|
|
|
return path_to_url(repo)
|
|
|
|
return repo
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_revision(self, location):
|
2015-11-25 01:25:01 +00:00
|
|
|
revision = self.run_command(
|
|
|
|
['revno'], show_stdout=False, cwd=location)
|
2013-10-11 17:28:32 +00:00
|
|
|
return revision.splitlines()[-1]
|
|
|
|
|
|
|
|
def get_tag_revs(self, location):
|
2015-11-25 01:25:01 +00:00
|
|
|
tags = self.run_command(
|
|
|
|
['tags'], show_stdout=False, cwd=location)
|
2013-10-11 17:28:32 +00:00
|
|
|
tag_revs = []
|
|
|
|
for line in tags.splitlines():
|
|
|
|
tags_match = re.search(r'([.\w-]+)\s*(.*)$', line)
|
|
|
|
if tags_match:
|
|
|
|
tag = tags_match.group(1)
|
|
|
|
rev = tags_match.group(2)
|
|
|
|
tag_revs.append((rev.strip(), tag.strip()))
|
|
|
|
return dict(tag_revs)
|
|
|
|
|
|
|
|
def get_src_requirement(self, dist, location, find_tags):
|
|
|
|
repo = self.get_url(location)
|
2015-11-25 01:25:01 +00:00
|
|
|
if not repo:
|
|
|
|
return None
|
2013-10-11 17:28:32 +00:00
|
|
|
if not repo.lower().startswith('bzr:'):
|
|
|
|
repo = 'bzr+' + repo
|
|
|
|
egg_project_name = dist.egg_name().split('-', 1)[0]
|
|
|
|
current_rev = self.get_revision(location)
|
|
|
|
tag_revs = self.get_tag_revs(location)
|
|
|
|
|
|
|
|
if current_rev in tag_revs:
|
|
|
|
# It's a tag
|
|
|
|
full_egg_name = '%s-%s' % (egg_project_name, tag_revs[current_rev])
|
|
|
|
else:
|
|
|
|
full_egg_name = '%s-dev_r%s' % (dist.egg_name(), current_rev)
|
|
|
|
return '%s@%s#egg=%s' % (repo, current_rev, full_egg_name)
|
|
|
|
|
|
|
|
|
|
|
|
vcs.register(Bazaar)
|