easybuild-framework
easybuild-framework copied to clipboard
Add function to modify path-like environment variable in a context
trafficstars
The proposed function wrap_path_env would have the utility to allow running commands or functions with modified values for PATH/LD_LIBRARY_PATH/...
The function allows to both append and prepend to a path-like environment variables, using by default the default system separator. A custom separator can be specified for each variable by passing a dictionary of strings with the same keys as prepend and append. If a key is not present in the sep dictionary, os.pathsep will be used unless strict is True, then an error will be raised.
Comparison to env= parameter in run_shell_cmd for 5.0.x:
- The
envparameter on it's own can be cumbersome to use as it requires redefining the entire environment, which would entail copying the environment and modifying the environment variables to have the same effect as the proposed function. - The
envparameter would not work for other python function calls (eg testing a module that independently calls commands)
Comparison with currently available framework.environment functions:
- I do not believe the functionality introduced could be easily replicated using the currently available functions (without rewriting the same logic). It is possible to call
reset_changesto reset all variables modified bysetvarbut it is possible that the user has already set some variables and only want to temporarily modify only some of them or different ones, without resetting the originally set variables.
Possible enhancements/discussions:
- Adding a
set/overrideparameter to override the value instead of just appending/prepending- The utility i would see in this is to make sure the appended path is correct, (eg an incorrect path is appended but the dependency is resolved with the original path leading to unexpected behaviors)
- Might need checking if the key is defined in (prepend | append) at the same time as (set) and either log a warning or raise an error
- The way the function is built now, only environment variable modified by the context manager are reset to the original value. If a variable is modified within the context manager it will be reset to the original value only if it was originally modified by the context manager.
- One possible solution would be to store the entire environment before entering the context, but I am not sure if this should be implemented or if it is possible that leaving non-managed variables to the discretion of the user should be a wanted/useful behavior
- While i think it would also be useful to have the capabilities to override non path-like variables, (could be implemented together with the
set/overrideparamenter) i am not sure if this should be implemented in a separate function.