1
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/>.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
16
from subprocess import check_call
18
17
from charmhelpers.fetch import (
20
filter_installed_packages,
22
23
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
26
if filter_installed_packages(['bzr']) != []:
28
if filter_installed_packages(['bzr']) != []:
29
raise NotImplementedError('Unable to install bzr')
38
32
class BzrUrlFetchHandler(BaseFetchHandler):
39
"""Handler for bazaar branches via generic and lp URLs"""
33
"""Handler for bazaar branches via generic and lp URLs."""
40
35
def can_handle(self, source):
41
36
url_parts = self.parse_url(source)
42
if url_parts.scheme not in ('bzr+ssh', 'lp'):
37
if url_parts.scheme not in ('bzr+ssh', 'lp', ''):
39
elif not url_parts.scheme:
40
return os.path.exists(os.path.join(source, '.bzr'))
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
44
def branch(self, source, dest, revno=None):
50
45
if not self.can_handle(source):
51
46
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:
49
cmd_opts += ['-r', str(revno)]
50
if os.path.exists(dest):
53
cmd += ['--overwrite', '-d', dest, source]
55
cmd = ['bzr', 'branch']
67
def install(self, source):
60
def install(self, source, dest=None, revno=None):
68
61
url_parts = self.parse_url(source)
69
62
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)
64
dest_dir = os.path.join(dest, branch_name)
66
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
69
if dest and not os.path.exists(dest):
70
mkdir(dest, perms=0o755)
75
self.branch(source, dest_dir)
73
self.branch(source, dest_dir, revno)
76
74
except OSError as e:
77
75
raise UnhandledSource(e.strerror)