~hopem/charms/trusty/keystone/lp1476325

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-01-09 15:54:17 UTC
  • mfrom: (99 keystone-242900)
  • mto: This revision was merged to the branch mainline in revision 100.
  • Revision ID: liam.young@canonical.com-20150109155417-ev9c5l3hcx7lo4ec
Merged next in and resolved conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from charmhelpers.fetch import (
 
3
    BaseFetchHandler,
 
4
    UnhandledSource
 
5
)
 
6
from charmhelpers.core.host import mkdir
 
7
 
 
8
import six
 
9
if six.PY3:
 
10
    raise ImportError('GitPython does not support Python 3')
 
11
 
 
12
try:
 
13
    from git import Repo
 
14
except ImportError:
 
15
    from charmhelpers.fetch import apt_install
 
16
    apt_install("python-git")
 
17
    from git import Repo
 
18
 
 
19
 
 
20
class GitUrlFetchHandler(BaseFetchHandler):
 
21
    """Handler for git branches via generic and github URLs"""
 
22
    def can_handle(self, source):
 
23
        url_parts = self.parse_url(source)
 
24
        # TODO (mattyw) no support for ssh git@ yet
 
25
        if url_parts.scheme not in ('http', 'https', 'git'):
 
26
            return False
 
27
        else:
 
28
            return True
 
29
 
 
30
    def clone(self, source, dest, branch):
 
31
        if not self.can_handle(source):
 
32
            raise UnhandledSource("Cannot handle {}".format(source))
 
33
 
 
34
        repo = Repo.clone_from(source, dest)
 
35
        repo.git.checkout(branch)
 
36
 
 
37
    def install(self, source, branch="master", dest=None):
 
38
        url_parts = self.parse_url(source)
 
39
        branch_name = url_parts.path.strip("/").split("/")[-1]
 
40
        if dest:
 
41
            dest_dir = os.path.join(dest, branch_name)
 
42
        else:
 
43
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
 
44
                                    branch_name)
 
45
        if not os.path.exists(dest_dir):
 
46
            mkdir(dest_dir, perms=0o755)
 
47
        try:
 
48
            self.clone(source, dest_dir, branch)
 
49
        except OSError as e:
 
50
            raise UnhandledSource(e.strerror)
 
51
        return dest_dir