Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to modify path-like environment variable in a context #4681

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

Crivella
Copy link

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 env parameter 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 env parameter 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_changes to reset all variables modified by setvar but 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/override parameter 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/override paramenter) i am not sure if this should be implemented in a separate function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant