@@ -225,22 +225,46 @@ def sanitize_env():
225
225
226
226
227
227
@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
231
231
original environment when the context is exited.
232
232
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.
235
235
If a key is not present in the sep dictionary, os.pathsep will be used unless strict is True, then an error
236
236
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.
237
244
"""
238
245
if prepend is None :
239
246
prepend = {}
240
247
if append is None :
241
248
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
+
242
262
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 :
244
268
if isinstance (sep , dict ):
245
269
if key not in sep :
246
270
if strict :
0 commit comments