X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=monitor%2Fdatabase%2Fdbpickle.py;h=f7db4803a677b53eda084ae61fc1415f0f474c33;hb=0a896f10303078562b737c83fd08dc25f0973c58;hp=6f3a87dcb7fff63e23fd794d15b62772199a6c3d;hpb=19414270cf2c8429daab02fdebbd8081d9ba0db0;p=monitor.git diff --git a/monitor/database/dbpickle.py b/monitor/database/dbpickle.py index 6f3a87d..f7db480 100644 --- a/monitor/database/dbpickle.py +++ b/monitor/database/dbpickle.py @@ -3,6 +3,7 @@ import sys import pickle import inspect import shutil +import time from monitor import config noserial=False @@ -16,16 +17,50 @@ except: DEBUG= 0 PICKLE_PATH=config.MONITOR_DATA_ROOT +def lastModified(name, type=None): + # TODO: fix for 'debug' mode also. + t = SPickle().mtime("production.%s" % name, type) + return t + +def escapeName(name): + """ + automatically escape names passed to the db to make sensible-filenames without + exposing this to users. + """ + return name.replace("/", "_") + +def cachedRecently(name, length=int(config.cachetime), type=None): + """ + return true or false based on whether the modified time of the cached + file is within 'length' minutes. + """ + name = name.replace("/", "_") + if hasattr(config, 'cachecalls') and not config.cachecalls: + # don't use cached calls if cachecalls is false + return False + + try: + t = lastModified(name, type) + except: + # file doesn't exist or we can't access it. + return False + + current_time = time.time() + if current_time > t + length*60: + return False + else: + return True def dbLoad(name, type=None): + name = escapeName(name) return SPickle().load(name, type) def dbExists(name, type=None): - #if self.config.debug: - # name = "debug.%s" % name + name = escapeName(name) return SPickle().exists(name, type) def dbDump(name, obj=None, type=None): + name = escapeName(name) # depth of the dump is 2 now, since we're redirecting to '.dump' return SPickle().dump(name, obj, type, 2) @@ -73,6 +108,10 @@ class SPickle: raise Exception("No PHPSerializer module available") return "%s/%s.phpserial" % (self.path, name) + + def mtime(self, name, type=None): + f = os.stat(self.__file(name, type)) + return f[-2] def exists(self, name, type=None): return os.path.exists(self.__file(name, type)) @@ -89,7 +128,7 @@ class SPickle: Otherwise, it's normal mode, if the file doesn't exist, raise error Load the file """ - print "loading %s" % name + #print "loading %s" % name if config.debug: if self.exists("debug.%s" % name, type):