~charmers/charms/vivid/ubuntu/trunk

« back to all changes in this revision

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

  • Committer: Charles Butler
  • Date: 2015-03-27 15:01:57 UTC
  • mfrom: (7.2.2 ubuntu)
  • Revision ID: chuck@dasroot.net-20150327150157-qm15dcmaweqogadh
  Liang Chen 2015-02-19 provide a config option for ubuntu charm
  Liang Chen 2015-02-19 sync charmhelper

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 charmhelpers.fetch import (
 
19
    BaseFetchHandler,
 
20
    UnhandledSource
 
21
)
 
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
 
36
 
 
37
 
 
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'):
 
44
            return False
 
45
        else:
 
46
            return True
 
47
 
 
48
    def clone(self, source, dest, branch):
 
49
        if not self.can_handle(source):
 
50
            raise UnhandledSource("Cannot handle {}".format(source))
 
51
 
 
52
        repo = Repo.clone_from(source, dest)
 
53
        repo.git.checkout(branch)
 
54
 
 
55
    def install(self, source, branch="master", dest=None):
 
56
        url_parts = self.parse_url(source)
 
57
        branch_name = url_parts.path.strip("/").split("/")[-1]
 
58
        if dest:
 
59
            dest_dir = os.path.join(dest, branch_name)
 
60
        else:
 
61
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
 
62
                                    branch_name)
 
63
        if not os.path.exists(dest_dir):
 
64
            mkdir(dest_dir, perms=0o755)
 
65
        try:
 
66
            self.clone(source, dest_dir, branch)
 
67
        except GitCommandError as e:
 
68
            raise UnhandledSource(e.message)
 
69
        except OSError as e:
 
70
            raise UnhandledSource(e.strerror)
 
71
        return dest_dir