/usr/share/pyshared/Pyblosxom/cache/entrypickle.py is in pyblosxom 1.5.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #######################################################################
# This file is part of Pyblosxom.
#
# Copyright (C) 2003-2011 by the Pyblosxom team. See AUTHORS.
#
# Pyblosxom is distributed under the MIT license. See the file
# LICENSE for distribution details.
#######################################################################
"""
This cache driver creates pickled data as cache in a directory.
To use this driver, add the following configuration options in your config.py
py['cacheDriver'] = 'entrypickle'
py['cacheConfig'] = '/path/to/a/cache/directory'
If successful, you will see the cache directory filled up with files that ends
with .entryplugin extention in the drectory.
"""
from Pyblosxom import tools
from Pyblosxom.cache.base import BlosxomCacheBase
import cPickle as pickle
import os
from os import makedirs
from os.path import normpath, dirname, exists, abspath
class BlosxomCache(BlosxomCacheBase):
"""
This cache stores each entry as a separate pickle file of the
entry's contents.
"""
def __init__(self, req, config):
"""
Takes in a Pyblosxom request object and a configuration string
which determines where to store the pickle files.
"""
BlosxomCacheBase.__init__(self, req, config)
self._cachefile = ""
def load(self, entryid):
"""
Takes an entryid and keeps track of the filename. We only
open the file when it's requested with getEntry.
"""
BlosxomCacheBase.load(self, entryid)
filename = os.path.join(self._config, entryid.replace('/', '_'))
self._cachefile = filename + '.entrypickle'
def getEntry(self):
"""
Open the pickle file and return the data therein. If this
fails, then we return None.
"""
filep = None
try:
filep = open(self._cachefile, 'rb')
data = pickle.load(filep)
filep.close()
return data
except IOError:
return None
if filep:
filep.close()
def isCached(self):
"""
Check to see if the file is updated.
"""
return os.path.isfile(self._cachefile) and \
os.stat(self._cachefile)[8] >= os.stat(self._entryid)[8]
def saveEntry(self, entrydata):
"""
Save the data in the entry object to a pickle file.
"""
filep = None
try:
self.__makepath(self._cachefile)
filep = open(self._cachefile, "w+b")
entrydata.update({'realfilename': self._entryid})
pickle.dump(entrydata, filep, 1)
except IOError:
pass
if filep:
filep.close()
def rmEntry(self):
"""
Removes the pickle file for this entry if it exists.
"""
if os.path.isfile(self._cachefile):
os.remove(self._cachefile)
def keys(self):
"""
Returns a list of the keys found in this entrypickle instance.
This corresponds to the list of entries that are cached.
@returns: list of full paths to entries that are cached
@rtype: list of strings
"""
import re
keys = []
cached = []
if os.path.isdir(self._config):
cached = tools.walk(self._request,
self._config,
1,
re.compile(r'.*\.entrypickle$'))
for cache in cached:
cache_data = pickle.load(open(cache))
key = cache_data.get('realfilename', '')
if not key and os.path.isfile(cache):
os.remove(cache)
self.load(key)
if not self.isCached():
self.rmEntry()
else:
keys.append(key)
return keys
def __makepath(self, path):
"""
Creates the directory and all parent directories for a
specified path.
@param path: the path to create
@type path: string
@returns: the normalized absolute path
@rtype: string
"""
dpath = normpath(dirname(path))
if not exists(dpath):
makedirs(dpath)
return normpath(abspath(path))
|