26. software — Detecting and checking installed software

A module to help with detecting required software and helper software, and to check the versions of it.

26.1. Classes defined in module software

class software.Software(name)[source]

Register for software versions.

This class holds a register of the version of installed software. The class is not intended to be used directly, but rather through the derived classes Module and External.

Parameters:name (str) – The software name as known in pyFormex: this is often the same as the real software name, but can be different if the real software name is complex. We try to use simple lower case names in pyFormex.

The default software object only has two attributes:

name

The registered name of the software.

Type:str
version[source]

The version of the software. This is set to None when the name is registered, and becomes a (possibly empty) string after calling the detect() method.

Type:str

Examples

>>> np = Software('numpy')
>>> Software.print_all()
  numpy (** Not Found **)
>>> Software.has('numpy')
''
>>> np.detect('detected')
'detected'
>>> Software.has('numpy')
'detected'
>>> Software.require('numpy')
>>> Software.has('foo')
Traceback (most recent call last):
ValueError: foo is not a registered Software
>>> foo = Software('foo')
>>> Software.require('foo')
Traceback (most recent call last):
ValueError: Required Software 'foo' (foo) not found
>>> Software.print_all()
  numpy (detected)
  foo (** Not Found **)
>>> Software('foo')
Traceback (most recent call last):
ValueError: A Software with name 'foo' is already registered
version()[source]

Return the version of the software, if installed.

If the software has already been detected, just returns the stored version string. Else, runs the software detect() method and stores and returns the version string.

Returns:str – The version of the software, or an empty string if the software can not be detected.
detect(version='', fatal=False, quiet=False)[source]

Detect the version of the software.

Parameters:
  • version (str) – The version string to be stored for a detected software. The default empty string means the software was not detected. Derived classes should call this method fromt their detect method and pass a non-empty string for detected softwares.
  • fatal (bool, optional) – If True and the software can not be loaded, a fatal exception is raised. Default is to silently ignore the problem and return an empty version string.
  • quiet (bool, optional) – If True, the whole operation is done silently. The only information about failure will be the returned empty version string.
Returns:

str – The version string of the software, empty if the software can not be loaded.

Notes

As a side effect, the detected version string is stored for later reuse. Thus subsequent tests will not try to re-detect.

classmethod detect_all()[source]

Detect all registered softwares.

Usually, the detection is only performed when needed. Calling this method will perform the detection for all registered softwares.

classmethod print_all()[source]

Print the list of registered softwares

classmethod detected(all=False)[source]

Return the successfully detected softwares and their version

Returns:OrderedDict – A dict with the software name as key and the detected version as value.
classmethod has(name, check=False, fatal=False, quiet=False)[source]

Test if we have the named software available.

Returns a nonzero (version) string if the software is available, or an empty string if it is not.

By default, the software is only checked on the first call. The optional argument check==True forces a new detection.

classmethod check(name, version)[source]

Check that we have a required version of a software.

classmethod require(name, version=None)[source]

Ensure that the named Python software/version is available.

Checks that the specified software is available, and that its version number is not lower than the specified version. If no version is specified, any version is ok.

Returns if the required software/version could be loaded, else an error is raised.

class software.Module(name, modname=None, attr=None)[source]

Register for Python module version detection rules.

This class holds a register of version detection rules for installed Python modules. Each instance holds the rule for one module, and it is automatically registered at instantiation. The modules used by pyFormex are declared in this module, but users can add their own just by creating a Module instance.

Parameters:
  • name (str) – The module name as known in pyFormex: this is often the same as the Python module name, but can be different if the Python module name is complex. We try to use simple lower case names in pyFormex.
  • modname (str, optional) – The correct Python package.module name. If not provided, it is equal to the pyFormex name.
  • attr (str or tuple of str, optional) – If a str, it is the name of the attribute holding the module version. This should be an attribute of the module modname. The default is ‘__version__’, as it is used by many projects. If the version is not stored in a direct attribute of the same module as used for the detection, then a tuple of strings can be specified, starting with the Python module name in which the version attribute is stored, and a list of subsequent attribute names leading to the version. In this case the first element of the tuple is always a module name. If it is the same as modname, an empty string may be specified. If the final attribute is a callable, it will be called to get the version. The result is always converted to str before being stored as the version.

Examples

>>> Module.register.clear()
>>> Module.detect_all()
>>> Module.print_all()
>>> np = Module('numpy')
>>> pil = Module('pil', modname='PIL', attr='VERSION')
>>> Module.print_all()
  numpy (** Not Found **)
  pil (** Not Found **)
>>> Module.has('numpy')
'1.16.2'
>>> Module.print_all()
  numpy (1.16.2)
  pil (** Not Found **)
>>> Module.has('foo')
Traceback (most recent call last):
ValueError: foo is not a registered Module
>>> Module.require('foo')
Traceback (most recent call last):
ValueError: foo is not a registered Module
>>> foo = Module('foo','FooBar')
>>> Module.has('foo')
''
>>> Module.require('foo')
Traceback (most recent call last):
ValueError: Required Module 'foo' (FooBar) not found

