15
15
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
18
from subprocess import check_call
18
19
from charmhelpers.fetch import (
22
filter_installed_packages,
22
25
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
28
if filter_installed_packages(['bzr']) != []:
30
if filter_installed_packages(['bzr']) != []:
31
raise NotImplementedError('Unable to install bzr')
38
34
class BzrUrlFetchHandler(BaseFetchHandler):
39
35
"""Handler for bazaar branches via generic and lp URLs"""
40
36
def can_handle(self, source):
41
37
url_parts = self.parse_url(source)
42
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'))
47
45
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
46
if not self.can_handle(source):
51
47
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:
48
if os.path.exists(dest):
49
check_call(['bzr', 'pull', '--overwrite', '-d', dest, source])
51
check_call(['bzr', 'branch', source, dest])
67
def install(self, source):
53
def install(self, source, dest=None):
68
54
url_parts = self.parse_url(source)
69
55
branch_name = url_parts.path.strip("/").split("/")[-1]
70
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",
72
62
if not os.path.exists(dest_dir):
73
63
mkdir(dest_dir, perms=0o755)