~quobyte/charms/trusty/quobyte-webconsole/trunk

« back to all changes in this revision

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

  • Committer: Bruno Ranieri
  • Date: 2016-07-13 14:50:01 UTC
  • Revision ID: bruno@quobyte.com-20160713145001-1h6cddu9sltlvx7w
Initial charm

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