Now fake a detection of Module ‘foo’

>>> Module.register['foo'].version = '1.2.3'
>>> Module.has('foo')
'1.2.3'
>>> Module.require('foo')
>>> Module.require('foo','>= 1.1.7')
>>> Module.require('foo','>= 1.3.0')
Traceback (most recent call last):
ValueError: Required version of Module 'foo' (FooBar) not found
detect(fatal=False, quiet=False)[source]

Detect the version of the module.

Parameters:
  • fatal (bool, optional) – If True and the module can not be loaded, a fatal exception is raised. Default is to silently ignore the problem and return an empty version string.
  • quiet (bool, optional) – If True, the whole operation is done silently. The only information about failure will be the returned empty version string.
Returns:

str – The version string of the module, empty if the module can not be loaded.

Notes

As a side effect, the detected version string is stored for later reuse. Thus subsequent tests will not try to re-detect.

class software.External(name, command, regex)[source]

Register for external application version detection rules.

This class holds a register of version detection rules for installed external applications. Each instance holds the rule for one application, and it is automatically registered at instantiation. The applications used by pyFormex are declared in this module, but users can add their own just by creating an External instance.

Parameters:
  • name (str) – The application name as known in pyFormex: this is often the same as the executable name, but can be different if the executable name is complex. We try to use simple lower case names in pyFormex.
  • command (str) – The command to run the application. Usually this includes an option to make the application just report its version and then exit. The command should be directly executable as-is, without invoking a new shell. If a shell is required, it should be made part of the command (see e.g. tetgen). Do not use commands that take a long time to load and run.
  • regex (r-string) – A regular expression that extracts the version from the output of the command. If the application does not have or report a version, any non-empty string is accepted as a positive detection (for example the executable’s name in a bin path). The regex string should contain one set of grouping parentheses, delimiting the part of the output that will be stored as version. If the output of the command does not match, an empty string is stored.

Examples

>>> External.register.clear()
>>> External.detect_all()
>>> External.print_all()
detect(fatal=False, quiet=False)[source]

Detect the version of the external.

Parameters:
  • fatal (bool, optional) – If True and the external can not be run, a fatal exception is raised. Default is to silently ignore the problem and return an empty version string.
  • quiet (bool, optional) – If True, the whole operation is done silently. The only information about failure will be the returned empty version string.
Returns:

str – The version string of the external, empty if the external can not be run.

Notes

As a side effect, the detected version string is stored for later reuse. Thus subsequent tests will not try to re-detect.

26.2. Functions defined in module software

software.Shaders()[source]

Return a list of the available GPU shader programs.

Shader programs are in the pyformex/glsl directory and consist at least of two files: ‘vertex_shader_SHADER.c’ and ‘fragment_shader_SHADER.c’. This function will return a list of all the SHADER filename parts currently available. The default shader programs do not have the ‘_SHADER’ part and will not be contained in this list.

software.detectedSoftware(all=True)[source]

Return a dict with all detected helper software

software.formatDict(d, indent=4)[source]

Format a dict in nicely formatted Python source representation.

Each (key, alue) pair is formatted on a line of the form:

key = value

If all the keys are strings containing only characters that are allowed in Python variable names, the resulting text is a legal Python script to define the items in the dict. It can be stored on a file and executed.

This format is the storage format of the Config class.

software.compareVersion(has, want)[source]

Check whether a detected version matches the requirements.

has is the version string detected. want is the required version string, possibly preceded by one of the doubly underscored comparison operators: __gt__, etc. If no comparison operator is specified, ‘__eq__’ is assumed.

Note that any tail behind x.y.z version is considered to be later version than x.y.z.

Returns the result of the comparison: True or False .. rubric:: Examples

>>> compareVersion('2.7','2.4.3')
False
>>> compareVersion('2.7','>2.4.3')
True
>>> compareVersion('2.7','>= 2.4.3')
True
>>> compareVersion('2.7','>= 2.7-rc3')
False
>>> compareVersion('2.7-rc4','>= 2.7-rc3')
True
software.checkDict(has, want)[source]

Check that software dict has has the versions required in want

software.checkSoftware(req, report=False)[source]

Check that we have the matching components

Returns True or False. If report=True, also returns a string with a full report.

software.registerSoftware(req)[source]

Register the current values of required software

software.soft2config(soft)[source]

Convert software collection to config

software.config2soft(conf)[source]

Convert software collection from config

software.storeSoftware(soft, fn, mode='pretty')[source]

Store the software collection on file.

software.readSoftware(fn, mode='python')[source]

Read the software collection from file.

  • mode = ‘pretty’: readable, editable
  • mode = ‘python’: readable, editable
  • mode = ‘config’: readable, editable
  • mode = ‘pickle’: binary