Skip to content

Commit a0497db

Browse files
committed
Added override and docstrings
1 parent 5562dcc commit a0497db

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

easybuild/tools/environment.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,46 @@ def sanitize_env():
225225

226226

227227
@contextlib.contextmanager
228-
def wrap_path_env(prepend=None, append=None, sep=os.pathsep, strict=False):
229-
"""This function is a context manager that temporarily modifies path-like environment variables.
230-
It will prepend and append the values of the given dictionaries to the current environment and restore the
228+
def wrap_path_env(override=None, prepend=None, append=None, sep=os.pathsep, strict=False):
229+
"""This function is a context manager that temporarily modifies environment variables.
230+
It will override or prepend/append the values of the given dictionaries to the current environment and restore the
231231
original environment when the context is exited.
232232
233-
A custom separator can be specified for each variable by passing a dictionary of strings with the same keys as
234-
prepend and append.
233+
For path-like variables, a custom separator can be specified for each variable by passing a dictionary of strings
234+
with the same keys as prepend and append.
235235
If a key is not present in the sep dictionary, os.pathsep will be used unless strict is True, then an error
236236
will be raised.
237+
238+
Args:
239+
override: A dictionary of environment variables to override.
240+
prepend: A dictionary of environment variables to prepend to.
241+
append: A dictionary of environment variables to append to.
242+
sep: A string or a dictionary of strings to use as separator for each variable.
243+
strict: If True, raise an error if a key is not present in the sep dictionary.
237244
"""
238245
if prepend is None:
239246
prepend = {}
240247
if append is None:
241248
append = {}
249+
if override is None:
250+
override = {}
251+
252+
path_keys = set(prepend.keys()) | set(append.keys())
253+
over_keys = set(override.keys())
254+
255+
duplicates = path_keys & over_keys
256+
if duplicates:
257+
raise EasyBuildError(
258+
"The keys in override must not overlap with the keys in prepend or append: '%s'",
259+
" ".join(duplicates)
260+
)
261+
242262
orig = {}
243-
for key in set(prepend.keys()) | set(append.keys()):
263+
for key in over_keys
264+
orig[key] = os.environ.get(key)
265+
setvar(key, override[key])
266+
267+
for key in path_keys:
244268
if isinstance(sep, dict):
245269
if key not in sep:
246270
if strict:

0 commit comments

Comments
 (0)