Mercurial > moin > 1.9
annotate MoinMoin/caching.py @ 3610:39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
author | Thomas Waldmann <tw AT waldmann-edv DOT de> |
---|---|
date | Sat, 17 May 2008 21:23:09 +0200 |
parents | ea5383222f2f |
children | 8cb2f4ebd45f b13a58a18dac |
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 |
1918
bb2e053067fb
fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1898
diff
changeset
|
5 @copyright: 2001-2004 by Juergen Hermann <jh@web.de>, |
3127
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
6 2006-2008 MoinMoin:ThomasWaldmann |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
7 @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
|
8 """ |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
9 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
10 import os |
1647
c0cb0884f7ec
use tempfile.mkstemp to avoid renaming trouble (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1638
diff
changeset
|
11 import tempfile |
1316
c4f6af5ee295
Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
1309
diff
changeset
|
12 |
3127
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
13 from MoinMoin import log |
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
14 logging = log.getLogger(__name__) |
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
15 |
1898
b5e94fec8669
caching: mkstemp uses 0600 to create files, we need chmod to change them to our preferred file mode
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1793
diff
changeset
|
16 from MoinMoin import config |
1793
2a4caa295346
Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1791
diff
changeset
|
17 from MoinMoin.util import filesys, lock, pickle, PICKLE_PROTOCOL |
931
141083b64fb1
whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
902
diff
changeset
|
18 |
1316
c4f6af5ee295
Mute the tempnam warning in the caching module.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents:
1309
diff
changeset
|
19 |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
20 class CacheError(Exception): |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
21 """ raised if we have trouble reading or writing to the cache """ |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
22 pass |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
23 |
1936
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
24 def get_arena_dir(request, arena, scope): |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
25 if scope == 'page_or_wiki': # XXX DEPRECATED, remove later |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
26 if isinstance(arena, str): |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
27 return os.path.join(request.cfg.cache_dir, request.cfg.siteid, arena) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
28 else: # arena is in fact a page object |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
29 return arena.getPagePath('cache', check_create=1) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
30 elif scope == 'item': # arena is a Page instance |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
31 # we could move cache out of the page directory and store it to cache_dir |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
32 return arena.getPagePath('cache', check_create=1) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
33 elif scope == 'wiki': |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
34 return os.path.join(request.cfg.cache_dir, request.cfg.siteid, arena) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
35 elif scope == 'farm': |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
36 return os.path.join(request.cfg.cache_dir, '__common__', arena) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
37 return None |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
38 |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
39 |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
40 def get_cache_list(request, arena, scope): |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
41 arena_dir = get_arena_dir(request, arena, scope) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
42 try: |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
43 return filesys.dclistdir(arena_dir) |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
44 except OSError: |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
45 return [] |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
46 |
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
47 |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
48 class CacheEntry: |
2238
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
49 def __init__(self, request, arena, key, scope='page_or_wiki', do_locking=True, |
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
50 use_pickle=False, use_encode=False): |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
51 """ 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
|
52 @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
|
53 @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
|
54 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
|
55 @param key: under which key we access the cache content |
681
2d5883e4b82c
scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
680
diff
changeset
|
56 @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
|
57 'item' - an item local cache |
2d5883e4b82c
scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
680
diff
changeset
|
58 'wiki' - a wiki local cache |
2d5883e4b82c
scope == {'item', 'wiki', 'farm'} for caching
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
680
diff
changeset
|
59 'farm' - a cache for the whole farm |
2238
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
60 @param do_locking: if there should be a lock, normally True |
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
61 @param use_pickle: if data should be pickled/unpickled (nice for arbitrary cache content) |
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
62 @param use_encode: if data should be encoded/decoded (nice for readable cache files) |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
63 """ |
412
481c432b3d11
fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents:
402
diff
changeset
|
64 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
|
65 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
|
66 self.locking = do_locking |
1638
5070d0358544
caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1637
diff
changeset
|
67 self.use_pickle = use_pickle |
2238
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
68 self.use_encode = use_encode |
1936
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
69 self.arena_dir = get_arena_dir(request, arena, scope) |
1429
1d55eff67af6
check for non-existing dir before calling os.makedirs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1428
diff
changeset
|
70 if not os.path.exists(self.arena_dir): |
1428
8a89649ab5f0
remove util.filesys.make[dD]irs - not needed any more due to os.umask() use
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1317
diff
changeset
|
71 os.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
|
72 if self.locking: |
412
481c432b3d11
fix some error handlers, logging for caching lock failures
Thomas Waldmann <tw@waldmann-edv.de>
parents:
402
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
77 def _filename(self): |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
78 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
|
79 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
80 def exists(self): |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
81 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
|
82 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
83 def mtime(self): |
3610
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
84 # DEPRECATED for checking a changed on-disk cache, please use |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
85 # self.uid() for this, see below |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
86 try: |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
87 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
|
88 except (IOError, OSError): |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
89 return 0 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
90 |
3610
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
91 def uid(self): |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
92 """ Return a value that likely changes when the on-disk cache was updated. |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
93 |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
94 See docstring of MoinMoin.util.filesys.fuid for details. |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
95 """ |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
96 return filesys.fuid(self._filename()) |
39456a5dc4e8
fix failure of detection of on-disk cache updates (this fixes unreliable dicts/groups tests), remove unused Page attribute
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
3127
diff
changeset
|
97 |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
98 def needsUpdate(self, filename, attachdir=None): |
1681
fc03cd8ff0f3
avoid accessing invalid page revisions, tune caching code (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1647
diff
changeset
|
99 # following code is not necessary. will trigger exception and give same result |
fc03cd8ff0f3
avoid accessing invalid page revisions, tune caching code (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1647
diff
changeset
|
100 #if not self.exists(): |
fc03cd8ff0f3
avoid accessing invalid page revisions, tune caching code (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1647
diff
changeset
|
101 # return 1 |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
102 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
103 try: |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
104 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
|
105 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
|
106 except os.error: |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
107 return 1 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
108 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
109 needsupdate = ftime > ctime |
931
141083b64fb1
whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
902
diff
changeset
|
110 |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
111 # 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
|
112 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
|
113 try: |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
114 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
|
115 except os.error: |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
116 ftime2 = 0 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
117 needsupdate = ftime2 > ctime |
931
141083b64fb1
whitespace and minor style changes only
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
902
diff
changeset
|
118 |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
119 return needsupdate |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
120 |
1920
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
121 # def copyto(self, filename): |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
122 # # currently unused function |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
123 # import shutil |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
124 # tmpfname = self._tmpfilename() |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
125 # fname = self._filename() |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
126 # if not self.locking or self.locking and self.wlock.acquire(1.0): |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
127 # try: |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
128 # shutil.copyfile(filename, tmpfname) |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
129 # # this is either atomic or happening with real locks set: |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
130 # filesys.rename(tmpfname, fname) |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
131 # finally: |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
132 # if self.locking: |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
133 # self.wlock.release() |
b06ef2a53efa
'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1918
diff
changeset
|
134 # else: |
3127
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
135 # logging.error("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
|
136 |
1638
5070d0358544
caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1637
diff
changeset
|
137 def update(self, content): |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
138 try: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
139 fname = self._filename() |
1638
5070d0358544
caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1637
diff
changeset
|
140 if self.use_pickle: |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
141 content = pickle.dumps(content, PICKLE_PROTOCOL) |
2238
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
142 elif self.use_encode: |
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
143 content = content.encode(config.charset) |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
144 if not self.locking or self.locking and self.wlock.acquire(1.0): |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
145 try: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
146 # we do not write content to old inode, but to a new file |
1936
adcdb9cc9258
caching.get_cache_list
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
1920
diff
changeset
|
147 # so we don't need to lock when we just want to read the file |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
148 # (at least on POSIX, this works) |
1647
c0cb0884f7ec
use tempfile.mkstemp to avoid renaming trouble (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1638
diff
changeset
|
149 tmp_handle, tmp_fname = tempfile.mkstemp('.tmp', self.key, self.arena_dir) |
c0cb0884f7ec
use tempfile.mkstemp to avoid renaming trouble (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1638
diff
changeset
|
150 os.write(tmp_handle, content) |
c0cb0884f7ec
use tempfile.mkstemp to avoid renaming trouble (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1638
diff
changeset
|
151 os.close(tmp_handle) |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
152 # this is either atomic or happening with real locks set: |
1647
c0cb0884f7ec
use tempfile.mkstemp to avoid renaming trouble (ported from 1.5)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1638
diff
changeset
|
153 filesys.rename(tmp_fname, fname) |
1898
b5e94fec8669
caching: mkstemp uses 0600 to create files, we need chmod to change them to our preferred file mode
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1793
diff
changeset
|
154 filesys.chmod(fname, 0666 & config.umask) # fix mode that mkstemp chose |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
155 finally: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
156 if self.locking: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
157 self.wlock.release() |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
158 else: |
3127
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
159 logging.error("Can't acquire write lock in %s" % self.lock_dir) |
2841
93be75db2051
handle out-of-space errors in PageEditor and caching, avoid 'current' corruption (thanks to TheAnarcat)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2238
diff
changeset
|
160 except (pickle.PicklingError, OSError, IOError, ValueError), err: |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
161 raise CacheError(str(err)) |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
162 |
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 else: |
3127
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
174 logging.error("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
|
175 |
1638
5070d0358544
caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1637
diff
changeset
|
176 def content(self): |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
177 try: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
178 if not self.locking or self.locking and self.rlock.acquire(1.0): |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
179 try: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
180 f = open(self._filename(), 'rb') |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
181 data = f.read() |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
182 f.close() |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
183 finally: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
184 if self.locking: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
185 self.rlock.release() |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
186 else: |
3127
ea5383222f2f
refactored misc. modules to use own logger instead of request.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
2841
diff
changeset
|
187 logging.error("Can't acquire read lock in %s" % self.lock_dir) |
1638
5070d0358544
caching: move use_pickle to __init__, remove unused encode/decode args from .content()/.update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1637
diff
changeset
|
188 if self.use_pickle: |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
189 data = pickle.loads(data) |
2238
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
190 elif self.use_encode: |
529a3c5e6d0e
add use_encode parameter to caching - useful for readable cache files. this re-adds similar functionality that was removed in changeset 5070d0358544
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1936
diff
changeset
|
191 data = data.decode(config.charset) |
1637
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
192 return data |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
193 except (pickle.UnpicklingError, IOError, EOFError, ValueError), err: |
47a173ae5401
caching module: use_pickle argument for .content() and .update()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
1429
diff
changeset
|
194 raise CacheError(str(err)) |
0
77665d8e2254
tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff
changeset
|
195 |