Tool to find stray node network entries in the PLC db. There were currently
[monitor.git] / soltesz.py
index 83100c4..d89eed0 100644 (file)
@@ -6,17 +6,19 @@ try:
        from PHPSerialize import *
        from PHPUnserialize import *
 except:
-       print >>sys.stderr, "PHPSerial db type not allowed."
+       #print >>sys.stderr, "PHPSerial db type not allowed."
        noserial=True
 
 import inspect
 import shutil
-from config import config
+from config2 import config
 config = config()
 
 DEBUG= 0
 PICKLE_PATH="pdb"
 
+class ExceptionTimeout(Exception): pass
+
 def dbLoad(name, type=None):
        return SPickle().load(name, type)
 
@@ -53,8 +55,8 @@ def if_cached_else(cond, name, function, type=None):
        return o
 
 class SPickle:
-       def __init__(self):
-               pass
+       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):
@@ -67,12 +69,12 @@ class SPickle:
 
        def __file(self, name, type=None):
                if type == None:
-                       return "%s/%s.pkl" % (PICKLE_PATH, name)
+                       return "%s/%s.pkl" % (self.path, name)
                else:
                        if noserial:
                                raise Exception("No PHPSerializer module available")
 
-                       return "%s/%s.phpserial" % (PICKLE_PATH, name)
+                       return "%s/%s.phpserial" % (self.path, name)
                
        def exists(self, name, type=None):
                return os.path.exists(self.__file(name, type))
@@ -102,11 +104,15 @@ class SPickle:
                        else:   # neither exist
                                raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
                else:
-                       if not self.exists("production.%s" % name, type):
+                       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
-                       name = "production.%s" % name
+                               
 
-               print "loading %s" % self.__file(name, type)
+               #print "loading %s" % self.__file(name, type)
                f = open(self.__file(name, type), 'r')
                if type == None:
                        o = pickle.load(f)
@@ -128,8 +134,8 @@ class SPickle:
                        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/" % PICKLE_PATH):
-                       os.mkdir("%s" % PICKLE_PATH)
+               if not os.path.isdir("%s/" % self.path):
+                       os.mkdir("%s" % self.path)
                if config.debug:
                        name = "debug.%s" % name
                else:
@@ -152,19 +158,70 @@ ssh_options = { 'StrictHostKeyChecking':'no',
                                'PasswordAuthentication':'no',
                                'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
 from select import select 
+import subprocess
+import signal
+
+class Sopen(subprocess.Popen):
+       def kill(self, signal = signal.SIGTERM):
+               os.kill(self.pid, signal)
+
 class CMD:
        def __init__(self):
                pass
 
-       def run_noexcept(self, cmd):
+       def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
 
-               (f_in, f_out, f_err) = os.popen3(cmd)
-               lout, lin, lerr = select([f_out,f_err], [], [], COMMAND_TIMEOUT*2)
-               if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
-                       # Reached a timeout!
-                       print "TODO: kill subprocess: '%s'" % cmd
-                       # TODO: kill subprocess??
+               try:
+                       return CMD.run(self,cmd,timeout)
+               except ExceptionTimeout:
+                       import traceback; print traceback.print_exc()
                        return ("", "SCRIPTTIMEOUT")
+                       
+
+#              s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
+#              #(f_in, f_out, f_err) = os.popen3(cmd)
+#              (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
+#              lout, lin, lerr = select([f_out,f_err], [], [], timeout)
+#              if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
+#                      # Reached a timeout!  Nuke process so it does not hang.
+#                      s.kill(signal.SIGKILL)
+#                      return ("", "SCRIPTTIMEOUT")
+#              o_value = f_out.read()
+#              e_value = ""
+#              if o_value == "":       # An error has occured
+#                      e_value = f_err.read()
+#
+#              o_value = o_value.strip()
+#              e_value = e_value.strip()
+#
+#              f_out.close()
+#              f_in.close()
+#              f_err.close()
+#              try:
+#                      s.kill()
+#              except OSError:
+#                      # no such process, due to it already exiting...
+#                      pass
+#
+#              return (o_value, e_value)
+       def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
+               (o,e) = self.run(cmd, timeout)
+               self.output = o
+               self.error = e
+               if self.s.returncode is None:
+                       self.s.wait()
+               return self.s.returncode
+
+       def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
+
+               s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
+               self.s = s
+               (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
+               lout, lin, lerr = select([f_out,f_err], [], [], timeout)
+               if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
+                       # Reached a timeout!  Nuke process so it does not hang.
+                       s.kill(signal.SIGKILL)
+                       raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
                o_value = f_out.read()
                e_value = ""
                if o_value == "":       # An error has occured
@@ -176,22 +233,14 @@ class CMD:
                f_out.close()
                f_in.close()
                f_err.close()
-               return (o_value, e_value)
-
-       def run(self, cmd):
-
-               (f_in, f_out, f_err) = os.popen3(cmd)
-               value = f_out.read()
-               if value == "":
-                       raise Exception, f_err.read()
-               value = value.strip()
+               try:
+                       s.kill()
+               except OSError:
+                       # no such process, due to it already exiting...
+                       pass
 
-               f_out.close()
-               f_in.close()
-               f_err.close()
-               return value
+               return (o_value, e_value)
 
-               
 
 class SSH(CMD):
        def __init__(self, user, host, options = ssh_options):
@@ -206,10 +255,10 @@ class SSH(CMD):
                        options = options + "-o %s=%s " % (o,v)
                return options
 
-       def run(self, cmd):
+       def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
                cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(), 
                                                                        self.user, self.host, cmd)
-               return CMD.run(self, cmd)
+               return CMD.run(self, cmd, timeout)
 
        def get_file(self, rmt_filename, local_filename=None):
                if local_filename == None: