1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
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.
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.
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/>.
18
from subprocess import check_call
19
from charmhelpers.fetch import (
22
filter_installed_packages,
25
from charmhelpers.core.host import mkdir
27
if filter_installed_packages(['git']) != []:
29
if filter_installed_packages(['git']) != []:
30
raise NotImplementedError('Unable to install git')
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', ''):
40
elif not url_parts.scheme:
41
return os.path.exists(os.path.join(source, '.git'))
45
def clone(self, source, dest, branch="master", depth=None):
46
if not self.can_handle(source):
47
raise UnhandledSource("Cannot handle {}".format(source))
49
if os.path.exists(dest):
50
cmd = ['git', '-C', dest, 'pull', source, branch]
52
cmd = ['git', 'clone', source, dest, '--branch', branch]
54
cmd.extend(['--depth', depth])
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]
61
dest_dir = os.path.join(dest, branch_name)
63
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
65
if not os.path.exists(dest_dir):
66
mkdir(dest_dir, perms=0o755)
68
self.clone(source, dest_dir, branch, depth)
70
raise UnhandledSource(e.strerror)