Source code for mojo.pavayo.executables.singleExecutableResource

"""
Settings for an executable
===========================

In the class ExecutableResources all relevant executables are stored. Using this class Pavayo becomes independent of the settings
for the TRACE Suite.
"""


import configparser
from distutils.version import StrictVersion, LooseVersion
import json
import os

from . import executableData as execConsts
from .version import Version


[docs]class SingleExecutableResource: """Settings for a single executable :param environmentName: environment name :param path: path of executable :param commandLineArguments: additional command line arguments :param numberOfLicenses: number of available licenses :param maxProcsPerExecution: maximum number of processors per execution :param displayVersion: flag whether executable is shown in screen output and report :param successStatement: list of success statements :param failStatement: list of fail statements :param ignoreExitCode: flag whether the exit code will be ignored :param versionCheck: flag whether version should be checked :type environmentName: str :type path: str :type commandLineArguments: list(str) or None :type numberOfLicenses: int or None :type maxProcsPerExecution: int or None :type displayVersion: bool :type successStatement: list or None :type failStatement: list or None :type ignoreExitCode: bool :type versionCheck: bool """ def __init__(self, environmentName="", path="", commandLineArguments=None, numberOfLicenses=None, maxProcsPerExecution=None, displayVersion=False, successStatement=None, failStatement=None, ignoreExitCode=False, versionCheck=True, versionCheckCommand=None): """Initiate class. :param environmentName: environment name :param path: path of executable :param commandLineArguments: additional command line arguments :param numberOfLicenses: number of available licenses :param maxProcsPerExecution: maximum number of processors per execution :param displayVersion: flag whether executable is shown in screen output and report :param successStatement: list of success statements :param failStatement: list of fail statements :param ignoreExitCode: flag whether the exit code will be ignored :param versionCheck: flag whether version should be checked :param versionCheckCommand: command line argument for version check :type environmentName: str :type path: str :type commandLineArguments: list(str) or str or None :type numberOfLicenses: int or None :type maxProcsPerExecution: int or None :type displayVersion: bool :type successStatement: list or None :type failStatement: list or None :type ignoreExitCode: bool :type versionCheck: bool :type versionCheckCommand: str or None """ self.environmentName = environmentName self.path = path if commandLineArguments is None: self.commandLineArguments = list() elif isinstance(commandLineArguments, str): self.commandLineArguments = [commandLineArguments, ] else: self.commandLineArguments = commandLineArguments self.numberOfLicenses = numberOfLicenses self.maxProcsPerExecution = maxProcsPerExecution self.displayVersion = displayVersion self.successStatement = successStatement or list() self.failStatement = failStatement or list() self.ignoreExitCode = ignoreExitCode if versionCheck: self.versionSettings = Version(versionCheckCommand) if self.commandLineArguments: self.versionSettings.args += " " + " ".join(self.commandLineArguments) else: self.versionSettings = None def _getVersionFromExecutable(self): """Gets version from the executable """ self.versionSettings.getVersion(self.path) def _getVersionFromJsonFile(self, statusFile=None): """Gets the version from the JSON status file :param statusFile: name of status file in the JSON format :type statusFile: None or str """ statusFile = statusFile if statusFile else execConsts.CONFIG_FILE_PATH with open(statusFile) as jsonFile: execName = self.environmentName[1:] content = json.load(jsonFile) execDict = content['_resourceDict'] try: versionString = ".".join(str(x) for x in execDict[execName]['versionSettings']['version']['version']) except KeyError: pass else: try: self.versionSettings.version = StrictVersion(versionString) except (TypeError, ValueError): self.versionSettings.version = LooseVersion(versionString) def _getVersionFromStatusFile(self, statusFile=None): """Gets the version from the old status file :param statusFile: name of status file :type statusFile: Optional[ConfigParser/str] """ if isinstance(statusFile, configparser.ConfigParser): configFile = statusFile else: statusFile = statusFile or execConsts.CONFIG_FILE_PATH configFile = configparser.ConfigParser() configFile.read(statusFile) versionDict = dict(configFile.items(execConsts.CONFIG_FILE_SECTION_SOFTWARE)) try: self.versionSettings.version = StrictVersion(versionDict[self.environmentName[1:].lower()]) except ValueError: self.versionSettings.version = LooseVersion(versionDict[self.environmentName[1:].lower()]) except KeyError: pass
[docs] def getVersion(self, options=None): """Determines the versions of the executable :param options: options parsed from the command line :type options: argParse.ArgumentParser """ if self.versionSettings is not None: if isinstance(self.path, str): if options is None or options.getVersionsFromExecutable: self._getVersionFromExecutable() elif isinstance(options.configFile, configparser.ConfigParser) or os.path.splitext(options.configFile)[1] != ".cfg": self._getVersionFromStatusFile(statusFile=options.configFile) else: self._getVersionFromJsonFile(statusFile=options.configFile)
@property def version(self): """Getter for the versin of the executable :return: version :rtype: StrictVersion or LooseVersion or str """ try: return self.versionSettings.version except AttributeError: return "" def __str__(self): """Returns the string output in a neat form :return: neat output :rtype: str """ outputText = list() _members = [attr for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__") and getattr(self, attr)] for member in _members: outputText.append(" {}: {}".format(member, getattr(self, member))) return "\n".join(outputText)