~landscape/landscape-charm/trunk

« back to all changes in this revision

Viewing changes to charmhelpers/fetch/bzrurl.py

  • Committer: 🤖 Landscape Builder
  • Author(s): Chad Smith
  • Date: 2017-03-04 03:06:08 UTC
  • mfrom: (382.1.4 landscape-charm)
  • Revision ID: _landscape_builder-20170304030608-lc8tyjw2806tmc4u
Merge add-apt-repository-retries [f=1370933] [r=landscape-builder,ericsnowcurrently] [a=Chad Smith]
Sync of charmhelpers to get updated fetch.add_source which handles 30 retries upon exit 1

Add charmhelpers.osplatform dependency to charm-helpers.yaml

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
18
16
from subprocess import check_call
20
18
    BaseFetchHandler,
21
19
    UnhandledSource,
22
20
    filter_installed_packages,
23
 
    apt_install,
 
21
    install,
24
22
)
25
23
from charmhelpers.core.host import mkdir
26
24
 
27
25
 
28
26
if filter_installed_packages(['bzr']) != []:
29
 
    apt_install(['bzr'])
 
27
    install(['bzr'])
30
28
    if filter_installed_packages(['bzr']) != []:
31
29
        raise NotImplementedError('Unable to install bzr')
32
30
 
33
31
 
34
32
class BzrUrlFetchHandler(BaseFetchHandler):
35
 
    """Handler for bazaar branches via generic and lp URLs"""
 
33
    """Handler for bazaar branches via generic and lp URLs."""
 
34
 
36
35
    def can_handle(self, source):
37
36
        url_parts = self.parse_url(source)
38
37
        if url_parts.scheme not in ('bzr+ssh', 'lp', ''):
42
41
        else:
43
42
            return True
44
43
 
45
 
    def branch(self, source, dest):
 
44
    def branch(self, source, dest, revno=None):
46
45
        if not self.can_handle(source):
47
46
            raise UnhandledSource("Cannot handle {}".format(source))
 
47
        cmd_opts = []
 
48
        if revno:
 
49
            cmd_opts += ['-r', str(revno)]
48
50
        if os.path.exists(dest):
49
 
            check_call(['bzr', 'pull', '--overwrite', '-d', dest, source])
 
51
            cmd = ['bzr', 'pull']
 
52
            cmd += cmd_opts
 
53
            cmd += ['--overwrite', '-d', dest, source]
50
54
        else:
51
 
            check_call(['bzr', 'branch', source, dest])
 
55
            cmd = ['bzr', 'branch']
 
56
            cmd += cmd_opts
 
57
            cmd += [source, dest]
 
58
        check_call(cmd)
52
59
 
53
 
    def install(self, source, dest=None):
 
60
    def install(self, source, dest=None, revno=None):
54
61
        url_parts = self.parse_url(source)
55
62
        branch_name = url_parts.path.strip("/").split("/")[-1]
56
63
        if dest:
59
66
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
60
67
                                    branch_name)
61
68
 
62
 
        if not os.path.exists(dest_dir):
63
 
            mkdir(dest_dir, perms=0o755)
 
69
        if dest and not os.path.exists(dest):
 
70
            mkdir(dest, perms=0o755)
 
71
 
64
72
        try:
65
 
            self.branch(source, dest_dir)
 
73
            self.branch(source, dest_dir, revno)
66
74
        except OSError as e:
67
75
            raise UnhandledSource(e.strerror)
68
76
        return dest_dir