~ubuntu-branches/ubuntu/gutsy/moin/gutsy

« back to all changes in this revision

Viewing changes to MoinMoin/caching.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-02-14 16:09:24 UTC
  • mfrom: (0.2.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20060214160924-fyrx3gvknzqvt4vj
Tags: 1.5.2-1ubuntu1
Drop python2.3 package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
from MoinMoin import config
11
11
from MoinMoin.util import filesys
12
12
 
 
13
locking = 1
 
14
if locking:
 
15
    from MoinMoin.util import lock
 
16
    
13
17
class CacheEntry:
14
18
    def __init__(self, request, arena, key):
15
19
        """ init a cache entry
18
22
                          page local cache area
19
23
            @param key: under which key we access the cache content
20
24
        """
 
25
        self.request = request
21
26
        if isinstance(arena, str):
22
27
            self.arena_dir = os.path.join(request.cfg.cache_dir, arena)
23
28
            filesys.makeDirs(self.arena_dir)
25
30
            cache_dir = None
26
31
            self.arena_dir = arena.getPagePath('cache', check_create=1)
27
32
        self.key = key
28
 
 
 
33
        if locking:
 
34
            self.lock_dir = os.path.join(self.arena_dir, '__lock__')
 
35
            self.rlock = lock.ReadLock(self.lock_dir, 60.0)
 
36
            self.wlock = lock.WriteLock(self.lock_dir, 60.0)
 
37
        
29
38
    def _filename(self):
30
39
        return os.path.join(self.arena_dir, self.key)
31
40
 
39
48
            return 0
40
49
 
41
50
    def needsUpdate(self, filename, attachdir=None):
42
 
        if not self.exists(): return 1
 
51
        if not self.exists():
 
52
            return 1
43
53
 
44
54
        try:
45
55
            ctime = os.path.getmtime(self._filename())
61
71
 
62
72
    def copyto(self, filename):
63
73
        import shutil
64
 
        shutil.copyfile(filename, self._filename())
65
 
 
66
 
        try:
67
 
            os.chmod(self._filename(), 0666 & config.umask)
68
 
        except OSError:
69
 
            pass
 
74
        if not locking or locking and self.wlock.acquire(1.0):
 
75
            try:
 
76
                shutil.copyfile(filename, self._filename())
 
77
                try:
 
78
                    os.chmod(self._filename(), 0666 & config.umask)
 
79
                except OSError:
 
80
                    pass
 
81
            finally:
 
82
                if locking:
 
83
                    self.wlock.release()
 
84
        else:
 
85
            request.log("Can't acquire write lock in %s", self.lock_dir)
70
86
 
71
87
    def update(self, content, encode=False):
72
88
        if encode:
73
89
            content = content.encode(config.charset)
74
 
        open(self._filename(), 'wb').write(content)
75
 
 
76
 
        try:
77
 
            os.chmod(self._filename(), 0666 & config.umask)
78
 
        except OSError:
79
 
            pass
 
90
        if not locking or locking and self.wlock.acquire(1.0):
 
91
            try:
 
92
                f = open(self._filename(), 'wb')
 
93
                f.write(content)
 
94
                f.close()
 
95
                try:
 
96
                    os.chmod(self._filename(), 0666 & config.umask)
 
97
                except OSError:
 
98
                    pass
 
99
            finally:
 
100
                if locking:
 
101
                    self.wlock.release()
 
102
        else:
 
103
            request.log("Can't acquire write lock in %s", self.lock_dir)
80
104
 
81
105
    def remove(self):
82
106
        try:
85
109
            pass
86
110
 
87
111
    def content(self, decode=False):
88
 
        data = open(self._filename(), 'rb').read()
 
112
        if not locking or locking and self.rlock.acquire(1.0):
 
113
            try:
 
114
                f = open(self._filename(), 'rb')
 
115
                data = f.read()
 
116
                f.close()
 
117
            finally:
 
118
                if locking:
 
119
                    self.rlock.release()
 
120
        else:
 
121
            request.log("Can't acquire read lock in %s", self.lock_dir)
89
122
        if decode:
90
123
            data = data.decode(config.charset)
91
124
        return data