~tdelaet/cimple/old-codebase

« back to all changes in this revision

Viewing changes to src/cimple/core/storage/purger.py

  • Committer: Thomas Delaet
  • Date: 2010-01-07 14:14:32 UTC
  • Revision ID: thomas@cole-20100107141432-yhker27v3pmn62uo
first phase of rewrite with focus on storage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import twisted.internet.reactor
2
 
import os
3
 
import os.path
4
 
import stat
5
 
import cimple.core.config
6
 
import time
7
 
from item import DataItem
8
 
 
9
 
class Purger:
10
 
        
11
 
        FREQUENCY = 3600
12
 
        
13
 
        def __init__(self, path, is_cache = True):
14
 
                self.path = path
15
 
                self.is_cache = is_cache
16
 
                #pylint: disable-msg=E1101
17
 
                twisted.internet.reactor.callWhenRunning(self.__purge)
18
 
        
19
 
        def __path(self, name):
20
 
                return os.path.join(self.path, name)
21
 
        
22
 
        def __space_used(self):
23
 
                result = 0
24
 
                for name in os.listdir(self.path):
25
 
                        result = result + os.stat(self.__path(name))[stat.ST_SIZE]
26
 
                return result
27
 
 
28
 
        def __needs_purging(self):
29
 
                if self.is_cache:
30
 
                        return self.__space_used() > cimple.core.config.cache_size()
31
 
                else:
32
 
                        vfs = os.statvfs(self.path)
33
 
                        return vfs.f_bfree < vfs.f_blocks * 0.10
34
 
                        
35
 
        def __compare_access_times(self, one_file, other_file):
36
 
                """
37
 
                Compare access times of two files
38
 
                """
39
 
                return os.stat(self.__path(one_file))[stat.ST_ATIME] - os.stat(self.__path(other_file))[stat.ST_ATIME]  
40
 
        
41
 
        def __purge(self):
42
 
                if self.__needs_purging():
43
 
                        if self.is_cache:
44
 
                                file_list = os.listdir(self.path)
45
 
                                file_list.sort(cmp=self.__compare_access_times)
46
 
                        else:
47
 
                                file_list = cimple.util.directory.Directory(self.path)
48
 
                else:
49
 
                        file_list = []
50
 
                for name in file_list:
51
 
                        if self.__needs_purging():
52
 
                                if not '.' in name:
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))
58
 
                                else:
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)
62
 
                                                        os.remove(path)
63
 
                #pylint: disable-msg=E1101
64
 
                twisted.internet.reactor.callLater(self.FREQUENCY, self.__purge)