~jorge/charms/precise/mysql/fix-metadata

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2014-02-19 14:49:31 UTC
  • mto: This revision was merged to the branch mainline in revision 121.
  • Revision ID: edward.hope-morley@canonical.com-20140219144931-ujfwlf11fx2y55h4
[dosaboy] added support for 'source' and 'key' config options so that
          an alternative archive can be added to get more recent
          packages e.g. ceph packages from the coud archive.

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