generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use env variable of subproces.run instead of patching os.envir…
…on directly Signed-off-by: Trong Nhan Mai <[email protected]>
- Loading branch information
Showing
4 changed files
with
38 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,45 @@ | ||
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
||
"""Context managers related to environment variables.""" | ||
"""Helper functions related to environment variables.""" | ||
|
||
import contextlib | ||
import os | ||
from collections.abc import Generator, Mapping | ||
from collections.abc import Mapping, MutableMapping | ||
|
||
|
||
@contextlib.contextmanager | ||
def patched_env( | ||
def get_patched_env( | ||
patch: Mapping[str, str | None], | ||
_env: dict[str, str] | None = None, | ||
) -> Generator[None, None, None]: | ||
"""Create a context in which ``os.environ`` is temporarily updated according to ``patch``. | ||
) -> MutableMapping[str, str]: | ||
"""Return a mapping whose elements copied from ``os.environ`` and are updated according to ``patch``. | ||
Out of the context, ``os.environ`` retains its original state. | ||
This function does not modify ``os.environ``. | ||
Parameters | ||
---------- | ||
patch : Mapping[str, str | None] | ||
A mapping (immutable) in which: | ||
- each key is an environment variable | ||
- each key is an environment variable. | ||
- each value is the value to set to the corresponding environment variable. | ||
If value is ``None``, the environment variable is "unset". | ||
_env : dict[str, str] | None | ||
The environment being updated. | ||
This is ``None`` by default, in which case ``os.environ`` is being updated. | ||
Returns | ||
------- | ||
MutableMapping[str, str] | ||
The patched mutable mapping which will not reflect any changes on it to ``os.environ``. | ||
""" | ||
env = os.environ if _env is None else _env | ||
|
||
# Make a copy of the environment before updating. | ||
before = dict(env) | ||
# Make a copy of the environment. | ||
copied_env = dict(env) | ||
|
||
for var, value in patch.items(): | ||
if value is None: | ||
env.pop(var, None) | ||
copied_env.pop(var, None) | ||
else: | ||
env[var] = value | ||
|
||
yield | ||
copied_env[var] = value | ||
|
||
# Restore the environment to the original state. | ||
env.clear() | ||
env.update(before) | ||
return copied_env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters