~plumgrid-team/charms/trusty/plumgrid-director/trunk

« back to all changes in this revision

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

  • Committer: bbaqar at plumgrid
  • Date: 2016-04-25 09:14:38 UTC
  • mfrom: (30.1.3 plumgrid-director)
  • Revision ID: bbaqar@plumgrid.com-20160425091438-4hk5s00dydf00jem
Merge: Liberty/Mitaka support

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
import os
 
18
from subprocess import check_call
18
19
from charmhelpers.fetch import (
19
20
    BaseFetchHandler,
20
 
    UnhandledSource
 
21
    UnhandledSource,
 
22
    filter_installed_packages,
 
23
    apt_install,
21
24
)
22
25
from charmhelpers.core.host import mkdir
23
26
 
24
 
import six
25
 
if six.PY3:
26
 
    raise ImportError('bzrlib does not support Python3')
27
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
 
28
if filter_installed_packages(['bzr']) != []:
 
29
    apt_install(['bzr'])
 
30
    if filter_installed_packages(['bzr']) != []:
 
31
        raise NotImplementedError('Unable to install bzr')
36
32
 
37
33
 
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', ''):
43
39
            return False
 
40
        elif not url_parts.scheme:
 
41
            return os.path.exists(os.path.join(source, '.bzr'))
44
42
        else:
45
43
            return True
46
44
 
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
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
 
48
        if os.path.exists(dest):
 
49
            check_call(['bzr', 'pull', '--overwrite', '-d', dest, source])
 
50
        else:
 
51
            check_call(['bzr', 'branch', source, dest])
66
52
 
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",
71
 
                                branch_name)
 
56
        if dest:
 
57
            dest_dir = os.path.join(dest, branch_name)
 
58
        else:
 
59
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
 
60
                                    branch_name)
 
61
 
72
62
        if not os.path.exists(dest_dir):
73
63
            mkdir(dest_dir, perms=0o755)
74
64
        try: