7 from monitor import config
11 from util.PHPSerialize import *
12 from util.PHPUnserialize import *
14 #print >>sys.stderr, "PHPSerial db type not allowed."
18 PICKLE_PATH=config.MONITOR_DATA_ROOT
20 def lastModified(name, type=None):
21 # TODO: fix for 'debug' mode also.
22 t = SPickle().mtime("production.%s" % name, type)
27 automatically escape names passed to the db to make sensible-filenames without
28 exposing this to users.
30 return name.replace("/", "_")
32 def cachedRecently(name, length=int(config.cachetime), type=None):
34 return true or false based on whether the modified time of the cached
35 file is within 'length' minutes.
37 name = name.replace("/", "_")
38 if hasattr(config, 'cachecalls') and not config.cachecalls:
39 # don't use cached calls if cachecalls is false
43 t = lastModified(name, type)
45 # file doesn't exist or we can't access it.
48 current_time = time.time()
49 if current_time > t + length*60:
54 def dbLoad(name, type=None):
55 name = escapeName(name)
56 return SPickle().load(name, type)
58 def dbExists(name, type=None):
59 name = escapeName(name)
60 return SPickle().exists(name, type)
62 def dbDump(name, obj=None, type=None):
63 name = escapeName(name)
64 # depth of the dump is 2 now, since we're redirecting to '.dump'
65 return SPickle().dump(name, obj, type, 2)
67 def if_cached_else_refresh(cond, refresh, name, function, type=None):
70 if not config.debug and s.exists("production.%s" % name, type):
71 s.remove("production.%s" % name, type)
72 if config.debug and s.exists("debug.%s" % name, type):
73 s.remove("debug.%s" % name, type)
75 return if_cached_else(cond, name, function, type)
77 def if_cached_else(cond, name, function, type=None):
79 if (cond and s.exists("production.%s" % name, type)) or \
80 (cond and config.debug and s.exists("debug.%s" % name, type)):
81 o = s.load(name, type)
85 s.dump(name, o, type) # cache the object using 'name'
86 o = s.load(name, type)
87 # TODO: what if 'o' hasn't been converted...
91 def __init__(self, path=PICKLE_PATH):
94 def if_cached_else(self, cond, name, function, type=None):
95 if cond and self.exists("production.%s" % name, type):
96 o = self.load(name, type)
100 self.dump(name, o, type) # cache the object using 'name'
103 def __file(self, name, type=None):
105 return "%s/%s.pkl" % (self.path, name)
108 raise Exception("No PHPSerializer module available")
110 return "%s/%s.phpserial" % (self.path, name)
112 def mtime(self, name, type=None):
113 f = os.stat(self.__file(name, type))
116 def exists(self, name, type=None):
117 return os.path.exists(self.__file(name, type))
119 def remove(self, name, type=None):
120 return os.remove(self.__file(name, type))
122 def load(self, name, type=None):
124 In debug mode, we should fail if neither file exists.
125 if the debug file exists, reset name
126 elif the original file exists, make a copy, reset name
127 else neither exist, raise an error
128 Otherwise, it's normal mode, if the file doesn't exist, raise error
131 #print "loading %s" % name
134 if self.exists("debug.%s" % name, type):
135 name = "debug.%s" % name
136 elif self.exists("production.%s" % name, type):
137 debugname = "debug.%s" % name
138 if not self.exists(debugname, type):
139 name = "production.%s" % name
140 shutil.copyfile(self.__file(name, type), self.__file(debugname, type))
142 else: # neither exist
143 raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
145 if self.exists("production.%s" % name, type):
146 name = "production.%s" % name
147 elif self.exists(name, type):
150 raise Exception, "No such file %s" % name
154 #print traceback.print_stack()
155 #print "loading %s" % self.__file(name, type)
156 #sys.stderr.write("-----------------------------\n")
157 f = open(self.__file(name, type), 'r')
162 raise Exception("No PHPSerializer module available")
164 o = s.unserialize(f.read())
169 # use the environment to extract the data associated with the local
171 def dump(self, name, obj=None, type=None, depth=1):
173 o = inspect.getouterframes(inspect.currentframe())
174 up1 = o[depth][0] # get the frame one prior to (up from) this frame
175 argvals = inspect.getargvalues(up1)
176 # TODO: check that 'name' is a local variable; otherwise this would fail.
177 obj = argvals[3][name] # extract the local variable name 'name'
178 if not os.path.isdir("%s/" % self.path):
179 os.mkdir("%s" % self.path)
181 name = "debug.%s" % name
183 name = "production.%s" % name
184 f = open(self.__file(name, type), 'w')
189 raise Exception("No PHPSerializer module available")
191 f.write(s.serialize(obj))