Source code for mojo.libdist.ConfigurationFile

"""
ConfigurationFile
==================
Contains the class 'ConfigurationFile' which can read in all data stored in the config xml.
When it is initialized the object gets the filename of the xml and the name of the settings.
First you have to call readXML to parse the xml and save it as ElementTree. Then you can validate
it against the xsd using the validateWithSchema method.
buildBuildConfigurations get all data or just the data in the specified settings
"""


import os

from lxml import etree

from .BuildConfiguration import BuildConfiguration
from . import libdistData


[docs]class ConfigurationFile: """ Object to process content of XML configuration file. :param filename: name of the xml file :param settings: name of the settings to build :param traceSuitePath: path to the trace suite :type filename: string :type settings: list :type traceSuitePath: string """ def __init__(self, filename, settings, traceSuitePath): """ stores XML filename in object :param filename: name of the xml file :param settings: name of the settings to build :param traceSuitePath: path to the trace suite :type filename: string :type settings: list :type traceSuitePath: string """ # list of BuildConfiguration objects self.buildConfigurations = [] # file name of configuration XML file self.filename = filename # name of the settings to build self.settings = settings # trace suite path self.traceSuitePath = traceSuitePath # save the ElementTree self._eTree = None
[docs] def buildBuildConfigurations(self): """ Read the settings which are stored in the ElementTree For each setting a BuildConfiguration's object is generated In these objects all necessary information for buildung this setting is saved All buildConfigurations are appended to the buildConfigurations list """ eRoot = self._eTree.getroot() # find all setting stored in the element tree for child in eRoot.findall(libdistData.BUILDCONFIGURATIONS_SET): eleName = child.find(libdistData.BUILDCONFIGURATIONS_SET_NAME) # if the set is part of the settings which are defined in the command line options a buildConfiguration is built # if no setting are defined in the command line options to all sets a buildConfiguration is built if not self.settings or eleName.text in self.settings: buildConfiguration = BuildConfiguration(eleName.text) # add all modules to the buildConfiguration eleModules = child.find(libdistData.BUILDCONFIGURATIONS_SET_MODULES) for module in eleModules: if module.tag == libdistData.BUILDCONFIGURATIONS_SET_MODULES_MODULE: buildConfiguration.addModules(libdistData.BUILDCONFIGURATIONS_SET_MODULES_MODULE, module.text) elif module.tag == libdistData.BUILDCONFIGURATIONS_SET_MODULES_MPI: mpiModule = module.text buildConfiguration.addModules(libdistData.BUILDCONFIGURATIONS_SET_MODULES_MPI, mpiModule) elif module.tag == libdistData.BUILDCONFIGURATIONS_SET_MODULES_COMPILER: try: compilerModule = libdistData.COMPILER[module.text] except KeyError: compilerModule = module.text buildConfiguration.addModules(libdistData.BUILDCONFIGURATIONS_SET_MODULES_COMPILER, compilerModule) elif module.tag == libdistData.BUILDCONFIGURATIONS_SET_MODULES_CGNS: cgnsModule = module.text buildConfiguration.addModules(libdistData.BUILDCONFIGURATIONS_SET_MODULES_CGNS, cgnsModule) elif module.tag == libdistData.BUILDCONFIGURATIONS_SET_MODULES_VTK: vtkModule = module.text buildConfiguration.addModules(libdistData.BUILDCONFIGURATIONS_SET_MODULES_VTK, vtkModule) # try to add all make commands to the build configuration try: eleMake = child.find(libdistData.BUILDCONFIGURATIONS_SET_MAKE) for command in eleMake.itertext(): buildConfiguration.addMakeCommand(command) except AttributeError: pass # try to add all macro change sets to the build configuration try: eleMacros = child.find(libdistData.BUILDCONFIGURATIONS_SET_MACROS) for headerfile in eleMacros: elePath = headerfile.find(libdistData.BUILDCONFIGURATIONS_SET_MACROS_HEADERFILE_PATH) headerPath = elePath.text for macro in headerfile.findall(libdistData.BUILDCONFIGURATIONS_SET_MACROS_HEADERFILE_MACRO): eleMacroName = macro.find(libdistData.BUILDCONFIGURATIONS_SET_MACROS_HEADERFILE_MACRO_NAME) macroName = eleMacroName.text eleMacroValue = macro.find(libdistData.BUILDCONFIGURATIONS_SET_MACROS_HEADERFILE_MACRO_VALUE) macroValue = eleMacroValue.text buildConfiguration.addMacroChangeSet((headerPath, macroName, macroValue)) except TypeError: pass self.buildConfigurations.append(buildConfiguration)
# TODO: control if there is no duplicate settings name
[docs] def readXML(self): """Read file and save ElementTree in _eTree. """ if self.filename: filename = self.filename else: raise IOError("Received no XML path in command line options and no XML found in default paths.") parser = etree.XMLParser(remove_blank_text=True) with open(filename, "r") as xmlFile: self._eTree = etree.parse(xmlFile, parser)
[docs] def validateWithSchema(self): """ ElementTree against XSD schema file. You'll get back true if the document is valid against the XML schema, and false if not. :return: return true if the document is valid, and false if not :rtype: boolean """ if os.path.isfile(os.path.join(self.traceSuitePath, libdistData.XML_SCHEMA_FILE)): schemaFilePath = os.path.join(self.traceSuitePath, libdistData.XML_SCHEMA_FILE) elif os.path.isfile(libdistData.DATA_XML_SCHEMA_FILE): schemaFilePath = libdistData.DATA_XML_SCHEMA_FILE else: print(libdistData.DATA_XML_SCHEMA_FILE) raise IOError("Found no XML schema file in default paths.") with open(schemaFilePath, "r") as schemaFile: schemaFileParsed = etree.parse(schemaFile) schema = etree.XMLSchema(schemaFileParsed) if not schema.validate(self._eTree): raise NotValidAgainstXmlException()
[docs]class NotValidAgainstXmlException(Exception): def __init__(self): super(NotValidAgainstXmlException, self).__init__() self.message = ("\n\nXml is not valid against the xml schema\nTry to correct " + "it with the Eclipse validate function which marks the error in the xml ") def __str__(self): return self.message
[docs]def createBuildConfigurations(configFilePath, settings, traceSuitePath): """ creates ConfigurationFile instance, reads the xml at the given configFilePath and saves the information stored in it. :param configFilePath: configuration file path :param settings: name of the settings to build :param traceSuitePath: path to the trace suite :type configFilePath: string :type settings: list :type traceSuitePath: string :return: buildConfigurations :rtype: list of BuildConfiguration instances """ configurationFile = ConfigurationFile(configFilePath, settings, traceSuitePath) configurationFile.readXML() configurationFile.validateWithSchema() configurationFile.buildBuildConfigurations() return configurationFile.buildConfigurations