view buttercup/source.py @ 22:1378e96c65ac

git updater method
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 30 May 2011 14:06:29 -0700
parents 1f54294629f9
children 425a3c73ac1d
line wrap: on
line source

import os
try:
    from subprocess import check_call as call
except:
    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))

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()):
            assert os.path.exists(os.path.join(self.directory(), '.hg'))
            call(['hg', 'pull'], cwd=self.directory())
            call(['hg', 'update'], cwd=self.directory())
        else:
            if not os.path.exists(self.srcdir):
                os.makedirs(self.srcdir)
            call(['hg', 'clone', self.uri], cwd=self.srcdir)
            # TODO: add a more intelligent .hg/hgrc

    __call__ = update


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

    __call__ = update