Source code for mojo.pavayo.executables.version

"""
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 shlex


from distutils.version import StrictVersion, LooseVersion
import subprocess
import sys

from ...bricabrac.fileIO import Printer


[docs]class Version: """Settings for the version of an executable. """ def __init__(self, versionArgument=None): """Settings for the version of an executable. """ self.version = None self.args = "--version" if versionArgument is not None: self.args = versionArgument self.key = "version" self.expression = None
[docs] def getVersion(self, path): """Get the version of the given executable :param path: path of executable :type path: str :return: version of executable :rtype: StrictVersion or LooseVersion """ if self.expression is None: if path.lower() in ("python", "python3"): self.version = StrictVersion("{info[0]}.{info[1]}.{info[2]}".format(info=sys.version_info)) else: versionText = getSoftwareVersionNumberString(path, versionArgument=self.args, versionKey=self.key) try: self.version = StrictVersion(versionText) except ValueError: self.version = LooseVersion(versionText) assert hasattr(self.version, "version"), f"The Version of '{path}' could not be parsed, got: '{versionText}'" else: self.version = None return self.version
[docs]def callSoftwareVersionOption(software, versionArgument="--version"): """Calls <SOFTWARE> --version. Returns the output string. :param software: path of the executable :param versionArgument: argument to get the version :type software: str :type versionArgument: str :return: output of the call :rtype: str """ try: commandLine = f"{software} {versionArgument}" try: commandLine = shlex.split(commandLine) except ValueError: commandLine = commandLine.split() return subprocess.check_output(commandLine, encoding='utf8') except subprocess.CalledProcessError as e: Printer.warning("Executing '{}' failed: {}".format(' '.join(commandLine), str(e))) return str(e) except OSError as e: Printer.warning("Could not execute '{}': {}".format(" ".join(commandLine), str(e))) return str(e)
[docs]def getSoftwareVersionNumberString(software, versionArgument="--version", versionKey="version"): """Get just the version number from the output of <SOFTWARE> <VERSION_ARGUMENT>. :param software: path of the executable :param versionArgument: argument to get the version :param versionKey: key to find version string in output :type software: str :type versionArgument: str :type versionKey: str :return: version string :rtype: str """ versionRead = "" versionKey = versionKey.lower() output = callSoftwareVersionOption(software, versionArgument=versionArgument) outputList = [item.lower() for item in output.split()] try: # output pattern: ..... .... version 1.1.1 .... .... versionRead = outputList[outputList.index(versionKey) + 1] except (KeyError, IndexError, ValueError): # allow one single token if len(outputList) == 1: versionRead = outputList[0] return versionRead
[docs]def is_version_str_equal(version1: str, version2: str) -> bool: """Compare to version strings piece by piece :param version1: first version string :param version2: second version string :return: flag whether versions are identical """ assert (isinstance(version1, str) and isinstance(version2, str)), "At least one parameter is not a string!" def _formatInt(txt): try: retVal = int(txt) except ValueError: retVal = txt return retVal version1Split = [_formatInt(txt) for txt in version1.split('.')] version2Split = [_formatInt(txt) for txt in version2.split('.')] return version1Split == version2Split