~ajkavanagh/charms/trusty/memcached/add-spaces-support

« back to all changes in this revision

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

  • Committer: Jorge Niedbalski
  • Date: 2016-09-20 18:01:20 UTC
  • mfrom: (72.2.3 memcached.py3)
  • Revision ID: jorge.niedbalski@canonical.com-20160920180120-apb4ut3cugwxcrer
[freyes, r=niedbalski] Fixes bug LP: #1576458

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014-2015 Canonical Limited.
2
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/>.
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
16
14
 
17
15
import os
 
16
from subprocess import check_call
18
17
from charmhelpers.fetch import (
19
18
    BaseFetchHandler,
20
 
    UnhandledSource
 
19
    UnhandledSource,
 
20
    filter_installed_packages,
 
21
    install,
21
22
)
22
23
from charmhelpers.core.host import mkdir
23
24
 
24
 
import six
25
 
if six.PY3:
26
 
    raise ImportError('bzrlib does not support Python3')
27
25
 
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
 
26
if filter_installed_packages(['bzr']) != []:
 
27
    install(['bzr'])
 
28
    if filter_installed_packages(['bzr']) != []:
 
29
        raise NotImplementedError('Unable to install bzr')
36
30
 
37
31
 
38
32
class BzrUrlFetchHandler(BaseFetchHandler):
39
 
    """Handler for bazaar branches via generic and lp URLs"""
 
33
    """Handler for bazaar branches via generic and lp URLs."""
 
34
 
40
35
    def can_handle(self, source):
41
36
        url_parts = self.parse_url(source)
42
 
        if url_parts.scheme not in ('bzr+ssh', 'lp'):
 
37
        if url_parts.scheme not in ('bzr+ssh', 'lp', ''):
43
38
            return False
 
39
        elif not url_parts.scheme:
 
40
            return os.path.exists(os.path.join(source, '.bzr'))
44
41
        else:
45
42
            return True
46
43
 
47
 
    def branch(self, source, dest):
48
 
        url_parts = self.parse_url(source)
49
 
        # If we use lp:branchname scheme we need to load plugins
 
44
    def branch(self, source, dest, revno=None):
50
45
        if not self.can_handle(source):
51
46
            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
 
47
        cmd_opts = []
 
48
        if revno:
 
49
            cmd_opts += ['-r', str(revno)]
 
50
        if os.path.exists(dest):
 
51
            cmd = ['bzr', 'pull']
 
52
            cmd += cmd_opts
 
53
            cmd += ['--overwrite', '-d', dest, source]
 
54
        else:
 
55
            cmd = ['bzr', 'branch']
 
56
            cmd += cmd_opts
 
57
            cmd += [source, dest]
 
58
        check_call(cmd)
66
59
 
67
 
    def install(self, source):
 
60
    def install(self, source, dest=None, revno=None):
68
61
        url_parts = self.parse_url(source)
69
62
        branch_name = url_parts.path.strip("/").split("/")[-1]
70
 
        dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
71
 
                                branch_name)
72
 
        if not os.path.exists(dest_dir):
73
 
            mkdir(dest_dir, perms=0o755)
 
63
        if dest:
 
64
            dest_dir = os.path.join(dest, branch_name)
 
65
        else:
 
66
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
 
67
                                    branch_name)
 
68
 
 
69
        if dest and not os.path.exists(dest):
 
70
            mkdir(dest, perms=0o755)
 
71
 
74
72
        try:
75
 
            self.branch(source, dest_dir)
 
73
            self.branch(source, dest_dir, revno)
76
74
        except OSError as e:
77
75
            raise UnhandledSource(e.strerror)
78
76
        return dest_dir