~openstack-charmers-next/charms/vivid/hacluster/trunk

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2016-03-30 09:07:46 UTC
  • mfrom: (63.1.4 trunk)
  • Revision ID: liam.young@canonical.com-20160330090746-rwwe91yjqi9j9ry3
[gnuoy, r=james-page] Add pause/resume actions

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
import os
 
18
from subprocess import check_call, CalledProcessError
18
19
from charmhelpers.fetch import (
19
20
    BaseFetchHandler,
20
 
    UnhandledSource
 
21
    UnhandledSource,
 
22
    filter_installed_packages,
 
23
    apt_install,
21
24
)
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
 
25
 
 
26
if filter_installed_packages(['git']) != []:
 
27
    apt_install(['git'])
 
28
    if filter_installed_packages(['git']) != []:
 
29
        raise NotImplementedError('Unable to install git')
36
30
 
37
31
 
38
32
class GitUrlFetchHandler(BaseFetchHandler):
40
34
    def can_handle(self, source):
41
35
        url_parts = self.parse_url(source)
42
36
        # TODO (mattyw) no support for ssh git@ yet
43
 
        if url_parts.scheme not in ('http', 'https', 'git'):
 
37
        if url_parts.scheme not in ('http', 'https', 'git', ''):
44
38
            return False
 
39
        elif not url_parts.scheme:
 
40
            return os.path.exists(os.path.join(source, '.git'))
45
41
        else:
46
42
            return True
47
43
 
48
 
    def clone(self, source, dest, branch, depth=None):
 
44
    def clone(self, source, dest, branch="master", depth=None):
49
45
        if not self.can_handle(source):
50
46
            raise UnhandledSource("Cannot handle {}".format(source))
51
47
 
52
 
        if depth:
53
 
            Repo.clone_from(source, dest, branch=branch, depth=depth)
 
48
        if os.path.exists(dest):
 
49
            cmd = ['git', '-C', dest, 'pull', source, branch]
54
50
        else:
55
 
            Repo.clone_from(source, dest, branch=branch)
 
51
            cmd = ['git', 'clone', source, dest, '--branch', branch]
 
52
            if depth:
 
53
                cmd.extend(['--depth', depth])
 
54
        check_call(cmd)
56
55
 
57
56
    def install(self, source, branch="master", dest=None, depth=None):
58
57
        url_parts = self.parse_url(source)
62
61
        else:
63
62
            dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
64
63
                                    branch_name)
65
 
        if not os.path.exists(dest_dir):
66
 
            mkdir(dest_dir, perms=0o755)
67
64
        try:
68
65
            self.clone(source, dest_dir, branch, depth)
69
 
        except GitCommandError as e:
 
66
        except CalledProcessError as e:
70
67
            raise UnhandledSource(e)
71
68
        except OSError as e:
72
69
            raise UnhandledSource(e.strerror)