def preparePythonWrapperScript(self): listScript = ["#!%s" % sys.executable, ""] listScript += ["import os, sys, xmlrpclib"] listScript += [ """pyXmlrcplibProxy = xmlrpclib.ServerProxy('http://%s:%d')""" % (self.__edServerXMLRCP.getHost(), self.__edServerXMLRCP.getPort()) ] listScript += ["""os.chdir("%s")""" % self.getWorkingDirectory()] listScript += ["""os.environ['EDNA_HOME']="%s" """ % EDUtilsPath.getEdnaHome()] listScript += ["""os.environ['EDNA_SITE']="%s" """ % EDUtilsPath.getEdnaSite()] listScript += ["""sys.path.append(os.path.join(os.environ["EDNA_HOME"], "kernel", "src"))"""] listScript += ["""from EDFactoryPluginStatic import EDFactoryPluginStatic"""] listScript += ["""from EDUtilsFile import EDUtilsFile"""] listScript += ["""edPlugin = EDFactoryPluginStatic.loadPlugin("%s")""" % self.__strNameOfPlugin] for strValue in self.__dictXMLDataInput: if strValue == self.getDefaultInputDataKey(): listScript += ['edPlugin.setDataInput("""%s""")' "" % self.__dictXMLDataInput[strValue]] else: listScript += ['edPlugin.setDataInput("""%s""", "%s")' % (self.__dictXMLDataInput[strValue], strValue)] listScript += ["""def actionSuccessFile(_edPlugin=None):"""] listScript += [""" strSuccessMessage = "" """] listScript += [""" for strKey in _edPlugin.getListOfDataOutputKeys():"""] listScript += [""" if strKey == _edPlugin.getDefaultOutputDataKey():"""] listScript += [""" strXMLOutput = _edPlugin.getDataOutput().marshal()"""] listScript += [""" strSuccessMessage += strKey + "\\n" """] listScript += [ """ EDUtilsFile.writeFile(os.path.join("%s", "XSDataResult_"+strKey+".xml"), strXMLOutput)""" % self.getWorkingDirectory() ] listScript += [""" else:"""] listScript += [""" iIndex = 1"""] listScript += [""" for xsDataResult in _edPlugin.getDataOutput(strKey):"""] listScript += [""" strXMLOutput = xsDataResult.marshal()"""] listScript += [""" strSuccessMessage += "%s_%d\\n" % (strKey, iIndex) """] listScript += [ """ EDUtilsFile.writeFile(os.path.join("%s", "XSDataResult_"+strKey+"_%%d.xml") %% iIndex, strXMLOutput)""" % self.getWorkingDirectory() ] listScript += [""" """] listScript += [ """ EDUtilsFile.writeFile(os.path.join("%s", "SUCCESS.txt"), strSuccessMessage)""" % self.getWorkingDirectory() ] listScript += ["""def actionSuccessXMLRCP(_edPlugin=None):"""] listScript += [""" pyXmlrcplibProxy.callbackSUCCESS("%d", [])""" % self.getId()] listScript += [""" for strKey in _edPlugin.getListOfDataOutputKeys():"""] listScript += [""" strXMLOutput = _edPlugin.getDataOutput().marshal()"""] listScript += [""" pyXmlrcplibProxy.setDataOutput("%d", strXMLOutput)""" % self.getId()] listScript += ["""def actionFailure(_edObject=None):"""] listScript += [""" pyXmlrcplibProxy.callbackFAILURE("%d", [])""" % self.getId()] listScript += ["""#edPlugin.connectSUCCESS(actionSuccessFile)"""] listScript += ["""edPlugin.connectSUCCESS(actionSuccessXMLRCP)"""] listScript += ["""edPlugin.connectFAILURE(actionFailure)"""] listScript += ["""edPlugin.executeSynchronous()"""] listScript += [""] listScript += [""] strPythonWrapper = "" for strLine in listScript: strPythonWrapper = strPythonWrapper + strLine + "\n" return strPythonWrapper
def sendEmail(self, _strSubject, _strMessage): """Sends an email to the EDNA contact person (if configured).""" self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Subject = %s" % _strSubject) self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Message:") self.DEBUG(_strMessage) if self.strEDNAContactEmail == None: self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: No email address configured!") elif not EDUtilsPath.getEdnaSite().startswith("ESRF"): self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Not executed at the ESRF! EDNA_SITE=%s" % EDUtilsPath.getEdnaSite()) else: try: self.DEBUG("Sending message to %s." % self.strEDNAContactEmail) self.DEBUG("Message: %s" % _strMessage) strMessage = """ EDNA_HOME = %s EDNA_SITE = %s PLUGIN_NAME = %s working_dir = %s %s """ % (EDUtilsPath.getEdnaHome(), EDUtilsPath.getEdnaSite(), self.getPluginName(), self.getWorkingDirectory(), _strMessage) strEmailMsg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % (self.strEDNAEmailSender, \ self.strEDNAContactEmail, \ _strSubject, strMessage)) server = smtplib.SMTP("localhost") server.sendmail(self.strEDNAEmailSender, self.strEDNAContactEmail, strEmailMsg) server.quit() except: self.ERROR("Error when sending email message!") self.writeErrorTrace()
def testGetProjectRootDirectory(self): edFactoryPluginTest = EDFactoryPluginTest() strEdnaHome = EDUtilsPath.getEdnaHome() strTestProjectRootDirectoryReference = EDUtilsPath.appendListOfPaths(strEdnaHome, [ "edna-kernel", "tests", "data", "EDFactoryPlugin", "testProject" ]) strTestProjectRootDirectory1 = edFactoryPluginTest.getProjectRootDirectory("EDTestCasePluginUnitTestPluginFactory") EDAssert.equal(strTestProjectRootDirectoryReference, strTestProjectRootDirectory1) strTestProjectRootDirectory2 = edFactoryPluginTest.getProjectRootDirectory("EDTestSuitePluginUnitTestProject") EDAssert.equal(strTestProjectRootDirectoryReference, strTestProjectRootDirectory2)
def testGetProjectRootDirectory(self): edFactoryPlugin = EDFactoryPlugin() strEdnaHome = EDUtilsPath.getEdnaHome() strTestProjectRootDirectoryReference = EDUtilsPath.appendListOfPaths(strEdnaHome, [ "kernel", "tests", "data", "EDFactoryPlugin", "testProject" ]) strTestProjectRootDirectory1 = edFactoryPlugin.getProjectRootDirectory("EDPluginTestPluginFactory") EDAssert.equal(strTestProjectRootDirectoryReference, strTestProjectRootDirectory1) strTestProjectRootDirectory2 = edFactoryPlugin.getProjectRootDirectory("XSDataTestProject") EDAssert.equal(strTestProjectRootDirectoryReference, strTestProjectRootDirectory2) strTestProjectRootDirectory3 = edFactoryPlugin.getProjectRootDirectory("PluginThatNotExists") EDAssert.equal(None, strTestProjectRootDirectory3)
def __init__(self, _pyStrTestName=None): EDTestCase.__init__(self, _pyStrTestName) strMXv1DataHome = EDUtilsTest.getPluginTestDataDirectory(self.getClassName()) strDataDir = "EDHandlerISPyBv1_3" self.strDataPath = os.path.join(strMXv1DataHome, strDataDir) self.dictReplace = {"${EDNA_TESTIMAGES}": EDUtilsPath.EDNA_TESTIMAGES, "${EDNA_HOME}": EDUtilsPath.getEdnaHome(), "${USER}": os.getenv("USER", "UndefindedUser"), "${TMPDIR}": os.getenv("TMPDIR", tempfile.gettempdir()), }
def preProcess(self): """ Creates the application working directory (log dir) Initializes the configuration retrieves the plugin xml data to be passed to the plugin """ EDVerbose.DEBUG("EDApplication.preProcess") self.processCommandline() if (not self.__bIsFailure): # Check that the plugin can be located strPluginLocation = EDFactoryPluginStatic.getFactoryPlugin().getModuleLocation(self.__strPluginName) if (strPluginLocation is None): EDVerbose.error("Plugin %s cannot be loaded!" % self.__strPluginName) self.__bIsFailure = True # Check that the input file can be read if (self.getDataInputFilePath() is not None) and (not os.path.exists(self.__strDataInputFilePath)): EDVerbose.error("Input XML file not found : %s" % self.__strDataInputFilePath) self.__bIsFailure = True # Check that the output file can be created if (self.__strDataOutputFilePath is not None): strOutputDirectory = os.path.dirname(self.__strDataOutputFilePath) if (strOutputDirectory is None or strOutputDirectory == ""): strOutputDirectory = os.getcwd() self.__strDataOutputFilePath = os.path.join(strOutputDirectory, self.__strDataOutputFilePath) if (not os.access(strOutputDirectory, os.W_OK)): EDVerbose.error("Output directory not writable: %s" % strOutputDirectory) self.__bIsFailure = True elif (os.path.exists(self.__strDataOutputFilePath)): if (not os.access(self.__strDataOutputFilePath, os.W_OK)): EDVerbose.error("Output file not writable: %s" % self.__strDataOutputFilePath) self.__bIsFailure = True if (not self.__bIsFailure): EDVerbose.DEBUG("EDApplication.PLUGIN_PARAM_LABEL: " + EDApplication.PLUGIN_PARAM_LABEL) # Load the configuration file if(self.__strConfigurationFileName is None): self.__strConfigurationHome = EDApplication.getConfigurationHome(self.__strPluginName) self.__strConfigurationFileName = os.path.abspath(os.path.join(self.__strConfigurationHome, "XSConfiguration_%s.xml" % EDUtilsPath.getEdnaSite())) if (os.path.exists(self.__strConfigurationFileName)): EDVerbose.screen("Loading Configuration file: %s" % self.__strConfigurationFileName) edConfiguration = EDConfiguration(self.__strConfigurationFileName) self.setConfiguration(edConfiguration) self.loadConfiguration() EDVerbose.DEBUG("EDApplication.preProcess: Checking... Number of plugins...: %d" % EDApplication.__edConfiguration.getPluginListSize()) pyDictionary = {} pyDictionary[ "${EDNA_HOME}" ] = EDUtilsPath.getEdnaHome() if self.getDataInputFilePath() is not None: self.__strXMLData = EDUtilsFile.readFileAndParseVariables(self.getDataInputFilePath(), pyDictionary) else: EDVerbose.warning("Cannot find configuration file: %s" % self.__strConfigurationFileName) # Create the application working directory if(self.__strWorkingDir is None): self.__strWorkingDir = self.__strApplicationInstanceName self.createApplicationWorkingDirectory()
def testCreateDNAFileDirectoryPath(self): pluginMXCuBE = self.createPlugin() pluginMXCuBE.configure() strResultCharacterisationFileName = "XSDataResultCharacterisation_reference.xml" strPathToReferenceFile = os.path.join(self.getPluginTestsDataHome(), strResultCharacterisationFileName) strXML = self.readAndParseFile(strPathToReferenceFile) xsDataResultCharacterisation = XSDataResultCharacterisation.parseString(strXML) strDNAFileDirectoryPath = pluginMXCuBE.createDNAFileDirectoryPath(xsDataResultCharacterisation) strDNAFileDirectoryPathExpected = os.path.join(EDUtilsPath.getEdnaHome(), "tests", "data", "images", "testscale_1_dnafiles") EDAssert.equal(strDNAFileDirectoryPathExpected, strDNAFileDirectoryPath)
def testGetPathToProjectConfigurationFile(self): edConfiguration = EDConfiguration() strPathToConfigurationFile1 = edConfiguration.getPathToProjectConfigurationFile("EDPluginTestPluginFactory") strPathToConfigurationFileReference1 = EDUtilsPath.appendListOfPaths(EDUtilsPath.getEdnaHome(), [ "kernel", "tests", "data", "EDFactoryPlugin", \ "testProject", "conf", "XSConfiguration_TestSite.xml" ]) EDAssert.equal(strPathToConfigurationFileReference1, strPathToConfigurationFile1) EDUtilsPath.setEdnaSite("NonexistingTestSite") strPathToConfigurationFile2 = edConfiguration.getPathToProjectConfigurationFile("EDPluginTestPluginFactory") strPathToConfigurationFileReference2 = None EDAssert.equal(strPathToConfigurationFileReference2, strPathToConfigurationFile2)
def sendEmail(self, _strSubject, _strMessage): """Sends an email to the EDNA contact person (if configured).""" strTime = "%.1f s" % (self.tStop - self.tStart) if EDUtilsPath.isESRF(): strPathImage = None for dataSet in self.dataInput.dataSet: for imageFile in dataSet.imageFile: strPathImage = imageFile.path.value break if strPathImage is not None: (strBeamline, strProposal, strPrefix) = self.getBeamlineProposalFromPath(strPathImage) else: strBeamline = "Unknown" strProposal = "Unknown" strPrefix = "Unknown" strHost = socket.gethostname() strSubject = "EDNA ch %s %s %s %s %s (%s)" % (_strSubject, strBeamline, strProposal, strPrefix, strHost, strTime) else: strSubject = "EDNA %s : %s (%s)" % (_strSubject, EDUtilsPath.getEdnaSite(), strTime) self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_4.sendEmail: Subject = %s" % strSubject) self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_4.sendEmail: Message:") self.DEBUG(_strMessage) if self.strEDNAContactEmail == None: self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_4.sendEmail: No email address configured!") elif not EDUtilsPath.getEdnaSite().startswith("ESRF"): self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_4.sendEmail: Not executed at the ESRF! EDNA_SITE=%s" % EDUtilsPath.getEdnaSite()) else: try: self.DEBUG("Sending message to %s." % self.strEDNAContactEmail) self.DEBUG("Message: %s" % _strMessage) strMessage = "EDNA_HOME = %s\n" % EDUtilsPath.getEdnaHome() strMessage += "EDNA_SITE = %s\n" % EDUtilsPath.getEdnaSite() strMessage += "PLUGIN_NAME = %s\n" % self.getPluginName() strMessage += "working_dir = %s\n\n" % self.getWorkingDirectory() strMessage += "Reference images:\n" xsDataInputMXCuBE = self.getDataInput() for xsDataSetMXCuBE in xsDataInputMXCuBE.getDataSet(): for xsDataFile in xsDataSetMXCuBE.getImageFile(): strMessage += "%s\n" % xsDataFile.getPath().getValue() strMessage += "\n" strMessage += _strMessage strEmailMsg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % (self.strEDNAEmailSender, \ self.strEDNAContactEmail, \ strSubject, strMessage)) server = smtplib.SMTP("localhost") server.sendmail(self.strEDNAEmailSender, self.strEDNAContactEmail, strEmailMsg) server.quit() except: self.ERROR("Error when sending email message!") self.writeErrorTrace()
def testGetPathToProjectConfigurationFile(self): EDUtilsPath.setEdnaSite("TestSite") edFactoryPlugin = EDFactoryPlugin() strPathToConfigurationFile1 = edFactoryPlugin.getPathToProjectConfigurationFile("EDPluginTestPluginFactory") strPathToConfigurationFileReference1 = EDUtilsPath.appendListOfPaths(EDUtilsPath.getEdnaHome(), [ "edna-kernel", "tests", "data", "EDFactoryPlugin", \ "testProject", "conf", "XSConfiguration_TestSite.xml" ]) EDAssert.equal(strPathToConfigurationFileReference1, strPathToConfigurationFile1) EDUtilsPath.setEdnaSite("NonexistingTestSite") strPathToConfigurationFile2 = edFactoryPlugin.getPathToProjectConfigurationFile("EDPluginTestPluginFactory") strPathToConfigurationFileReference2 = None EDAssert.equal(strPathToConfigurationFileReference2, strPathToConfigurationFile2) # Since this is a static variable we need to reset it to None in order not to break any other tests... EDUtilsPath.setEdnaSite(None)
def __init__(self, _strPluginName, _strPluginDir=None, _strTestName=None): """ Initialize the test case by determining the paths to the plugin home and plugin test directories. """ EDTestCase.__init__(self, _strTestName) self.__strPluginName = _strPluginName self.__strTestsDataDir = None self.__strPluginTestsDataDir = None self.__strPluginHome = EDUtilsTest.getFactoryPluginTest().getModuleLocation(_strPluginName) self.__strPluginTestsDataHome = EDUtilsTest.getPluginTestDataDirectory(self.getClassName()) self.__listRequiredConfigurationPluginNames = [] self.__strConfigurationFile = None self.__dictConfigurations = {} #key=pluginName ; value=config self.dictReplace = {"${EDNA_TESTS_DATA_HOME}": EDUtilsTest.getTestsDataHome(), "${EDNA_PLUGIN_TESTS_DATA_HOME}" : self.getPluginTestsDataHome(), "${EDNA_HOME}": EDUtilsPath.getEdnaHome(), "${USER}": os.getenv("USER", "UndefindedUser"), "${TMPDIR}": os.getenv("TMPDIR", tempfile.gettempdir()), }
def sendEmail(self, _strSubject, _strMessage): """Sends an email to the EDNA contact person (if configured).""" self.DEBUG( "EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Subject = %s" % _strSubject) self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Message:") self.DEBUG(_strMessage) if self.strEDNAContactEmail == None: self.DEBUG( "EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: No email address configured!" ) elif not EDUtilsPath.getEdnaSite().startswith("ESRF"): self.DEBUG( "EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Not executed at the ESRF! EDNA_SITE=%s" % EDUtilsPath.getEdnaSite()) else: try: self.DEBUG("Sending message to %s." % self.strEDNAContactEmail) self.DEBUG("Message: %s" % _strMessage) strMessage = """ EDNA_HOME = %s EDNA_SITE = %s PLUGIN_NAME = %s working_dir = %s %s """ % (EDUtilsPath.getEdnaHome(), EDUtilsPath.getEdnaSite(), self.getPluginName(), self.getWorkingDirectory(), _strMessage) strEmailMsg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % (self.strEDNAEmailSender, \ self.strEDNAContactEmail, \ _strSubject, strMessage)) server = smtplib.SMTP("localhost") server.sendmail(self.strEDNAEmailSender, self.strEDNAContactEmail, strEmailMsg) server.quit() except: self.ERROR("Error when sending email message!") self.writeErrorTrace()
def sendEmail(self, _strSubject, _strMessage): """Sends an email to the EDNA contact person (if configured).""" strTime = "%.1f s" % (self.tStop - self.tStart) if EDUtilsPath.isESRF(): strPathImage = None for dataSet in self.dataInput.dataSet: for imageFile in dataSet.imageFile: strPathImage = imageFile.path.value break if strPathImage is not None: (strBeamline, strProposal, strPrefix) = self.getBeamlineProposalFromPath(strPathImage) else: strBeamline = "Unknown" strProposal = "Unknown" strPrefix = "Unknown" strHost = socket.gethostname() strSubject = "EDNA ch %s %s %s %s %s (%s)" % ( _strSubject, strBeamline, strProposal, strPrefix, strHost, strTime) else: strSubject = "EDNA %s : %s (%s)" % ( _strSubject, EDUtilsPath.getEdnaSite(), strTime) self.DEBUG( "EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Subject = %s" % strSubject) self.DEBUG("EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Message:") self.DEBUG(_strMessage) if self.strEDNAContactEmail == None: self.DEBUG( "EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: No email address configured!" ) elif not EDUtilsPath.getEdnaSite().startswith("ESRF"): self.DEBUG( "EDPluginControlInterfaceToMXCuBEv1_3.sendEmail: Not executed at the ESRF! EDNA_SITE=%s" % EDUtilsPath.getEdnaSite()) else: try: self.DEBUG("Sending message to %s." % self.strEDNAContactEmail) self.DEBUG("Message: %s" % _strMessage) strMessage = "EDNA_HOME = %s\n" % EDUtilsPath.getEdnaHome() strMessage += "EDNA_SITE = %s\n" % EDUtilsPath.getEdnaSite() strMessage += "PLUGIN_NAME = %s\n" % self.getPluginName() strMessage += "working_dir = %s\n\n" % self.getWorkingDirectory( ) strMessage += "Reference images:\n" xsDataInputMXCuBE = self.getDataInput() for xsDataSetMXCuBE in xsDataInputMXCuBE.getDataSet(): for xsDataFile in xsDataSetMXCuBE.getImageFile(): strMessage += "%s\n" % xsDataFile.getPath().getValue() strMessage += "\n" strMessage += _strMessage strEmailMsg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % (self.strEDNAEmailSender, \ self.strEDNAContactEmail, \ strSubject, strMessage)) server = smtplib.SMTP("localhost") server.sendmail(self.strEDNAEmailSender, self.strEDNAContactEmail, strEmailMsg) server.quit() except: self.ERROR("Error when sending email message!") self.writeErrorTrace()
def preProcess(self): """ Creates the application working directory (log dir) Initializes the configuration retrieves the plugin xml data to be passed to the plugin """ EDVerbose.DEBUG("EDApplication.preProcess") self.processCommandline() if (not self.__bIsFailure): # Check that the plugin can be located strPluginLocation = EDFactoryPluginStatic.getFactoryPlugin( ).getModuleLocation(self.__strPluginName) if (strPluginLocation is None): EDVerbose.error("Plugin %s cannot be loaded!" % self.__strPluginName) self.__bIsFailure = True # Check that the input file can be read if (self.getDataInputFilePath() is not None) and ( not os.path.exists(self.__strDataInputFilePath)): EDVerbose.error("Input XML file not found : %s" % self.__strDataInputFilePath) self.__bIsFailure = True # Check that the output file can be created if (self.__strDataOutputFilePath is not None): strOutputDirectory = os.path.dirname( self.__strDataOutputFilePath) if (strOutputDirectory is None or strOutputDirectory == ""): strOutputDirectory = os.getcwd() self.__strDataOutputFilePath = os.path.join( strOutputDirectory, self.__strDataOutputFilePath) if (not os.access(strOutputDirectory, os.W_OK)): EDVerbose.error("Output directory not writable: %s" % strOutputDirectory) self.__bIsFailure = True elif (os.path.exists(self.__strDataOutputFilePath)): if (not os.access(self.__strDataOutputFilePath, os.W_OK)): EDVerbose.error("Output file not writable: %s" % self.__strDataOutputFilePath) self.__bIsFailure = True if (not self.__bIsFailure): EDVerbose.DEBUG("EDApplication.PLUGIN_PARAM_LABEL: " + EDApplication.PLUGIN_PARAM_LABEL) if self.__strConfigurationFileName is not None: # Load the configuration file if (os.path.exists(self.__strConfigurationFileName)): EDVerbose.screen("Loading Configuration file: %s" % self.__strConfigurationFileName) EDConfigurationStatic.addConfigurationFile( self.__strConfigurationFileName, _bReplace=True) else: EDVerbose.warning("Cannot find configuration file: %s" % self.__strConfigurationFileName) pyDictionary = {} pyDictionary["${EDNA_HOME}"] = EDUtilsPath.getEdnaHome() if self.getDataInputFilePath() is not None: self.__strXMLData = EDUtilsFile.readFileAndParseVariables( self.getDataInputFilePath(), pyDictionary) # Create the application working directory if (self.__strWorkingDir is None): self.__strWorkingDir = self.__strApplicationInstanceName self.createApplicationWorkingDirectory()
def getTestsHome(): """ Returns the tests home directory path <EDNA_HOME>/tests """ strTestsHome = os.path.join(EDUtilsPath.getEdnaHome(), "tests") return strTestsHome