annotate MoinMoin/caching.py @ 1317:76a76def8687

remove most chmod calls, use 1 os.umask() call in request module instead (port from 1.5)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 18 Aug 2006 21:20:15 +0200
parents c4f6af5ee295
children 8a89649ab5f0
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin caching module
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
7 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 import os
1316
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
10 import warnings
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
11
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12 from MoinMoin import config
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
13 from MoinMoin.util import filesys, lock
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
14
1316
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
15 # filter the tempname warning because we create the tempfile only in directories
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
16 # where only we should have write access initially
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
17 warnings.filterwarnings("ignore", "tempnam.*security", RuntimeWarning, "MoinMoin.caching")
c4f6af5ee295 Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1309
diff changeset
18
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19 class CacheEntry:
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
20 def __init__(self, request, arena, key, scope='page_or_wiki', do_locking=True):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
21 """ init a cache entry
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 @param request: the request object
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
23 @param arena: either a string or a page object, when we want to use
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24 page local cache area
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 @param key: under which key we access the cache content
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
26 @param lock: if there should be a lock, normally True
681
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
27 @param scope: the scope where we are caching:
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
28 'item' - an item local cache
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
29 'wiki' - a wiki local cache
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
30 'farm' - a cache for the whole farm
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
31 """
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
32 self.request = request
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
33 self.key = key
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
34 self.locking = do_locking
683
a2beb43407ea deprecate usage of CacheEntry without explicit scope
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 681
diff changeset
35 if scope == 'page_or_wiki': # XXX DEPRECATED, remove later
680
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
36 if isinstance(arena, str):
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
37 self.arena_dir = os.path.join(request.cfg.cache_dir, request.cfg.siteid, arena)
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
38 filesys.makeDirs(self.arena_dir)
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
39 else: # arena is in fact a page object
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
40 self.arena_dir = arena.getPagePath('cache', check_create=1)
681
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
41 elif scope == 'item': # arena is a Page instance
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
42 # we could move cache out of the page directory and store it to cache_dir
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
43 self.arena_dir = arena.getPagePath('cache', check_create=1)
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
44 elif scope == 'wiki':
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
45 self.arena_dir = os.path.join(request.cfg.cache_dir, request.cfg.siteid, arena)
2d5883e4b82c scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 680
diff changeset
46 filesys.makeDirs(self.arena_dir)
680
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
47 elif scope == 'farm':
d6556513c4d3 add scope param to CacheEntry, scope=='farm' uses farmwide caching, use it for i18n
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 463
diff changeset
48 self.arena_dir = os.path.join(request.cfg.cache_dir, '__common__', arena)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
49 filesys.makeDirs(self.arena_dir)
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
50 if self.locking:
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
51 self.lock_dir = os.path.join(self.arena_dir, '__lock__')
1309
238c43cb8877 lock: LazyReadLock, LazyWriteLock == do nothing. Only for POSIX OS and POSIX style of code using that kind of locks (see comments there).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1307
diff changeset
52 self.rlock = lock.LazyReadLock(self.lock_dir, 60.0)
238c43cb8877 lock: LazyReadLock, LazyWriteLock == do nothing. Only for POSIX OS and POSIX style of code using that kind of locks (see comments there).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1307
diff changeset
53 self.wlock = lock.LazyWriteLock(self.lock_dir, 60.0)
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
54
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
55 def _filename(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
56 return os.path.join(self.arena_dir, self.key)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
57
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
58 def _tmpfilename(self):
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
59 return os.tempnam(self.arena_dir, self.key)
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
60
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
61 def exists(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
62 return os.path.exists(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
64 def mtime(self):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
65 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
66 return os.path.getmtime(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
67 except (IOError, OSError):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
68 return 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
69
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
70 def needsUpdate(self, filename, attachdir=None):
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
71 if not self.exists():
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
72 return 1
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
73
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
74 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
75 ctime = os.path.getmtime(self._filename())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
76 ftime = os.path.getmtime(filename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
77 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
78 return 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
79
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
80 needsupdate = ftime > ctime
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
81
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
82 # if a page depends on the attachment dir, we check this, too:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
83 if not needsupdate and attachdir:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
84 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
85 ftime2 = os.path.getmtime(attachdir)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
86 except os.error:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
87 ftime2 = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
88 needsupdate = ftime2 > ctime
931
141083b64fb1 whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 902
diff changeset
89
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
90 return needsupdate
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
91
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
92 def copyto(self, filename):
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
93 # currently unused function
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
94 import shutil
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
95 tmpfname = self._tmpfilename()
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
96 fname = self._filename()
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
97 if not self.locking or self.locking and self.wlock.acquire(1.0):
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
98 try:
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
99 shutil.copyfile(filename, tmpfname)
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
100 # this is either atomic or happening with real locks set:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
101 filesys.rename(tmpfname, fname)
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
102 finally:
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
103 if self.locking:
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
104 self.wlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
105 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
106 self.request.log("Can't acquire write lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
107
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
108 def update(self, content, encode=False):
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
109 tmpfname = self._tmpfilename()
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
110 fname = self._filename()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
111 if encode:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
112 content = content.encode(config.charset)
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
113 if not self.locking or self.locking and self.wlock.acquire(1.0):
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
114 try:
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
115 # we do not write content to old inode, but to a new file
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
116 # se we don't need to lock when we just want to read the file
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
117 # (at least on POSIX, this works)
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
118 f = open(tmpfname, 'wb')
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
119 f.write(content)
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
120 f.close()
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
121 # this is either atomic or happening with real locks set:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
122 filesys.rename(tmpfname, fname)
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
123 finally:
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
124 if self.locking:
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
125 self.wlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
126 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
127 self.request.log("Can't acquire write lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
128
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
129 def remove(self):
1307
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
130 if not self.locking or self.locking and self.wlock.acquire(1.0):
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
131 try:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
132 try:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
133 os.remove(self._filename())
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
134 except OSError:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
135 pass
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
136 finally:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
137 if self.locking:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
138 self.wlock.release()
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
139 else:
6bc162aba29d caching: update contents by tmpfile+rename, not touching old inode content. add locking for .remove() call. small fixes to lock code.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 931
diff changeset
140 self.request.log("Can't acquire write lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
141
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
142 def content(self, decode=False):
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
143 if not self.locking or self.locking and self.rlock.acquire(1.0):
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
144 try:
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
145 f = open(self._filename(), 'rb')
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
146 data = f.read()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
147 f.close()
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
148 finally:
902
6cc24670fd0f Speeded up linkto: search by avoiding read locks on pagelinks retrieval.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 683
diff changeset
149 if self.locking:
402
c04edbe29fac add locking to caching module
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
150 self.rlock.release()
412
481c432b3d11 fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents: 402
diff changeset
151 else:
463
051f79718bdd fixing yet another bug in error logging calls
Thomas Waldmann <tw@waldmann-edv.de>
parents: 458
diff changeset
152 self.request.log("Can't acquire read lock in %s" % self.lock_dir)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
153 if decode:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
154 data = data.decode(config.charset)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
155 return data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
156