Adding support for executing python scripts on remote runtime
authorlaurent
Sun, 11 Mar 2012 19:42:14 +0100
changeset 699 6ff64cadb1ff
parent 697 3e83853081d4
child 700 b0a7abd50b09
Adding support for executing python scripts on remote runtime
connectors/PYRO/__init__.py
plugger.py
runtime/PLCObject.py
--- a/connectors/PYRO/__init__.py	Mon Mar 05 12:02:20 2012 +0100
+++ b/connectors/PYRO/__init__.py	Sun Mar 11 19:42:14 2012 +0100
@@ -130,6 +130,10 @@
             return RemotePLCObjectProxy.GetPLCstatus()
         GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, "Broken")
 
+        def _PyroRemoteExec(self, script, **kwargs):
+            return RemotePLCObjectProxy.RemoteExec(script, **kwargs)
+        RemoteExec = PyroCatcher(_PyroRemoteExec, (-1, "RemoteExec script failed!"))
+
         def __getattr__(self, attrName):
             member = self.__dict__.get(attrName, None)
             if member is None:
--- a/plugger.py	Mon Mar 05 12:02:20 2012 +0100
+++ b/plugger.py	Sun Mar 11 19:42:14 2012 +0100
@@ -169,7 +169,10 @@
                 return True
 
         return False
-        
+    
+    def RemoteExec(self, script, **kwargs):
+        return self.PlugParent.RemoteExec(script, **kwargs)
+    
     def OnPlugSave(self):
         #Default, do nothing and return success
         return True
@@ -1776,6 +1779,11 @@
     def GetTicktime(self):
         return self._Ticktime
 
+    def RemoteExec(self, script, **kwargs):
+        if self._connector is None:
+            return -1, "No runtime connected"
+        return self._connector.RemoteExec(script, **kwargs)
+
     def DebugThreadProc(self):
         """
         This thread waid PLC debug data, and dispatch them to subscribers
@@ -1815,12 +1823,11 @@
         self.DebugThread = None
 
     def KillDebugThread(self):
-        tmp_debugthread = self.DebugThread
         self.debug_break = True
-        if tmp_debugthread is not None:
+        if self.DebugThread is not None:
             self.logger.writeyield(_("Stopping debugger...\n"))
-            tmp_debugthread.join(timeout=5)
-            if tmp_debugthread.isAlive() and self.logger:
+            self.DebugThread.join(timeout=5)
+            if self.DebugThread.isAlive() and self.logger:
                 self.logger.write_warning(_("Couldn't stop debugger.\n"))
             else:
                 self.logger.write(_("Debugger stopped.\n"))
--- a/runtime/PLCObject.py	Mon Mar 05 12:02:20 2012 +0100
+++ b/runtime/PLCObject.py	Sun Mar 11 19:42:14 2012 +0100
@@ -34,6 +34,10 @@
     from _ctypes import dlopen, dlclose
 
 import traceback
+def get_last_traceback(tb):
+    while tb.tb_next:
+        tb = tb.tb_next
+    return tb
 
 lib_ext ={
      "linux2":".so",
@@ -389,3 +393,14 @@
                 #PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value))
         return self.PLCStatus, None, []
 
+    def RemoteExec(self, script, **kwargs):
+        try:
+            exec script in kwargs
+        except:
+            e_type, e_value, e_traceback = sys.exc_info()
+            line_no = traceback.tb_lineno(get_last_traceback(e_traceback))
+            return (-1, "RemoteExec script failed!\n\nLine %d: %s\n\t%s" % 
+                        (line_no, e_value, script.splitlines()[line_no - 1]))
+        return (0, kwargs.get("returnVal", None))
+    
+