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 subprocess import check_call
2
19
from charmhelpers.fetch import (
22
filter_installed_packages,
6
25
from charmhelpers.core.host import mkdir
9
from bzrlib.branch import Branch
11
from charmhelpers.fetch import apt_install
12
apt_install("python-bzrlib")
13
from bzrlib.branch import Branch
28
if filter_installed_packages(['bzr']) != []:
30
if filter_installed_packages(['bzr']) != []:
31
raise NotImplementedError('Unable to install bzr')
16
34
class BzrUrlFetchHandler(BaseFetchHandler):
17
35
"""Handler for bazaar branches via generic and lp URLs"""
18
36
def can_handle(self, source):
19
37
url_parts = self.parse_url(source)
20
if url_parts.scheme not in ('bzr+ssh', 'lp'):
38
if url_parts.scheme not in ('bzr+ssh', 'lp', ''):
40
elif not url_parts.scheme:
41
return os.path.exists(os.path.join(source, '.bzr'))
25
45
def branch(self, source, dest):
26
url_parts = self.parse_url(source)
27
# If we use lp:branchname scheme we need to load plugins
28
46
if not self.can_handle(source):
29
47
raise UnhandledSource("Cannot handle {}".format(source))
30
if url_parts.scheme == "lp":
31
from bzrlib.plugin import load_plugins
34
remote_branch = Branch.open(source)
35
remote_branch.bzrdir.sprout(dest).open_branch()
36
except Exception as e:
48
if os.path.exists(dest):
49
check_call(['bzr', 'pull', '--overwrite', '-d', dest, source])
51
check_call(['bzr', 'branch', source, dest])
39
def install(self, source):
53
def install(self, source, dest=None):
40
54
url_parts = self.parse_url(source)
41
55
branch_name = url_parts.path.strip("/").split("/")[-1]
42
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
57
dest_dir = os.path.join(dest, branch_name)
59
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
44
62
if not os.path.exists(dest_dir):
45
mkdir(dest_dir, perms=0755)
63
mkdir(dest_dir, perms=0o755)
47
65
self.branch(source, dest_dir)
48
66
except OSError as e: