PathManager

(class from pyomo.common.fileutils)

class pyomo.common.fileutils.PathManager(finder, dataClass)[source]

Bases: object

The PathManager defines a registry class for path locations

The PathManager defines a class very similar to the CachedFactory class; however it does not register type constructors. Instead, it registers instances of PathData (or ExecutableData). These contain the resolved path to the directory object under which the PathData object was registered. We do not use the PyUtilib register_executable and registered_executable functions so that we can automatically include Pyomo-specific locations in the search path (namely the PYOMO_CONFIG_DIR).

Users will generally interact with this class through global instances of this class (pyomo.common.Executable and pyomo.common.Library).

Users are not required or expected to register file names with the PathManager; they will be automatically registered upon first use. Generally, users interact through the path() and available() methods:

>>> from pyomo.common import Executable
>>> if Executable('demo_exec_file').available():
...     loc = Executable('demo_exec_file').path()
...     print(os.path.isfile(loc))
True
>>> print(os.access(loc, os.X_OK))
True

For convenience, available() and path() are available by casting the PathData object requrned from Executable or Library to either a bool or str:

>>> if Executable('demo_exec_file'):
...     cmd = "%s --help" % Executable('demo_exec_file')

The PathManager caches the location / existence of the target directory entry. If something in the environment changes (e.g., the PATH) or the file is created or removed after the first time a client queried the location or availability, the PathManager will return incorrect information. You can cause the PathManager to refresh its cache by calling rehash() on either the PathData (for the single file) or the PathManager to refresh the cache for all files:

>>> # refresh the cache for a single file
>>> Executable('demo_exec_file').rehash()
>>> # or all registered files
>>> Executable.rehash()

The Executable singleton looks for executables in the system PATH and in the list of directories specified by the pathlist attribute. Executable.pathlist defaults to a list containing the os.path.join(pyomo.common.envvar.PYOMO_CONFIG_DIR, 'bin').

The Library singleton looks for executables in the system LD_LIBRARY_PATH, PATH and in the list of directories specified by the pathlist attribute. Library.pathlist defaults to a list containing the os.path.join(pyomo.common.envvar.PYOMO_CONFIG_DIR, 'lib').

Users may also override the normal file resolution by explicitly setting the location using set_path():

>>> Executable('demo_exec_file').set_path(os.path.join(
...     pyomo.common.envvar.PYOMO_CONFIG_DIR, 'bin', 'demo_exec_file'))

Explicitly setting the path is an absolute operation and will set the location whether or not that location points to an actual file. Additionally, the explicit location will persist through calls to rehash(). If you wish to remove the explicit executable location, call set_path(None):

>>> Executable('demo_exec_file').set_path(None)

The Executable singleton uses ExecutableData, an extended form of the PathData class, which provides the executable property as an alias for path() and set_path():

>>> loc = Executable('demo_exec_file').executable
>>> print(os.path.isfile(loc))
True
>>> Executable('demo_exec_file').executable = os.path.join(
...     pyomo.common.envvar.PYOMO_CONFIG_DIR, 'bin', 'demo_exec_file')
>>> Executable('demo_exec_file').executable = None
__init__(finder, dataClass)[source]

Methods

__init__(finder, dataClass)

rehash()

Requery the location of all registered executables

Member Documentation

rehash()[source]

Requery the location of all registered executables

This method derives its name from the csh command of the same name, which rebuilds the hash table of executables reachable through the PATH.