runtime/PLCObject.py
author etisserant
Mon, 25 Aug 2008 18:01:49 +0200
changeset 220 ad3292145fc2
parent 219 43d65f0179e2
child 227 48c13b84505c
permissions -rwxr-xr-x
Fixed local PLC runtime kill on aborted quit attempt.
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     3
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     6
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
#General Public License for more details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    20
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    25
import Pyro.core as pyro
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    26
from threading import Timer
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
    27
import ctypes, os, commands
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    28
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    29
if os.name == ("nt", "ce"):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    30
    from _ctypes import LoadLibrary as dlopen
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    31
    from _ctypes import FreeLibrary as dlclose
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    32
elif os.name == "posix":
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    33
    from _ctypes import dlopen, dlclose
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    34
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    35
import os,sys,traceback
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
lib_ext ={
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
     "linux2":".so",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    39
     "win32":".dll",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    40
     }.get(sys.platform, "")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    41
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    42
class PLCObject(pyro.ObjBase):
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
    43
    def __init__(self, workingdir, daemon, argv):
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    44
        pyro.ObjBase.__init__(self)
219
43d65f0179e2 Fixed problem with first PLC argument (argv)
etisserant
parents: 216
diff changeset
    45
        self.argv = [workingdir] + argv # force argv[0] to be "path" to exec...
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    46
        self.workingdir = workingdir
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    47
        self.PLCStatus = "Stopped"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    48
        self.PLClibraryHandle = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    49
        # Creates fake C funcs proxies
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    50
        self._FreePLC()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    51
        self.daemon = daemon
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    52
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    53
        # Get the last transfered PLC if connector must be restart
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    54
        try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    55
            self.CurrentPLCFilename=open(
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    56
                             self._GetMD5FileName(),
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    57
                             "r").read().strip() + lib_ext
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    58
        except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    59
            self.PLCStatus = "Empty"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    60
            self.CurrentPLCFilename=None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    61
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    62
    def _GetMD5FileName(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    63
        return os.path.join(self.workingdir, "lasttransferedPLC.md5")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    64
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    65
    def _GetLibFileName(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    66
        return os.path.join(self.workingdir,self.CurrentPLCFilename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    67
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    68
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    69
    def _LoadNewPLC(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    71
        Load PLC library
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    72
        Declare all functions, arguments and return values
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    73
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    74
        print "Load PLC"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    75
        try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    76
            self._PLClibraryHandle = dlopen(self._GetLibFileName())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    77
            self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    78
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
            self._startPLC = self.PLClibraryHandle.startPLC
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
            self._startPLC.restype = ctypes.c_int
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
            self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
            
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
            self._stopPLC = self.PLClibraryHandle.stopPLC
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
            self._stopPLC.restype = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
            self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    87
            self._ResetDebugVariables.restype = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    88
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
            self._RegisterDebugVariable = self.PLClibraryHandle.ResetDebugVariables
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
            self._RegisterDebugVariable.restype = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
            self._IterDebugData = self.PLClibraryHandle.IterDebugData
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
            self._IterDebugData.restype = ctypes.c_void_p
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
            self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    96
            self._FreeDebugData = self.PLClibraryHandle.FreeDebugData
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    97
            self._FreeDebugData.restype = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    98
            return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    99
        except:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   100
            print traceback.format_exc()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   101
            return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   102
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   103
    def _FreePLC(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   104
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   105
        Unload PLC library.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   106
        This is also called by __init__ to create dummy C func proxies
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   107
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   108
        # Forget all refs to library
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   109
        self._startPLC = lambda:None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   110
        self._stopPLC = lambda:None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   111
        self._ResetDebugVariables = lambda:None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   112
        self._RegisterDebugVariable = lambda x:None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   113
        self._IterDebugData = lambda x,y:None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   114
        self._FreeDebugData = lambda:None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   115
        self.PLClibraryHandle = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   116
        # Unload library explicitely
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   117
        if getattr(self,"_PLClibraryHandle",None) is not None:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   118
            print "Unload PLC"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   119
            dlclose(self._PLClibraryHandle)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   120
            res = self._DetectDirtyLibs()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   121
        else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   122
            res = False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   123
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   124
        self._PLClibraryHandle = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   125
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   126
        return res
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   127
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   128
    def _DetectDirtyLibs(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   129
        # Detect dirty libs
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   130
        # Get lib dependencies (for dirty lib detection)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   131
        if os.name == "posix":
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   132
            # parasiting libs listed with ldd
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   133
            badlibs = [ toks.split()[0] for toks in commands.getoutput(
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   134
                            "ldd "+self._GetLibFileName()).splitlines() ]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   135
            for badlib in badlibs:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   136
                if badlib[:6] in ["libwx_",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   137
                                  "libwxs",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   138
                                  "libgtk",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   139
                                  "libgdk",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   140
                                  "libatk",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   141
                                  "libpan",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   142
                                  "libX11",
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   143
                                  ]:
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   144
                    #badhandle = dlopen(badlib, dl.RTLD_NOLOAD)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   145
                    print "Dirty lib detected :" + badlib
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   146
                    #dlclose(badhandle)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   147
                    return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   148
        return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   149
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   150
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   151
    def StartPLC(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   152
        print "StartPLC"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   153
        if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
219
43d65f0179e2 Fixed problem with first PLC argument (argv)
etisserant
parents: 216
diff changeset
   154
            c_argv = ctypes.c_char_p * len(self.argv)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   155
            if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   156
                self.PLCStatus = "Started"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   157
                return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   158
            else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   159
                print "_StartPLC did not return 0 !"
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   160
                self._DoStopPLC()
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   161
        return False
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   162
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   163
    def _DoStopPLC(self):
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   164
        self._stopPLC()
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   165
        self.PLCStatus = "Stopped"
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   166
        if self._FreePLC():
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   167
            self.PLCStatus = "Dirty"
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   168
        return True
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   169
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   170
    def StopPLC(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   171
        if self.PLCStatus == "Started":
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents: 203
diff changeset
   172
            self._DoStopPLC()
216
11124e129a28 Fixed typo in PLCObject.py
etisserant
parents: 209
diff changeset
   173
            return True
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   174
        return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   175
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   176
    def _Reload(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   177
        self.daemon.shutdown(True)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   178
        self.daemon.sock.close()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   179
        os.execv(sys.executable,[sys.executable]+sys.argv[:])
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   180
        # never reached
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   181
        return 0
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   182
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   183
    def ForceReload(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   184
        # respawn python interpreter
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   185
        Timer(0.1,self._Reload).start()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   186
        return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   187
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   188
    def GetPLCstatus(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   189
        return self.PLCStatus
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   190
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   191
    def NewPLC(self, md5sum, data, extrafiles):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   192
        print "NewPLC (%s)"%md5sum
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   193
        if self.PLCStatus in ["Stopped", "Empty", "Dirty"]:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   194
            NewFileName = md5sum + lib_ext
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   195
            extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   196
            try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   197
                os.remove(os.path.join(self.workingdir,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   198
                                       self.CurrentPLCFilename))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   199
                for filename in file(extra_files_log, "r").readlines() + extra_files_log:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   200
                    try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   201
                        os.remove(os.path.join(self.workingdir, filename))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   202
                    except:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   203
                        pass
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   204
            except:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   205
                pass
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   206
                        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   207
            try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   208
                # Create new PLC file
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   209
                open(os.path.join(self.workingdir,NewFileName),
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   210
                     'wb').write(data)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   211
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   212
                # Store new PLC filename based on md5 key
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   213
                open(self._GetMD5FileName(), "w").write(md5sum)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   214
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   215
                # Then write the files
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   216
                log = file(extra_files_log, "w")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   217
                for fname,fdata in extrafiles:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   218
                    fpath = os.path.join(self.workingdir,fname)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   219
                    open(fpath, "wb").write(fdata)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   220
                    log.write(fname+'\n')
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   221
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   222
                # Store new PLC filename
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   223
                self.CurrentPLCFilename = NewFileName
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   224
            except:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   225
                print traceback.format_exc()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   226
                return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   227
            if self.PLCStatus == "Empty":
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   228
                self.PLCStatus = "Stopped"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   229
            return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   230
        return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   231
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   232
    def MatchMD5(self, MD5):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   233
        try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   234
            last_md5 = open(self._GetMD5FileName(), "r").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   235
            return last_md5 == MD5
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   236
        except:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   237
            return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   238
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   239
    def SetTraceVariablesList(self, idxs):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   240
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   241
        Call ctype imported function to append 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   242
        these indexes to registred variables in PLC debugger
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   243
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   244
        # keep a copy of requested idx
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   245
        self._Idxs = idxs[:]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   246
        self._ResetDebugVariables()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   247
        for idx in idxs:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   248
            self._RegisterDebugVariable(idx)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   249
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   250
    def GetTraceVariables(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   251
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   252
        Return a list of variables, corresponding to the list of requiered idx
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   253
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   254
        self._WaitDebugData()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   255
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   256
        for idx in self._Idxs:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   257
            buffer=self._IterDebugData()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   258
        self._FreeDebugData()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   259
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   260
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   261
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   262