1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
5
# charm-helpers is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3 as
7
# published by the Free Software Foundation.
9
# charm-helpers is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
18
from charmhelpers.fetch import (
22
from charmhelpers.core.host import mkdir
26
raise ImportError('bzrlib does not support Python3')
29
from bzrlib.branch import Branch
30
from bzrlib import bzrdir, workingtree, errors
32
from charmhelpers.fetch import apt_install
33
apt_install("python-bzrlib")
34
from bzrlib.branch import Branch
35
from bzrlib import bzrdir, workingtree, errors
38
class BzrUrlFetchHandler(BaseFetchHandler):
39
"""Handler for bazaar branches via generic and lp URLs"""
40
def can_handle(self, source):
41
url_parts = self.parse_url(source)
42
if url_parts.scheme not in ('bzr+ssh', 'lp'):
47
def branch(self, source, dest):
48
url_parts = self.parse_url(source)
49
# If we use lp:branchname scheme we need to load plugins
50
if not self.can_handle(source):
51
raise UnhandledSource("Cannot handle {}".format(source))
52
if url_parts.scheme == "lp":
53
from bzrlib.plugin import load_plugins
56
local_branch = bzrdir.BzrDir.create_branch_convenience(dest)
57
except errors.AlreadyControlDirError:
58
local_branch = Branch.open(dest)
60
remote_branch = Branch.open(source)
61
remote_branch.push(local_branch)
62
tree = workingtree.WorkingTree.open(dest)
64
except Exception as e:
67
def install(self, source):
68
url_parts = self.parse_url(source)
69
branch_name = url_parts.path.strip("/").split("/")[-1]
70
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
72
if not os.path.exists(dest_dir):
73
mkdir(dest_dir, perms=0o755)
75
self.branch(source, dest_dir)
77
raise UnhandledSource(e.strerror)