view buttercup/source.py @ 20:60ddc49acd5f

add partial checkout method to hg
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 30 May 2011 13:49:51 -0700
parents d3b1bf9d8235
children 1f54294629f9
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:
            pass

class GitSource(Source):

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