view commitwatcher/store.py @ 45:32cf3d3469c3 default tip

assert
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 07 Nov 2013 14:19:06 -0800
parents ef90a853afd1
children
line wrap: on
line source

"""
storage classes for commits and related data
"""

from abc import abstractmethod

__all__ = ['CommitStore', 'MemoryStore']

class CommitStore(object):
    """
    ABC for commit storage
    """

    def __init__(self, verbose=True, events=None):
        self.verbose = verbose
        self.events = events

    @abstractmethod
    def __contains__(self, revision):
        """if a particular revision is already added"""

    @abstractmethod
    def store(self, commit):
        """store a commit"""

    def add(self, commit):
        """adds a commit to the store"""
        if commit.revision not in self:
            return
        self.store(commit)

    def paths(self):
        """
        return paths touched by commits
        """

        paths = set()
        for commit in commits:
            for f in commit.files:
                paths.update(self.ancestry(f))
        return paths


class MemoryStore(CommitStore):
    """store in volatile memory"""
    # volatile!

    def __init__(self, *args, **kwargs):
        CommitStore.__init__(self, *args, **kwargs)
        self.path_to_commit = {}
        self._commits = []

    def store(self, commit):
        paths = self.paths(commit)
        for path in paths:
            self.path_to_commit.set_default(path, []).append(commit)
        raise NotImplementedError