Mercurial > hg > buttercup
view buttercup/source.py @ 26:8179c05d7692
note ABC
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 30 May 2011 21:13:02 -0700 |
parents | a54543838686 |
children | 901755a65249 |
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)) def update(self): raise NotImplementedError("`Source` is an abstract base class") 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 def sources(source_dict, **kwargs): 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