def cb_start(args): """ We want to grab the lock file so that we don't lose entries in between. """ request = args["request"] data = request.getData() config = request.getConfiguration() datadir = config["datadir"] filename = datadir + "/viewcounts.dat" try: if os.path.isfile(filename): f = open(filename, "r+") tools.lock(f, tools.LOCK_NB | tools.LOCK_EX) p = pickle.load(f) f.seek(0, 0) else: f = open(filename, "w") tools.lock(f, tools.LOCK_NB | tools.LOCK_EX) p = {} except: f = None p = {} data["viewcount_fp"] = f data["viewcount_picklefile"] = p
def get_cache(filename): """Load the cache of a file @param filename: The name of the file to check for a cache @type filename: String @returns: The associated cache Dictionary if it exists, else {} """ try: f = file(filename, 'rb') tools.lock(f, tools.LOCK_EX) c = cPickle.load(f) f.close() return c except IOError: return {}
def put_cache(cache, filename): """Write L{cache} to L{filename} @param cache: Dictionary to store in cache @type cache: Dictionary @param filename: name of file to put the cache in @type filename: String @returns: None """ f = file(filename, 'wb') tools.lock(f, tools.LOCK_EX) cb = cPickle.Pickler(f, -1) cb.dump(cache) f.close()
def cb_prepare(args): """ Callback registered with prepareChain. This does all the work @param args: args dict containing the request @type args: dict """ request = args["request"] config = request.getConfiguration() data = request.getData() httpData = request.getHttp() datadir = config["datadir"] try: f = open(datadir + "/logfile.dat", "r+") tools.lock(f, tools.LOCK_EX) stats = pickle.load(f) f.seek(0, 0) except: f = open(datadir + "/logfile.dat", "w") tools.lock(f, tools.LOCK_EX) stats = PyblStats(config) stats._request = request stats._config = config stats._referrer_length = int(config.get('referrer_length', 15)) stats._num_referrers = int(config.get('num_referrers', 15)) stats.addReferer(httpData.get('HTTP_REFERER', '-')) stats.addDestination(httpData.get('REQUEST_URI', '-')) stats.addVisitor(httpData.get('REMOTE_ADDR', '-')) data["referrers"] = stats.genReferrers() # next 2 lines null out modules vars for pickling stats._request = None stats._config = None pickle.dump(stats, f) tools.unlock(f) f.close()