sphinx-gsoc2009 / sphinx / web /

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-

    Handle MD5 sums database and checking.
    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.

import cPickle
from sphinx.errors import SphinxError

class PickleLoadSave(object):
    """Basic class for loading and saving pickle files. Contains two methods:
    dbread() and dbsave()."""
    def dbread(self, dbfile):
            w = open(dbfile)
            data = cPickle.load(w)
            return {}
        return data

    def dbsave(self, dbfile, data):
            w = open(dbfile, 'w')
            cPickle.dump(data, w)
        except Exception, err:
            raise SphinxError(err)

class RepoSums(PickleLoadSave):
    def __init__(self, sumsfile):
        self.sumsfile = sumsfile

    def add_record(self, filename, md5sum):
        data = self.dbread(self.sumsfile)
        data[filename] = md5sum
        self.dbsave(self.sumsfile, data)

    def get_record(self, filename):
        data = self.dbread(self.sumsfile)
        return data.get(filename)

    def sum_changed(self, filename, md5sum):
        """Compare the pair filename:md5sum with the pair from the database.

        Return True if the pair with which the method was invoked is not the
        same as the pair saved in the database (also, return True, if the pair
        did not exist in the database before the method was invoked).
        In any of above cases - add a new pair to the database.

        Return False otherwise (e.g. the pairs are the same)."""
        data = self.dbread(self.sumsfile)
        if not data:
            self.add_record(filename, md5sum)
            return True

        dbsum = data.get(filename)
        if dbsum != md5sum:
            self.add_record(filename, md5sum)
            return True
            return False