~stub/charms/trusty/nrpe/chsync

« back to all changes in this revision

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

  • Committer: Stuart Bishop
  • Date: 2016-07-08 14:49:27 UTC
  • Revision ID: stuart.bishop@canonical.com-20160708144927-jl3oh7s819h77nmz
Resync charmhelpers

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, CalledProcessError
18
17
from charmhelpers.fetch import (
19
18
    BaseFetchHandler,
20
 
    UnhandledSource
 
19
    UnhandledSource,
 
20
    filter_installed_packages,
 
21
    apt_install,
21
22
)
22
 
from charmhelpers.core.host import mkdir
23
 
 
24
 
import six
25
 
if six.PY3:
26
 
    raise ImportError('GitPython does not support Python 3')
27
 
 
28
 
try:
29
 
    from git import Repo
30
 
except ImportError:
31
 
    from charmhelpers.fetch import apt_install
32
 
    apt_install("python-git")
33
 
    from git import Repo
34
 
 
35
 
from git.exc import GitCommandError  # noqa E402
 
23
 
 
24
if filter_installed_packages(['git']) != []:
 
25
    apt_install(['git'])
 
26
    if filter_installed_packages(['git']) != []:
 
27
        raise NotImplementedError('Unable to install git')
36
28
 
37
29
 
38
30
class GitUrlFetchHandler(BaseFetchHandler):
40
32
    def can_handle(self, source):
41
33
        url_parts = self.parse_url(source)
42
34
        # TODO (mattyw) no support for ssh git@ yet
43
 
        if url_parts.scheme not in ('http', 'https', 'git'):
 
35
        if url_parts.scheme not in ('http', 'https', 'git', ''):
44
36
            return False
 
37
        elif not url_parts.scheme:
 
38
            return os.path.exists(os.path.join(source, '.git'))
45
39
        else:
46
40
            return True
47
41
 
48
 
    def clone(self, source, dest, branch):
 
42
    def clone(self, source, dest, branch="master", depth=None):
49
43
        if not self.can_handle(source):
50
44
            raise UnhandledSource("Cannot handle {}".format(source))
51
45
 
52
 
        repo = Repo.clone_from(source, dest)
53
 
        repo.git.checkout(branch)
 
46
        if os.path.exists(dest):
 
47
            cmd = ['git', '-C', dest, 'pull', source, branch]
 
48
        else:
 
49
            cmd = ['git', 'clone', source, dest, '--branch', branch]
 
50
            if depth:
 
51
                cmd.extend(['--depth', depth])
 
52
        check_call(cmd)
54
53
 
55
 
    def install(self, source, branch="master", dest=None):
 
54
    def install(self, source, branch="master", dest=None, depth=None):
56
55
        url_parts = self.parse_url(source)
57
56
        branch_name = url_parts.path.strip("/").split("/")[-1]
58
57
        if dest:
60
59
        else:
61
60
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
62
61
                                    branch_name)
63
 
        if not os.path.exists(dest_dir):
64
 
            mkdir(dest_dir, perms=0o755)
65
62
        try:
66
 
            self.clone(source, dest_dir, branch)
67
 
        except GitCommandError as e:
68
 
            raise UnhandledSource(e.message)
 
63
            self.clone(source, dest_dir, branch, depth)
 
64
        except CalledProcessError as e:
 
65
            raise UnhandledSource(e)
69
66
        except OSError as e:
70
67
            raise UnhandledSource(e.strerror)
71
68
        return dest_dir