add files to repository.
authorStephen Soltesz <soltesz@cs.princeton.edu>
Fri, 15 Aug 2008 19:56:42 +0000 (19:56 +0000)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Fri, 15 Aug 2008 19:56:42 +0000 (19:56 +0000)
monitor/config.py [new file with mode: 0644]
monitor/database.py [new file with mode: 0644]

diff --git a/monitor/config.py b/monitor/config.py
new file mode 100644 (file)
index 0000000..b37e04a
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/python
+
+# load defaults from /etc/monitor.conf
+# home/.monitor.conf
+# $PWD/.monitor.conf
+import os
+import ConfigParser
+
+class Options(object):
+       def __init__(self):
+               cp = ConfigParser.ConfigParser()
+               cp.optionxform = str
+               # load defaults from global, home dir, then $PWD
+               cp.read(['/etc/monitor.conf', os.path.expanduser('~/.monitor.conf'), 
+                                          '.monitor.conf', 'monitor.conf'])
+               self.cp = cp
+               self.section = "default"
+       def __getattr__(self, name):
+               if name in self.cp.sections():
+                       self.section = name
+                       return self
+               else:
+                       return self.cp.get(self.section, name)
+
+
+import config
+imported = False
+
+def updatemodule(module, cf):
+       module.__dict__.update(cf.__dict__)
+
+def update_section(options, section, bool=False):
+       # Place all default commandline values at the top level of this module
+       for key in options.cp.options(section):
+               if bool:
+                       config.__dict__.update({key : options.cp.getboolean(section, key)})
+               else:
+                       config.__dict__.update({key : options.cp.get(section, key)})
+
+def update(parseoptions):
+       update_commandline()
+       # now update the top-level module with all other args passed in here.
+       for key in parseoptions.__dict__.keys():
+               config.__dict__.update({key: parseoptions.__dict__[key]})
+
+if not config.imported:
+       imported = True
+
+       #from config import options as config
+       options = Options()
+       update_section(options, 'commandline', True)
+       update_section(options, 'monitorconfig')
+
+#for i in dir(config):
+#      if "__" not  in i:
+#              print i, "==", config.__dict__[i]
+#print "======================================"
+
diff --git a/monitor/database.py b/monitor/database.py
new file mode 100644 (file)
index 0000000..3b5bd65
--- /dev/null
@@ -0,0 +1,154 @@
+import os
+import sys
+import pickle
+noserial=False
+try:
+       from PHPSerialize import *
+       from PHPUnserialize import *
+except:
+       #print >>sys.stderr, "PHPSerial db type not allowed."
+       noserial=True
+
+import inspect
+import shutil
+import config
+import config as monitorconfig
+
+DEBUG= 0
+PICKLE_PATH=monitorconfig.MONITOR_DATA_ROOT
+
+
+def dbLoad(name, type=None):
+       return SPickle().load(name, type)
+
+def dbExists(name, type=None):
+       #if self.config.debug:
+       #       name = "debug.%s" % name
+       return SPickle().exists(name, type)
+
+def dbDump(name, obj=None, type=None):
+       # depth of the dump is 2 now, since we're redirecting to '.dump'
+       return SPickle().dump(name, obj, type, 2)
+
+def if_cached_else_refresh(cond, refresh, name, function, type=None):
+       s = SPickle()
+       if refresh:
+               if not config.debug and s.exists("production.%s" % name, type):
+                       s.remove("production.%s" % name, type)
+               if config.debug and s.exists("debug.%s" % name, type):
+                       s.remove("debug.%s" % name, type)
+
+       return if_cached_else(cond, name, function, type)
+
+def if_cached_else(cond, name, function, type=None):
+       s = SPickle()
+       if (cond and s.exists("production.%s" % name, type)) or \
+          (cond and config.debug and s.exists("debug.%s" % name, type)):
+               o = s.load(name, type)
+       else:
+               o = function()
+               if cond:
+                       s.dump(name, o, type)   # cache the object using 'name'
+                       o = s.load(name, type)
+               # TODO: what if 'o' hasn't been converted...
+       return o
+
+class SPickle:
+       def __init__(self, path=PICKLE_PATH):
+               self.path = path
+
+       def if_cached_else(self, cond, name, function, type=None):
+               if cond and self.exists("production.%s" % name, type):
+                       o = self.load(name, type)
+               else:
+                       o = function()
+                       if cond:
+                               self.dump(name, o, type)        # cache the object using 'name'
+               return o
+
+       def __file(self, name, type=None):
+               if type == None:
+                       return "%s/%s.pkl" % (self.path, name)
+               else:
+                       if noserial:
+                               raise Exception("No PHPSerializer module available")
+
+                       return "%s/%s.phpserial" % (self.path, name)
+               
+       def exists(self, name, type=None):
+               return os.path.exists(self.__file(name, type))
+
+       def remove(self, name, type=None):
+               return os.remove(self.__file(name, type))
+
+       def load(self, name, type=None):
+               """ 
+               In debug mode, we should fail if neither file exists.
+                       if the debug file exists, reset name
+                       elif the original file exists, make a copy, reset name
+                       else neither exist, raise an error
+               Otherwise, it's normal mode, if the file doesn't exist, raise error
+               Load the file
+               """
+
+               if config.debug:
+                       if self.exists("debug.%s" % name, type):
+                               name = "debug.%s" % name
+                       elif self.exists("production.%s" % name, type):
+                               debugname = "debug.%s" % name
+                               if not self.exists(debugname, type):
+                                       name = "production.%s" % name
+                                       shutil.copyfile(self.__file(name, type), self.__file(debugname, type))
+                               name = debugname
+                       else:   # neither exist
+                               raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
+               else:
+                       if   self.exists("production.%s" % name, type):
+                               name = "production.%s" % name
+                       elif self.exists(name, type):
+                               name = name
+                       else:
+                               raise Exception, "No such file %s" % name
+                               
+
+               #import traceback
+               #print traceback.print_stack()
+               #print "loading %s" % self.__file(name, type)
+               #sys.stderr.write("-----------------------------\n")
+               f = open(self.__file(name, type), 'r')
+               if type == None:
+                       o = pickle.load(f)
+               else:
+                       if noserial:
+                               raise Exception("No PHPSerializer module available")
+                       s = PHPUnserialize()
+                       o = s.unserialize(f.read())
+               f.close()
+               return o
+                       
+       
+       # use the environment to extract the data associated with the local
+       # variable 'name'
+       def dump(self, name, obj=None, type=None, depth=1):
+               if obj == None:
+                       o = inspect.getouterframes(inspect.currentframe())
+                       up1 = o[depth][0] # get the frame one prior to (up from) this frame
+                       argvals = inspect.getargvalues(up1)
+                       # TODO: check that 'name' is a local variable; otherwise this would fail.
+                       obj = argvals[3][name] # extract the local variable name 'name'
+               if not os.path.isdir("%s/" % self.path):
+                       os.mkdir("%s" % self.path)
+               if config.debug:
+                       name = "debug.%s" % name
+               else:
+                       name = "production.%s" % name
+               f = open(self.__file(name, type), 'w')
+               if type == None:
+                       pickle.dump(obj, f)
+               else:
+                       if noserial:
+                               raise Exception("No PHPSerializer module available")
+                       s = PHPSerialize()
+                       f.write(s.serialize(obj))
+               f.close()
+               return