summaryrefslogtreecommitdiffstats
path: root/combox/silo.py
blob: a2a6b9b85226a47dc548fb4eeb5587050ba7a51e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#    Copyright (C) 2015 Combox author(s). See AUTHORS.
#
#    This file is part of Combox.
#
#   Combox is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Combox is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Combox (see COPYING).  If not, see
#   <http://www.gnu.org/licenses/>.

import pickledb

from os import path
from threading import Lock

from combox.file import hash_file

class ComboxSilo(object):
    """The Combox silo.

    Helps keep track of files in combox directory.
    """


    def __init__(self, config):
        """config: a dictinary which contains combox configuration.

        """
        self.config = config

        silo = path.join(config['silo_dir'], 'silo.db')
        self.db = pickledb.load(silo, True)

        self.lock = Lock()


    def update(self, filep):
        """Update filep's info in db

        filep and the hash of its content is written to the db.

        filep: path to the file in combox directory.

        """
        with self.lock:
            fhash = hash_file(filep)
            return self.db.set(filep, fhash)


    def keys(self):
        """Returns a list of all keys in db."""
        # this is why Redis or some other key-value DB should be used
        # instead of PickleDB
        with self.lock:
            return self.db.db.keys()


    def remove(self, filep):
        """Removes filep from db.

        filep: path to the file in combox directory.

        """
        try:
            with self.lock:
                return self.db.rem(filep)
        except KeyError, e:
            # means `filep' not present in db.
            return False


    def exists(self, filep):
        """Checks if filep's info is stored in db.

        Returns True if filep's info is in db; False otherwise.

        filep: path to the file in combox directory.

        """

        with self.lock:
            if self.db.get(filep) is None:
                return False
            else:
                return True


    def stale(self, filep, fhash=None):
        """Returns True if filep's hash is different from the hash stored in db.

        Returns None, if filep's info is not yet stored in db.
        Returns False, if filep's hash has not changed it.

        filep: path to the file in combox directory.
        fhash: If not None, it is assumed to be filep's hash.
        """

        if not fhash:
            fhash = hash_file(filep)

        with self.lock:
            fhash_in_db = self.db.get(filep)

        if fhash_in_db is None:
            return None
        elif fhash == fhash_in_db:
            return False
        else:
            return True