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 charmhelpers.fetch import (
22
from charmhelpers.core.host import mkdir
26
raise ImportError('GitPython does not support Python 3')
31
from charmhelpers.fetch import apt_install
32
apt_install("python-git")
35
from git.exc import GitCommandError # noqa E402
38
class GitUrlFetchHandler(BaseFetchHandler):
39
"""Handler for git branches via generic and github URLs"""
40
def can_handle(self, source):
41
url_parts = self.parse_url(source)
42
# TODO (mattyw) no support for ssh git@ yet
43
if url_parts.scheme not in ('http', 'https', 'git'):
48
def clone(self, source, dest, branch, depth=None):
49
if not self.can_handle(source):
50
raise UnhandledSource("Cannot handle {}".format(source))
53
Repo.clone_from(source, dest, branch=branch, depth=depth)
55
Repo.clone_from(source, dest, branch=branch)
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)
69
except GitCommandError as e:
70
raise UnhandledSource(e.message)
72
raise UnhandledSource(e.strerror)