view buttercup/source.py @ 45:048e391423a1

note about git config
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 05 Apr 2013 13:37:24 -0700
parents 693f209fdbaa
children b717de8b384f
line wrap: on
line source

"""
VCS sources for the flower project
"""

import os
try:
    from subprocess import check_call as call
except ImportError:
    from subprocess import call

class Source(object):
    """abstract base class for VCS source"""
    def __init__(self, uri, srcdir=None):
        self.uri = uri
        self.srcdir = srcdir or os.getcwd()

    @classmethod
    def directory_name(cls, uri):
        """return relative directory name from self.uri"""
        return uri.rstrip('/').split('/')[-1]

    def directory(self):
        return os.path.join(self.srcdir, self.directory_name(self.uri))

    def update(self):
        raise NotImplementedError("`Source` is an abstract base class")

    def __call__(self):
        """convenience wrapper to update"""
        self.update()


class HgSource(Source):
    """mercurial source"""

    def update(self):
        """updates a checkout or does one if it does not exist"""
        if os.path.exists(self.directory()):
            hgdir = os.path.join(self.directory(), '.hg')
            assert os.path.exists(hgdir), "Not an hg directory: %s" % hgdir
            try:
                call(['hg', 'pull'], cwd=self.directory())
                call(['hg', 'update'], cwd=self.directory())
            except:
                print 'Directory: %s' % self.directory()
                raise
        else:
            if not os.path.exists(self.srcdir):
                os.makedirs(self.srcdir)
            call(['hg', 'clone', self.uri], cwd=self.srcdir)

            # add a more intelligent .hg/hgrc
            # TODO


class GitSource(Source):

    @classmethod
    def directory_name(cls, uri):
        ext = '.git'
        if uri.endswith(uri):
            uri = uri[:-len(ext)]
        return Source.directory_name(uri)

    def update(self):
        """updates a checkout or does one if it does not exist"""
        if os.path.exists(self.directory()):
            assert os.path.exists(os.path.join(self.directory(), '.git'))
            call(['git', 'pull'], cwd=self.directory())
        else:
            if not os.path.exists(self.srcdir):
                os.makedirs(self.srcdir)
            call(['git', 'clone', self.uri], cwd=self.srcdir)
            # TODO: add a more intelligent .git/config, e.g.
# [core]
#         repositoryformatversion = 0
#         filemode = true
#         bare = false
#         logallrefupdates = true
# [remote "origin"]
#         fetch = +refs/heads/*:refs/remotes/origin/*
#         url = git://github.com/mozilla/toolbox.git
#         pushurl = git@github.com:mozilla/toolbox.git
# [branch "master"]
#         remote = origin
#         merge = refs/heads/master
# see: http://git.661346.n2.nabble.com/Separate-default-push-pull-td4555821.html

def sources(source_dict, **kwargs):
    """
    return source objects from a dict:
    {'hg': ['http://k0s.org/hg/pyloader'],
     'git': ['git://github.com/mozilla/toolbox.git']}
    """
    retval = []
    for repo_type, sources in source_dict.items():
        if repo_type == 'hg':
            retval.extend([HgSource(source, **kwargs)
                           for source in sources])
        elif repo_type == 'git':
            retval.extend([GitSource(source, **kwargs)
                           for source in sources])
        else:
            raise AssertionError("Source type unknown: %s" % repo_type)
    return retval