def process(self, _edObject=None):
     EDPluginExec.process(self)
     self.DEBUG("EDPluginExecPlotGlev1_1.process")
     for dictPlot in self.listPlot:
         if os.path.dirname(dictPlot["script"]) != self.getWorkingDirectory():
             shutil.copy(dictPlot["script"], self.getWorkingDirectory())
         listDataFiles = dictPlot["data"]
         for strDataFileFullPath in listDataFiles:
             strDataFile = os.path.basename(strDataFileFullPath)
             if not os.path.exists(os.path.join(self.getWorkingDirectory(), strDataFile)):
                 shutil.copy(strDataFileFullPath, self.getWorkingDirectory())
         if EDUtilsPath.isESRF():
             # Force PXSOFT version of gle
             strCommand = "/opt/pxsoft/bin/gle -verbosity 0 -r 150 -d jpg %s" % os.path.basename(dictPlot["script"])
         else:
             strCommand = "gle -verbosity 0 -r 150 -d jpg %s" % os.path.basename(dictPlot["script"])
         # Copied from EDPluginExecProcess
         self.DEBUG(self.getBaseName() + ": Processing")
         timer = threading.Timer(float(self.getTimeOut()), self.kill)
         timer.start()
         try:
             self.__subprocess = EDUtilsPlatform.Popen(shlex.split(str(EDUtilsPlatform.escape(strCommand))),
                                                cwd=self.getWorkingDirectory())
             self.__iPID = self.__subprocess.pid
             self.__strExecutionStatus = str(self.__subprocess.wait())
         except OSError as e:
             strErrorMessage = self.getPluginName() + " : required program gle not installed"
             self.error(strErrorMessage)
             self.setFailure()
         finally:
             timer.cancel()
 def preProcess(self):
     EDTestCasePluginExecuteDistlSignalStrengthThinClientv1_1.preProcess(
         self)
     # Load the configuration file
     xsPluginItem = self.getPluginConfiguration()
     if xsPluginItem is not None:
         strServerPort = EDConfiguration.getStringParamValue(xsPluginItem, \
             EDPluginDistlSignalStrengthThinClientv1_1.CONF_DISTL_SIGNAL_STRENGTH_SERVER_PORT)
         if strServerPort is None:
             iServerPort = EDPluginDistlSignalStrengthThinClientv1_1.DEFAULT_SERVER_PORT
         else:
             iServerPort = int(strServerPort)
         strPathToServer = EDConfiguration.getStringParamValue(xsPluginItem, \
             EDPluginDistlSignalStrengthThinClientv1_1.CONF_PATH_TO_DISTL_SIGNAL_STRENGTH_SERVER)
         if (strPathToServer == None):
             strErrorMessage = "EDPluginLabelitv1_1.configure : Configuration parameter missing: " + \
                                 "distl.mp_spotfinder_server_read_file"
             self.error(strErrorMessage)
             self.addErrorMessage(strErrorMessage)
             self.setFailure()
         # Start the server using random port
         self.subprocess = EDUtilsPlatform.Popen(shlex.split(
             str(
                 EDUtilsPlatform.escape(strPathToServer +
                                        " distl.port=%d" % iServerPort))),
                                                 cwd=os.getcwd())
         self.iPID = self.subprocess.pid
         # Give the server some time to start up
         time.sleep(8)
Beispiel #3
0
 def kill(self):
     self.WARNING("I will kill subprocess %s pid= %s" % (self.__subprocess, self.__iPID))
     EDUtilsPlatform.kill(self.__iPID)
     self.DEBUG("EDPluginExecProcess.process ========================================= ERROR! ================")
     errorMessage = EDMessage.ERROR_EXECUTION_03 % ('EDPluginExecProcess.process', self.getClassName(), "Timeout ")
     self.error(errorMessage)
     self.addErrorMessage(errorMessage)
     raise RuntimeError, errorMessage
 def kill(self):
     self.WARNING("I will kill subprocess %s pid= %s" % (self.__subprocess, self.__iPID))
     EDUtilsPlatform.kill(self.__iPID)
     self.DEBUG("EDPluginExecProcess.process ========================================= ERROR! ================")
     errorMessage = EDMessage.ERROR_EXECUTION_03 % ("EDPluginExecProcess.process", self.getClassName(), "Timeout ")
     self.error(errorMessage)
     self.addErrorMessage(errorMessage)
     raise RuntimeError, errorMessage
 def unitTestArchSize(self):
     """
     Test architecture and size
     """
     EDVerbose.DEBUG("EDTestCaseEDUtilsPlatform.unitTestArchSize")
     if EDUtilsPlatform.size == 64:
         EDAssert.equal(distutils.util.get_platform(), EDUtilsPlatform.getPythonPlatform(), "distutil.util.get_platform")
     else:
         EDAssert.equal(distutils.util.get_platform(), EDUtilsPlatform.getSystemPlatform(), "distutil.util.get_platform")
