Since v3.5.0, EasyBuild supports hooks that can be used to customise the behaviour of EasyBuild according to site policies if needed, without having to change the EasyBuild framework or the existing easyblocks.
What are hooks?¶
Hooks are user-defined functions that are called by the EasyBuild framework at specific times during the installation procedure. They can be leveraged to alter or augment the installation procedure.
This is usually done to conform with site-specific policies that are difficult to enforce otherwise, but it can also be (ab)used to fix specific problems or inject self-implemented enhancements (before you flesh them out in a proper contribution, for example).
EasyBlock instance and the parsed easyconfig file that are being used
are fully accessible (and modifiable) from within hook implementations. Hence, this mechanism
provides a lot of flexibility to change the EasyBuild functionality should you require it,
without having to modify the codebase of EasyBuild itself.
Configuring EasyBuild to use your hook implementations¶
To instruct EasyBuild to use your hook implementations,
you only need to specify the location of the Python module (
*.py) that implements them.
This is done via the
--hooks configuration option
(or equivalently via the
$EASYBUILD_HOOKS environment variable, or via
hooks = ...
in an EasyBuild configuration file, see also Configuring EasyBuild).
eb --hooks=$HOME/my_eb_hooks.py ...
export EASYBUILD_HOOKS=$HOME/my_eb_hooks.py eb ...
Currently (since EasyBuild v3.7.0), three types of hooks are supported:
end_hook, which are triggered once before starting software installations, and once right after completing all installations, respectively
parse_hook, which is triggered when an easyconfig file is being parsed
module_write_hook, which is triggered right before a module file is written. This includes the temporary module file used when installing extensions and during the sanity check, as well as the devel module.
- "step" hooks that are triggered before and after every step of each installation procedure that is performed,
also aptly named '
pre'- and '
The list of currently available hooks in order of execution,
which can also be consulted using
eb --avail-hooks, is:
start_hook(only called once in an EasyBuild session)
parse_hook(available since EasyBuild v3.7.0)
end_hook(only called once in an EasyBuild session)
module_write_hook(called multiple times per installation, available since EasyBuild v4.4.1)
All functions implemented in the provided Python module for which the name ends with
_hook are considered.
*_hook functions are encountered that do not match any of the available hooks, an error is reported.
EasyBuild will try to provide suggestions for available hooks that closely match the encountered unknown hook.
$ eb --hooks wrong_hooks.py example.eb == temporary log file in case of crash /tmp/eb-nMawy1/easybuild-Gu2ZP6.log ERROR: Found one or more unknown hooks: * stat_hook (did you mean 'start_hook'?) * this_is_not_a_hook * install_hook (did you mean 'pre_install_hook', or 'post_install_hook'?) Run 'eb --avail-hooks' to get an overview of known hooks
To implement hooks, simply define one or more functions in a Python module (
each named after an available hook.
Do take into account the following:
end_hook, no arguments are provided
parse_hook, one argument is provided: the
EasyConfiginstance that corresponds to the easyconfig file being parsed (usually referred to as
module_write_hook, 3 arguments are provided:
EasyBlockinstance used to perform the installation (usually referred to as
- the filepath of the module that will be written
- the module text as a string The return value of this hook, when set, will replace the original text that is then written to the module file.
- for the step hooks, one argument is provided:
EasyBlockinstance used to perform the installation (usually referred to as
- the parsed easyconfig file can be accessed in the step hooks via the
EasyBlockinstance, i.e., via
It is recommended to anticipate possible changes in the provided (named) arguments,
**kwargs mechanism commonly used in Python. This
avoids that your hook implementations may break when updating to future EasyBuild versions. For example:
# example pre-configure hook that anticipates changes in provided arguments def pre_configure_hook(self, *args, **kwargs): ...
In hooks you have access to the full functionality provided by the EasyBuild framework,
easybuild.tools.* (or other
easybuild.* namespaces) to leverage
Parse hook specifics¶
parse_hook is triggered right after reading the easyconfig file,
before further parsing of some easyconfig parameters (like
custom data structures is done.
This is important since it allows to dynamically modify easyconfig files while they are still "raw", i.e. when the easyconfig parameter values are still basic Python data structures like lists, dictionaries, etc. that are easy to manipulate (see also Manipulating easyconfig parameters).
parse_hook easyconfig parameters can be accessed and/or modified in a straightforward way,
see Example hook to inject a custom patch file.
Due to internal details of the EasyBuild framework, you may run into some surprises when implementing hooks. Here are some things to take into account:
Resolving of template values¶
In all step hooks, template values in easyconfig parameters will be resolved whenever they are accessed.
That is, if the
%(version) template is used in for example the
sources easyconfig parameter,
it will be replaced with the actual value of the
version easyconfig parameter whenever the
sources value is used.
This can be avoided by temporarily disabling templating by wrapping the code in
There is one notable exception to this:
Templates in easyconfig parameters are not resolved in
because templating has been disabled explicitly before
parse_hook is called;
this helps significantly to simplify manipulating of easyconfig parameter values
(see also Manipulating easyconfig parameters).
Manipulating easyconfig parameters¶
You may run into surprises when trying to manipulate easyconfig parameters, for various reasons.
First of all, the original easyconfig parameters may already be processed in another data structure which does not resemble the original format in which the parameter was defined in the easyconfig file.
Moreover, this processing could be done either "in place" by replacing the original easyconfig parameter value, or in a separate variable, which effectively means that any changes to the original easyconfig parameter value are simply ignored.
In addition, because of how the templating mechanism for easyconfig parameter works, changes to easyconfig parameters with non-string values (i.e. lists, dictionaries, etc.) will go up in smoke if not done correctly.
More specifically, the following approach will not work in any of the (step) hooks, except for
def pre_fetch_hook(self): "Example of pre-fetch hook to manipulate list of patches." # this does NOT have the intended affect in any pre- or post-step hook self.cfg['patches'].append('example.patch')
The problem here is that the value obtained via
self.cfg['patches'] is not a reference
to the actual easyconfig parameter value but a reference to a temporary copy thereof;
hence any updates on the copy are effectively lost immediately.
To achieve the intended effect, you can either:
temporarily disable the templating mechanism:
def pre_fetch_hook(self): "Example of pre-fetch hook to manipulate list of patches." # temporarily disable templating, so changes to 'patches' easyconfig parameter are picked up with self.cfg.disable_templating: # add patch self.cfg['patches'].append('example.patch') # templating state restored
or replace the original value entirely:
def pre_fetch_hook(self): "Example of pre-fetch hook to manipulate list of patches." self.cfg['patches'] = self.cfg['patches'] + ['example.patch']
A better approach for manipulating easyconfig parameters is to use the
was introduced in EasyBuild v3.7.0 (see Parse hook specifics),
where these kind of surprises will not occur (because templating is automatically disabled
parse_hook is called and restored immediately afterwards).
See also Example hook to inject a custom patch file.
Archived easyconfig file vs hooks¶
EasyBuild archives the easyconfig file that was used for a particular installation:
A copy is stored both in the
easybuild subdirectory of the software installation
directory and in the easyconfigs repository (see Easyconfigs repository).
If any changes were made to the easyconfig file via hooks, these changes will not be reflected in these copies. The assumption here is that the hooks will also be in place for future (re-)installations.
EasyBuild does however store an additional copy of the easyconfig file which includes any modifications that were done dynamically, for example by hooks. If subtoolchains were used to resolve dependencies, they will also be hardwired in this copy.
This "reproducible easyconfig" is stored in the
of the software installation directory.
Examples of hook implementations¶
--without-verbs in OpenMPI configure options¶
def pre_configure_hook(self, *args, **kwargs): """Example pre-configure hook to replace --with-verbs with --without -verbs for OpenMPI.""" if self.name == 'OpenMPI' and '--with-verbs' in self.cfg['configopts']: self.log.info("[pre-configure hook] Replacing --with-verbs with --without-verbs") self.cfg['configopts'] = self.cfg['configopts'].replace('--with-verbs', '--without-verbs')
Inject a custom patch file¶
def parse_hook(ec, *args, **kwargs): """Example parse hook to inject a patch file for a fictive software package named 'Example'.""" if ec.name == 'Example': patch_file = 'example.patch' ec.log.info("[parse hook] Injecting additional patch file '%s'", patch_file) ec['patches'].append(patch_file)
EBPYTHONPREFIXES in (Lua) modules¶
def module_write_hook(self, filepath, module_txt, *args, **kwargs): # note: if `self.mod_filepath == filepath` => final module file if 'Python' in (dep['name'] for dep in self.cfg.dependencies()): search = r'prepend_path\("PYTHONPATH", pathJoin\(root, "lib/python\d.\d/site-packages"\)\)' replace = 'prepend_path("EBPYTHONPREFIXES", root)' return re.sub(search, replace, module_txt)