~james-page/charms/trusty/openstack-dashboard/juno-support

« back to all changes in this revision

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

  • Committer: James Page
  • Date: 2013-09-02 16:26:50 UTC
  • mfrom: (17.9.2 openstack-dashboard)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.page@canonical.com-20130902162650-pdfmcmg4uxki40m6
Re-merge with redux branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
from charmhelpers.fetch import (
3
 
    BaseFetchHandler,
4
 
    UnhandledSource
5
 
)
6
 
from charmhelpers.core.host import mkdir
7
 
 
8
 
try:
9
 
    from bzrlib.branch import Branch
10
 
except ImportError:
11
 
    from charmhelpers.fetch import apt_install
12
 
    apt_install("python-bzrlib")
13
 
    from bzrlib.branch import Branch
14
 
 
15
 
class BzrUrlFetchHandler(BaseFetchHandler):
16
 
    """Handler for bazaar branches via generic and lp URLs"""
17
 
    def can_handle(self, source):
18
 
        url_parts = self.parse_url(source)
19
 
        if url_parts.scheme not in ('bzr+ssh', 'lp'):
20
 
            return False
21
 
        else:
22
 
            return True
23
 
 
24
 
    def branch(self, source, dest):
25
 
        url_parts = self.parse_url(source)
26
 
        # If we use lp:branchname scheme we need to load plugins
27
 
        if not self.can_handle(source):
28
 
            raise UnhandledSource("Cannot handle {}".format(source))
29
 
        if url_parts.scheme == "lp":
30
 
            from bzrlib.plugin import load_plugins
31
 
            load_plugins()
32
 
        try:
33
 
            remote_branch = Branch.open(source)
34
 
            remote_branch.bzrdir.sprout(dest).open_branch()
35
 
        except Exception as e:
36
 
            raise e
37
 
 
38
 
    def install(self, source):
39
 
        url_parts = self.parse_url(source)
40
 
        branch_name = url_parts.path.strip("/").split("/")[-1]
41
 
        dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched", branch_name)
42
 
        if not os.path.exists(dest_dir):
43
 
            mkdir(dest_dir, perms=0755)
44
 
        try:
45
 
            self.branch(source, dest_dir)
46
 
        except OSError as e:
47
 
            raise UnhandledSource(e.strerror)
48
 
        return dest_dir
49