Beispiel #6
0
 def kill(self):
     EDVerbose.WARNING("I will kill subprocess %s pid= %s" % (self.__subprocess, self.__iPID))
     EDUtilsPlatform.kill(self.__iPID)
     self.synchronizeOff()
     self.__strExecutionStatus = "timeout"
     EDVerbose.DEBUG("EDPluginExecProcess.process ========================================= ERROR! ================")
     errorMessage = EDMessage.ERROR_EXECUTION_03 % ('EDPluginExecProcess.process', self.getClassName(), "Timeout ")
     EDVerbose.error(errorMessage)
     self.addErrorMessage(errorMessage)
     raise RuntimeError, errorMessage
 def unitTestArchSize(self):
     """
     Test architecture and size
     """
     EDVerbose.DEBUG("EDTestCaseEDUtilsPlatform.unitTestArchSize")
     if EDUtilsPlatform.size == 64:
         EDAssert.equal(distutils.util.get_platform(),
                        EDUtilsPlatform.getPythonPlatform(),
                        "distutil.util.get_platform")
     else:
         EDAssert.equal(distutils.util.get_platform(),
                        EDUtilsPlatform.getSystemPlatform(),
                        "distutil.util.get_platform")
Beispiel #8
0
 def process(self, _edObject = None):
     EDPluginExec.process(self)
     self.DEBUG("EDPluginExecPlotGlev1_0.process")
     for strPath in self.listPlot:
         strCommand = "gle -verbosity 0 -r 150 -d jpg %s.gle" % strPath
         # Copied from EDPluginExecProcess
         self.DEBUG(self.getBaseName() + ": Processing")
         timer = threading.Timer(float(self.getTimeOut()), self.kill)
         timer.start()
         self.__subprocess = EDUtilsPlatform.Popen(shlex.split(str(EDUtilsPlatform.escape(strCommand))),
                                                cwd=self.getWorkingDirectory())
         self.__iPID = self.__subprocess.pid
         self.__strExecutionStatus = str(self.__subprocess.wait())
         timer.cancel()            
Beispiel #9
0
 def kill(self):
     EDVerbose.WARNING("I will kill subprocess %s pid= %s" %
                       (self.__subprocess, self.__iPID))
     EDUtilsPlatform.kill(self.__iPID)
     self.synchronizeOff()
     self.__strExecutionStatus = "timeout"
     EDVerbose.DEBUG(
         "EDPluginExecProcess.process ========================================= ERROR! ================"
     )
     errorMessage = EDMessage.ERROR_EXECUTION_03 % (
         'EDPluginExecProcess.process', self.getClassName(), "Timeout ")
     EDVerbose.error(errorMessage)
     self.addErrorMessage(errorMessage)
     raise RuntimeError, errorMessage
