~paulgear/charms/trusty/ntpmaster/fix-python3-on-trusty

« back to all changes in this revision

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

  • Committer: Adam Israel
  • Date: 2016-03-30 17:02:08 UTC
  • mfrom: (12.1.1 trunk)
  • Revision ID: adam.israel@gmail.com-20160330170208-hlkrwz32rw4jcfl4
[paulgear] Sync charm-helpers

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
 
1
17
import os
 
18
from subprocess import check_call
2
19
from charmhelpers.fetch import (
3
20
    BaseFetchHandler,
4
 
    UnhandledSource
 
21
    UnhandledSource,
 
22
    filter_installed_packages,
 
23
    apt_install,
5
24
)
6
25
from charmhelpers.core.host import mkdir
7
26
 
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
 
27
 
 
28
if filter_installed_packages(['bzr']) != []:
 
29
    apt_install(['bzr'])
 
30
    if filter_installed_packages(['bzr']) != []:
 
31
        raise NotImplementedError('Unable to install bzr')
14
32
 
15
33
 
16
34
class BzrUrlFetchHandler(BaseFetchHandler):
17
35
    """Handler for bazaar branches via generic and lp URLs"""
18
36
    def can_handle(self, source):
19
37
        url_parts = self.parse_url(source)
20
 
        if url_parts.scheme not in ('bzr+ssh', 'lp'):
 
38
        if url_parts.scheme not in ('bzr+ssh', 'lp', ''):
21
39
            return False
 
40
        elif not url_parts.scheme:
 
41
            return os.path.exists(os.path.join(source, '.bzr'))
22
42
        else:
23
43
            return True
24
44
 
25
45
    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
46
        if not self.can_handle(source):
29
47
            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
 
48
        if os.path.exists(dest):
 
49
            check_call(['bzr', 'pull', '--overwrite', '-d', dest, source])
 
50
        else:
 
51
            check_call(['bzr', 'branch', source, dest])
38
52
 
39
 
    def install(self, source):
 
53
    def install(self, source, dest=None):
40
54
        url_parts = self.parse_url(source)
41
55
        branch_name = url_parts.path.strip("/").split("/")[-1]
42
 
        dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
43
 
                                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
 
44
62
        if not os.path.exists(dest_dir):
45
 
            mkdir(dest_dir, perms=0755)
 
63
            mkdir(dest_dir, perms=0o755)
46
64
        try:
47
65
            self.branch(source, dest_dir)
48
66
        except OSError as e: