~charmers/charms/vivid/ubuntu/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/fetch/bzrurl.py

  • Committer: Charles Butler
  • Date: 2015-03-27 15:01:57 UTC
  • mfrom: (7.2.2 ubuntu)
  • Revision ID: chuck@dasroot.net-20150327150157-qm15dcmaweqogadh
  Liang Chen 2015-02-19 provide a config option for ubuntu charm
  Liang Chen 2015-02-19 sync charmhelper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
import os
 
18
from charmhelpers.fetch import (
 
19
    BaseFetchHandler,
 
20
    UnhandledSource
 
21
)
 
22
from charmhelpers.core.host import mkdir
 
23
 
 
24
import six
 
25
if six.PY3:
 
26
    raise ImportError('bzrlib does not support Python3')
 
27
 
 
28
try:
 
29
    from bzrlib.branch import Branch
 
30
    from bzrlib import bzrdir, workingtree, errors
 
31
except ImportError:
 
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
 
36
 
 
37
 
 
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'):
 
43
            return False
 
44
        else:
 
45
            return True
 
46
 
 
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
 
54
            load_plugins()
 
55
        try:
 
56
            local_branch = bzrdir.BzrDir.create_branch_convenience(dest)
 
57
        except errors.AlreadyControlDirError:
 
58
            local_branch = Branch.open(dest)
 
59
        try:
 
60
            remote_branch = Branch.open(source)
 
61
            remote_branch.push(local_branch)
 
62
            tree = workingtree.WorkingTree.open(dest)
 
63
            tree.update()
 
64
        except Exception as e:
 
65
            raise e
 
66
 
 
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",
 
71
                                branch_name)
 
72
        if not os.path.exists(dest_dir):
 
73
            mkdir(dest_dir, perms=0o755)
 
74
        try:
 
75
            self.branch(source, dest_dir)
 
76
        except OSError as e:
 
77
            raise UnhandledSource(e.strerror)
 
78
        return dest_dir