Beispiel #10
0
 def process_on_oar(self, _edObject=None):
     """
     Special processing method for OAR. Since oarsub returns
     immediately we cannot rely on Popen.wait(). Instead we'll poll
     the cluster using the oarstat utility. In order to do that
     we'll setup a timer firing at regular intervals until we're
     done or the total time exceeds the timeout config parameter.
     """
     if self._oar_options is not None:
         command = '{0} {1} {2}'.format(self.getExecutable(),
                                    self._oar_options,
                                    self.getCommandline())
     else:
         command = '{0} {1}'.format(self.getExecutable(), self.getCommandline())
     EDVerbose.DEBUG('EDPluginExecProcess.process_on_oar executing: "{0}"'.format(command))
     self._start_time = time.time()
     self._timer = threading.Timer(float(self._oar_poll_interval), self.poll_oar)
     oarsub = subprocess.Popen(shlex.split(EDUtilsPlatform.escape(command)),
                               cwd=self.getWorkingDirectory(),
                               stdout=subprocess.PIPE)
     oar_job_id = None
     for line in oarsub.stdout:
         if line.startswith('OARJOB_ID='):
             oar_job_id = int(line.split()[1])
     if oar_job_id is not None:
         EDVerbose.DEBUG('EDPluginExecProcess.process_on_oar: job id is "{0}"'.format(oar_job_id))
         self._oar_job_id = oar_job_id
     else:
         msg = 'EDPluginExecProcess.process_on_oar: could not get OAR_JOB_ID!'
         EDVerbose.ERROR(msg)
         self.addErrorMessage(msg)
         raise RuntimeError(msg)
 def preProcess(self):
     EDTestCasePluginExecuteDistlSignalStrengthThinClientv1_1.preProcess(self)
     # Load the configuration file
     xsPluginItem = self.getPluginConfiguration()
     if xsPluginItem is not None:
         strServerPort = EDConfiguration.getStringParamValue(xsPluginItem, \
             EDPluginDistlSignalStrengthThinClientv1_1.CONF_DISTL_SIGNAL_STRENGTH_SERVER_PORT)
         if strServerPort is None:
             iServerPort = EDPluginDistlSignalStrengthThinClientv1_1.DEFAULT_SERVER_PORT
         else:
             iServerPort = int(strServerPort)
         strPathToServer = EDConfiguration.getStringParamValue(xsPluginItem, \
             EDPluginDistlSignalStrengthThinClientv1_1.CONF_PATH_TO_DISTL_SIGNAL_STRENGTH_SERVER)
         if(strPathToServer == None):
             strErrorMessage = "EDPluginLabelitv1_1.configure : Configuration parameter missing: " + \
                                 "distl.mp_spotfinder_server_read_file"
             self.error(strErrorMessage)
             self.addErrorMessage(strErrorMessage)
             self.setFailure()
         # Start the server using random port
         self.subprocess = EDUtilsPlatform.Popen(shlex.split(str(EDUtilsPlatform.escape(strPathToServer + " distl.port=%d" % iServerPort))),
                                                    cwd=os.getcwd())
         self.iPID = self.subprocess.pid
         # Give the server some time to start up
         time.sleep(8)
Beispiel #12
0
 def process_locally(self, _edObject=None):
     """
     Sets the process up with the executable, command line and time out
     Launches the process, in case of error, an error message is added to the list, and the plugins fails
     """
     strCommand = self.getExecutable() + " " + self.getCommandline()
     EDVerbose.DEBUG("EDPluginExecProcess.process executing: " +
                     self.getExecutable())
     self.synchronizeOn()
     EDVerbose.screen(self.getBaseName() + ": Processing")
     timer = threading.Timer(float(self.getTimeOut()), self.kill)
     timer.start()
     self.__subprocess = EDUtilsPlatform.Popen(
         shlex.split(str(EDUtilsPlatform.escape(strCommand))),
         cwd=self.getWorkingDirectory())
     self.__iPID = self.__subprocess.pid
     self.__strExecutionStatus = str(self.__subprocess.wait())
     timer.cancel()
     EDVerbose.DEBUG("EDPluginExecProcess.process finished ")
     self.synchronizeOff()
 def process(self, _edObject = None):
     EDPluginExec.process(self)
     self.DEBUG("EDPluginExecPlotGlev1_0.process")
     for strPath in self.listPlot:
         strCommand = "gle -verbosity 0 -r 150 -d jpg %s.gle" % strPath
         # Copied from EDPluginExecProcess
         self.DEBUG(self.getBaseName() + ": Processing")
         timer = threading.Timer(float(self.getTimeOut()), self.kill)
         timer.start()
         self.__subprocess = EDUtilsPlatform.Popen(shlex.split(str(EDUtilsPlatform.escape(strCommand))),
                                                cwd=self.getWorkingDirectory())
         self.__iPID = self.__subprocess.pid
         self.__strExecutionStatus = str(self.__subprocess.wait())
         timer.cancel()            
