39. software — Detecting and checking installed software

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

39.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:

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
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 (1...)
  pil (** Not Found **)
>>> SaneVersion(Module.has('numpy')) >= SaneVersion('1.16')
True
>>> Module.print_all()
  numpy (1...)
  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 (>= 1.3.0) of Module 'foo' (FooBar) not found
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()

39.2. Functions defined in module software

software.CompareVersion(a, cmp, b)[source]

Compare versions a and b with comparator cmp

Comparisons are done after converting the values with distutils.version.LooseVersion

Parameters
  • a (str) – The two version strings to compare. Version strings can be something like ‘3.1.7a5’.

  • b (str) – The two version strings to compare. Version strings can be something like ‘3.1.7a5’.

  • cmp (str) – The comparator to be used. For example: ‘>=’.

Returns

bool – The result of a cmp b.

Examples

>>> CompareVersion('3.2.7a5', '>=', '3.2.7')
True
>>> CompareVersion('3.2.7a5', '<', '3.2.8')
True
>>> CompareVersion('3.3', '<', '3.30')
True
software.listLibraries()[source]

Return a list with the acceleration libraries

software.listShaders()[source]

Return a list with the available GPU shader programs.

Returns

list – A list of the shader versions available.

Notes

Shader programs are stored in the pyformex/glsl directory and consist of at least of two files: ‘vertex_shader_SHADER.c’ and ‘fragment_shader_SHADER.c’. The SHADER part is the version mnemomic which can be used in the ‘–shader SHADER’ option of the pyformex command.

software.detectedSoftware(all=True)[source]

Return a dict with all detected helper software

pyFormex makes use of a lot of external helper software. This gives an overview of the detected useful software and their version (where possible).

Returns

dict – A dict with the keys ‘System’, ‘Modules’ and ‘Externals’, each having a dict as value:

  • ’System’: contains information about system, pyFormex, Python

  • ’Modules’: the detected Python modules

  • ’Externals’: the detected external programs

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.formatDict(d, indent=2, sort_keys=False, mode='json')[source]

Format a possibly nested dict

>>> d = {'a':0, 'd':{'y':'s', 'x':'t'}, 'b':1}
>>> print(formatDict(d))
{
  "a": 0,
  "d": {
    "y": "s",
    "x": "t"
  },
  "b": 1
}
>>> print(formatDict(d, mode='python'))
{'a': 0, 'd': {'y': 's', 'x': 't'}, 'b': 1}
software.storeSoftware(soft, fn, mode='json', indent=2)[source]

Store the software collection on file.

The collection is by default stored in JSON format.

software.readSoftware(fn)[source]

Read the software collection from file.

Default mode is ‘python’ because it reads json as well as python. ‘legacy’ can be used to read old software files, though it is recommended to change the files by removing the ‘soft = ‘ at the start.