~1chb1n/charms/trusty/ceph-osd/15.10-stable-flip-tests-helper-syncs

« back to all changes in this revision

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

  • Committer: Corey Bryant
  • Date: 2014-10-21 12:46:40 UTC
  • mfrom: (30.1.1 ceph-osd-next)
  • Revision ID: corey.bryant@canonical.com-20141021124640-6kz32v3cmxnhr7xc
[gnuoy,r=coreycb] Sync charm-helpers.

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
try:
 
9
    from git import Repo
 
10
except ImportError:
 
11
    from charmhelpers.fetch import apt_install
 
12
    apt_install("python-git")
 
13
    from git import Repo
 
14
 
 
15
 
 
16
class GitUrlFetchHandler(BaseFetchHandler):
 
17
    """Handler for git branches via generic and github URLs"""
 
18
    def can_handle(self, source):
 
19
        url_parts = self.parse_url(source)
 
20
        #TODO (mattyw) no support for ssh git@ yet
 
21
        if url_parts.scheme not in ('http', 'https', 'git'):
 
22
            return False
 
23
        else:
 
24
            return True
 
25
 
 
26
    def clone(self, source, dest, branch):
 
27
        if not self.can_handle(source):
 
28
            raise UnhandledSource("Cannot handle {}".format(source))
 
29
 
 
30
        repo = Repo.clone_from(source, dest)
 
31
        repo.git.checkout(branch)
 
32
 
 
33
    def install(self, source, branch="master"):
 
34
        url_parts = self.parse_url(source)
 
35
        branch_name = url_parts.path.strip("/").split("/")[-1]
 
36
        dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
 
37
                                branch_name)
 
38
        if not os.path.exists(dest_dir):
 
39
            mkdir(dest_dir, perms=0755)
 
40
        try:
 
41
            self.clone(source, dest_dir, branch)
 
42
        except OSError as e:
 
43
            raise UnhandledSource(e.strerror)
 
44
        return dest_dir