Beispiel #14
0
 def process_locally(self, _edObject=None):
     """
     Sets the process up with the executable, command line and time out
     Launches the process, in case of error, an error message is added to the list, and the plugins fails
     """
     strCommand = self.getExecutable() + " " + self.getCommandline()
     EDVerbose.DEBUG("EDPluginExecProcess.process executing: " + self.getExecutable())
     self.synchronizeOn()
     EDVerbose.screen(self.getBaseName() + ": Processing")
     timer = threading.Timer(float(self.getTimeOut()), self.kill)
     timer.start()
     self.__subprocess = EDUtilsPlatform.Popen(shlex.split(str(EDUtilsPlatform.escape(strCommand))),
                                                cwd=self.getWorkingDirectory())
     self.__iPID = self.__subprocess.pid
     self.__strExecutionStatus = str(self.__subprocess.wait())
     timer.cancel()
     EDVerbose.DEBUG("EDPluginExecProcess.process finished ")
     self.synchronizeOff()
 def process(self, _edObject = None):
     EDPluginExec.process(self)
     self.DEBUG("EDPluginExecPlotGlev1_0.process")
     for dictPlot in self.listPlot:
         if os.path.dirname(dictPlot["script"]) != self.getWorkingDirectory():
             shutil.copy(dictPlot["script"], self.getWorkingDirectory())
         listDataFiles = dictPlot["data"]
         for strDataFileFullPath in listDataFiles:
             strDataFile = os.path.basename(strDataFileFullPath)
             if not os.path.exists(os.path.join(self.getWorkingDirectory(),strDataFile)):
                 shutil.copy(strDataFileFullPath, self.getWorkingDirectory())
         strCommand = "gle -verbosity 0 -r 150 -d jpg %s" % os.path.basename(dictPlot["script"])
         # Copied from EDPluginExecProcess
         self.DEBUG(self.getBaseName() + ": Processing")
         timer = threading.Timer(float(self.getTimeOut()), self.kill)
         timer.start()
         self.__subprocess = EDUtilsPlatform.Popen(shlex.split(str(EDUtilsPlatform.escape(strCommand))),
                                                cwd=self.getWorkingDirectory())
         self.__iPID = self.__subprocess.pid
         self.__strExecutionStatus = str(self.__subprocess.wait())
         timer.cancel()            
