~dekervit/charms/precise/mongodb/trunk

« back to all changes in this revision

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

  • Committer: Juan L. Negron
  • Date: 2014-04-14 16:18:45 UTC
  • mfrom: (46.1.3 mongodb)
  • Revision ID: juan@ubuntu.com-20140414161845-fdlysbuv2r0op3va
Adding source option. MP:215514

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