1
import twisted.internet.reactor
5
import cimple.core.config
7
from item import DataItem
13
def __init__(self, path, is_cache = True):
15
self.is_cache = is_cache
16
#pylint: disable-msg=E1101
17
twisted.internet.reactor.callWhenRunning(self.__purge)
19
def __path(self, name):
20
return os.path.join(self.path, name)
22
def __space_used(self):
24
for name in os.listdir(self.path):
25
result = result + os.stat(self.__path(name))[stat.ST_SIZE]
28
def __needs_purging(self):
30
return self.__space_used() > cimple.core.config.cache_size()
32
vfs = os.statvfs(self.path)
33
return vfs.f_bfree < vfs.f_blocks * 0.10
35
def __compare_access_times(self, one_file, other_file):
37
Compare access times of two files
39
return os.stat(self.__path(one_file))[stat.ST_ATIME] - os.stat(self.__path(other_file))[stat.ST_ATIME]
42
if self.__needs_purging():
44
file_list = os.listdir(self.path)
45
file_list.sort(cmp=self.__compare_access_times)
47
file_list = cimple.util.directory.Directory(self.path)
50
for name in file_list:
51
if self.__needs_purging():
53
data_item = DataItem()
54
data_item.from_base32(name)
55
if self.is_cache or not data_item.is_persistent():
56
if self.is_cache or not data_item.is_replicating():
57
os.remove(os.path.join(self.path, name))
59
if name.endswith('hoarded'):
60
if os.stat(os.path.join(self.path, name))[stat.ST_ATIME] < time.time() - 86400:
61
path = os.path.join(self.path, name)
63
#pylint: disable-msg=E1101
64
twisted.internet.reactor.callLater(self.FREQUENCY, self.__purge)