Beispiel #16
0
 def process_on_oar(self, _edObject=None):
     """
     Special processing method for OAR. Since oarsub returns
     immediately we cannot rely on Popen.wait(). Instead we'll poll
     the cluster using the oarstat utility. In order to do that
     we'll setup a timer firing at regular intervals until we're
     done or the total time exceeds the timeout config parameter.
     """
     if self._oar_options is not None:
         command = '{0} {1} {2}'.format(self.getExecutable(),
                                        self._oar_options,
                                        self.getCommandline())
     else:
         command = '{0} {1}'.format(self.getExecutable(),
                                    self.getCommandline())
     self.DEBUG(
         'EDPluginExecProcess.process_on_oar executing: "{0}"'.format(
             command))
     self._start_time = time.time()
     self._timer = threading.Timer(float(self._oar_poll_interval),
                                   self.poll_oar)
     oarsub = subprocess.Popen(shlex.split(EDUtilsPlatform.escape(command)),
                               cwd=self.getWorkingDirectory(),
                               stdout=subprocess.PIPE)
     oar_job_id = None
     for line in oarsub.stdout:
         if line.startswith('OARJOB_ID='):
             oar_job_id = int(line.split()[1])
     if oar_job_id is not None:
         self.DEBUG(
             'EDPluginExecProcess.process_on_oar: job id is "{0}"'.format(
                 oar_job_id))
         self._oar_job_id = oar_job_id
     else:
         msg = 'EDPluginExecProcess.process_on_oar: could not get OAR_JOB_ID!'
         self.ERROR(msg)
         self.addErrorMessage(msg)
         raise RuntimeError(msg)
    def preparePythonScript(self):
        """
        Returns a string containing the Python script.
        """
        self.DEBUG("EDPluginExecProcessScript.preparePythonScript")

        #when running python script allow 1s+1% gracetime to let the python scripting mechanism to finish
        iScriptTimeOut = max(1, self.getTimeOut())
        self.setTimeOut(1 + 1.05 * iScriptTimeOut)
        self.DEBUG("Original Timeout is %s setting to %s" % (iScriptTimeOut, self.getTimeOut()))

        listScript = ["#!%s" % sys.executable, ""]

        listScript += ["import os, sys, subprocess, threading, socket, signal", "",
                      "os.chdir('%s')" % EDUtilsPlatform.escape(self.getWorkingDirectory()), "",
                      "def writeStdOutErr(std,filename):",
                      "        open(filename,'wb').write(std.read())",
                      "",
                      "def kill(pid):",
                      "    sys.stderr.write('TIMEOUT of %s pid: '+str(pid)+os.linesep)" %
                            EDUtilsPlatform.escape(self.__strScriptExecutable),
                      "    open('%s','a').write('TIMEOUT after %ss %s')" %
                            (EDUtilsPlatform.escape(self.__strPathToHostNamePidFile),
                            iScriptTimeOut, EDUtilsPlatform.escapedLinesep),
                      ]
        if EDUtilsPlatform.name == "nt":
            listScript += ["    os.kill(pid,signal.SIGTERM)",
                         "    sys.exit(-signal.SIGTERM)", ""]
        else:
            listScript += ["    os.kill(pid,signal.SIGKILL)",
                         "    sys.exit(-signal.SIGKILL)", ""]

        # Add pre-execution commands - if any
        cmdline = ""
        if self.__bRequireCCP4:
            if self.__strConfigSetupCCP4 == None or self.__strConfigSetupCCP4 == "":
                self.DEBUG("EDPluginExecProcessScript.preparePythonScript : CCP4 setup script not defined.")
            else:
                cmdline += ". %s %s" % (self.__strConfigSetupCCP4, EDUtilsPlatform.cmdSep)

        for strCommandPreExecution in self.__listCommandPreExecution:
            if strCommandPreExecution.strip().endswith("&"):
                cmdline += strCommandPreExecution
            else:
                cmdline += strCommandPreExecution + EDUtilsPlatform.cmdSep
        if len(cmdline) > 2:
            listScript += ["def getEnvironment(filename):",
                      "    dictEnv=None",
                      "    for oneFullLine in open(filename,'rb').readlines():",
                      "        oneLine=oneFullLine.strip()",
                      "        if ('=' in oneLine) and (isinstance(dictEnv,dict)):",
                      "            key,value = oneLine.split('=',1)",
                      "            dictEnv[key]=value",
                      "        elif oneLine == '%s':" % EDPluginExecProcessScript.START_OF_ENVIRONMENT,
                      "            dictEnv={}",
                      "    return dictEnv",
                      ""
                      ]

            cmdline += "echo %secho %s%s%s" % (EDUtilsPlatform.cmdSep,
                                                EDPluginExecProcessScript.START_OF_ENVIRONMENT,
                                                EDUtilsPlatform.cmdSep,
                                                EDUtilsPlatform.cmdEnv)
            listScript += ['subPre = subprocess.Popen("""%s""",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)' % cmdline,
                           'threadStdErr = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdErr", args=(subPre.stderr,"preExecute.err"))',
                           'threadStdOut = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdOut", args=(subPre.stdout,"preExecute.log"))',
                           'threadStdErr.start()',
                           'threadStdOut.start()',
                           'threadStdErr.join()',
                           'threadStdOut.join()',
                           'subPre.wait()',
                           'dictEnv=getEnvironment("preExecute.log")']
        else:
            listScript.append('dictEnv=os.environ')

#        # Execution
        listScript.append("")

        strcmd = "%s %s" % (
            EDUtilsPlatform.escape(self.__strScriptExecutable),
            EDUtilsPlatform.escape(self.__strScriptCommandline))

        listCommandLine = shlex.split(str(strcmd))
        if self.__listCommandExecution == []:
            listScript.append('subP = subprocess.Popen(%s,stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dictEnv)' % listCommandLine)
        else:
            listScript.append('subP = subprocess.Popen(%s, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dictEnv)' % listCommandLine)
            for strCommandExecution in self.__listCommandExecution:
                listScript.append('subP.stdin.write("%s%s")' % (strCommandExecution,
                                            EDUtilsPlatform.escapedLinesep))
            listScript.append('subP.stdin.close()')
        listScript += ['timer = threading.Timer(%s,kill,args=(subP.pid,))' % (iScriptTimeOut),
                       'timer.start()',
                       'open("%s","wb").write("%%s %%s %%s"%%(subP.pid,socket.gethostname(),os.linesep)) ' %
                                EDUtilsPlatform.escape(self.__strPathToHostNamePidFile),
                       'threadStdErr = threading.Thread(target=writeStdOutErr, name="WriteStdErr", args=(subP.stderr,"%s"))' %
                                EDUtilsPlatform.escape(self.__strScriptErrorLogFileName),
                       'threadStdOut = threading.Thread(target=writeStdOutErr, name="WriteStdOut", args=(subP.stdout,"%s"))' %
                                EDUtilsPlatform.escape(self.__strScriptLogFileName),
                       'threadStdErr.start()',
                       'threadStdOut.start()',
                       'threadStdErr.join()',
                       'threadStdOut.join()',
                       'subP.wait()',
                       'timer.cancel()'
                       '']
        # Add post-execution commands - if any
        cmdline = ""
        for strCommandPostExecution in self.__listCommandPostExecution:
            if strCommandPostExecution.strip().endswith(" & "):
                cmdline += strCommandPostExecution
            else:
                cmdline += strCommandPostExecution + EDUtilsPlatform.cmdSep

            listScript.append('subPost = subprocess.Popen("""%s""", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dictEnv)' % cmdline)
            listScript.append('threadStdErr = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdErr", args=(subPost.stderr,"postExecute.log"))')
            listScript.append('threadStdOut = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdOut", args=(subPost.stdout,"postExecute.err"))')
            listScript.append('threadStdErr.start()')
            listScript.append('threadStdOut.start()')
            listScript.append('threadStdErr.join()')
            listScript.append('threadStdOut.join()')
            listScript.append('subPost.wait()')


        listScript.append("sys.exit(subP.returncode)")
        listScript.append("")
        strPythonScript = os.linesep.join(listScript)
        return strPythonScript
 def testExecute(self):
     self.run()
     # Kill the server
     EDUtilsPlatform.kill(self.iPID)
Beispiel #19
0
    def preparePythonScript(self):
        """
        Returns a string containing the Python script.
        """
        self.DEBUG("EDPluginExecProcessScript.preparePythonScript")

        # when running python script allow 1s+1% gracetime to let the python scripting mechanism to finish
        iScriptTimeOut = max(1, self.getTimeOut())
        self.setTimeOut(1 + 1.05 * iScriptTimeOut)
        self.DEBUG("Original Timeout is %s setting to %s" %
                   (iScriptTimeOut, self.getTimeOut()))

        listScript = ["#!%s" % sys.executable, ""]

        listScript += [
            "import os, sys, subprocess, threading, socket, signal",
            "",
            "os.chdir('%s')" %
            EDUtilsPlatform.escape(self.getWorkingDirectory()),
            "",
            "def writeStdOutErr(std,filename):",
            "        open(filename,'wb').write(std.read())",
            "",
            "def kill(pid):",
            "    sys.stderr.write('TIMEOUT of %s pid: '+str(pid)+os.linesep)" %
            EDUtilsPlatform.escape(self.__strScriptExecutable),
            "    open('%s','a').write('TIMEOUT after %ss %s')" %
            (EDUtilsPlatform.escape(self.__strPathToHostNamePidFile),
             iScriptTimeOut, EDUtilsPlatform.escapedLinesep),
        ]
        if EDUtilsPlatform.name == "nt":
            listScript += [
                "    os.kill(pid,signal.SIGTERM)",
                "    sys.exit(-signal.SIGTERM)", ""
            ]
        else:
            listScript += [
                "    os.kill(pid,signal.SIGKILL)",
                "    sys.exit(-signal.SIGKILL)", ""
            ]

        # Add pre-execution commands - if any
        cmdline = ""
        if self.__bRequireCCP4:
            if self.__strConfigSetupCCP4 == None or self.__strConfigSetupCCP4 == "":
                self.DEBUG(
                    "EDPluginExecProcessScript.preparePythonScript : CCP4 setup script not defined."
                )
            else:
                cmdline += ". %s %s" % (self.__strConfigSetupCCP4,
                                        EDUtilsPlatform.cmdSep)

        for strCommandPreExecution in self.__listCommandPreExecution:
            if strCommandPreExecution.strip().endswith("&"):
                cmdline += strCommandPreExecution
            else:
                cmdline += strCommandPreExecution + EDUtilsPlatform.cmdSep
        if len(cmdline) > 2:
            listScript += [
                "def getEnvironment(filename):", "    dictEnv=None",
                "    for oneFullLine in open(filename,'rb').readlines():",
                "        oneLine=oneFullLine.strip()",
                "        if ('=' in oneLine) and (isinstance(dictEnv,dict)):",
                "            key,value = oneLine.split('=',1)",
                "            dictEnv[key]=value",
                "        elif oneLine == '%s':" %
                EDPluginExecProcessScript.START_OF_ENVIRONMENT,
                "            dictEnv={}", "    return dictEnv", ""
            ]

            cmdline += "echo %secho %s%s%s" % (
                EDUtilsPlatform.cmdSep,
                EDPluginExecProcessScript.START_OF_ENVIRONMENT,
                EDUtilsPlatform.cmdSep, EDUtilsPlatform.cmdEnv)
            listScript += [
                'subPre = subprocess.Popen("""%s""",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)'
                % cmdline,
                'threadStdErr = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdErr", args=(subPre.stderr,"preExecute.err"))',
                'threadStdOut = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdOut", args=(subPre.stdout,"preExecute.log"))',
                'threadStdErr.start()', 'threadStdOut.start()',
                'threadStdErr.join()', 'threadStdOut.join()', 'subPre.wait()',
                'dictEnv=getEnvironment("preExecute.log")'
            ]
        else:
            listScript.append('dictEnv=os.environ')

#        # Execution
        listScript.append("")

        strcmd = "%s %s" % (EDUtilsPlatform.escape(self.__strScriptExecutable),
                            EDUtilsPlatform.escape(
                                self.__strScriptCommandline))

        listCommandLine = shlex.split(str(strcmd))
        if self.__listCommandExecution == []:
            listScript.append(
                'subP = subprocess.Popen(%s,stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dictEnv)'
                % listCommandLine)
        else:
            listScript.append(
                'subP = subprocess.Popen(%s, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dictEnv)'
                % listCommandLine)
            for strCommandExecution in self.__listCommandExecution:
                if sys.version.startswith('2'):
                    listScript.append(
                        'subP.stdin.write("%s%s")' %
                        (strCommandExecution, EDUtilsPlatform.escapedLinesep))
                else:
                    listScript.append(
                        'subP.stdin.write(bytes("%s%s", "utf-8"))' %
                        (strCommandExecution, EDUtilsPlatform.escapedLinesep))
            listScript.append('subP.stdin.close()')
        listScript += [
            'timer = threading.Timer(%s,kill,args=(subP.pid,))' %
            (iScriptTimeOut), 'timer.start()',
            'open("%s","wt").write("%%s %%s %%s"%%(subP.pid,socket.gethostname(),os.linesep)) '
            % EDUtilsPlatform.escape(self.__strPathToHostNamePidFile),
            'threadStdErr = threading.Thread(target=writeStdOutErr, name="WriteStdErr", args=(subP.stderr,"%s"))'
            % EDUtilsPlatform.escape(self.__strScriptErrorLogFileName),
            'threadStdOut = threading.Thread(target=writeStdOutErr, name="WriteStdOut", args=(subP.stdout,"%s"))'
            % EDUtilsPlatform.escape(self.__strScriptLogFileName),
            'threadStdErr.start()', 'threadStdOut.start()',
            'threadStdErr.join()', 'threadStdOut.join()', 'subP.wait()',
            'timer.cancel()'
            ''
        ]
        # Add post-execution commands - if any
        cmdline = ""
        for strCommandPostExecution in self.__listCommandPostExecution:
            if strCommandPostExecution.strip().endswith(" & "):
                cmdline += strCommandPostExecution
            else:
                cmdline += strCommandPostExecution + EDUtilsPlatform.cmdSep

            listScript.append(
                'subPost = subprocess.Popen("""%s""", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=dictEnv)'
                % cmdline)
            listScript.append(
                'threadStdErr = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdErr", args=(subPost.stderr,"postExecute.log"))'
            )
            listScript.append(
                'threadStdOut = threading.Thread(target=writeStdOutErr, name="PreExecuteWriteStdOut", args=(subPost.stdout,"postExecute.err"))'
            )
            listScript.append('threadStdErr.start()')
            listScript.append('threadStdOut.start()')
            listScript.append('threadStdErr.join()')
            listScript.append('threadStdOut.join()')
            listScript.append('subPost.wait()')

        listScript.append("sys.exit(subP.returncode)")
        listScript.append("")
        strPythonScript = os.linesep.join(listScript)
        return strPythonScript
 def testExecute(self):
     self.run()
     # Kill the server
     EDUtilsPlatform.kill(self.iPID)