diff options
author | Ross Burton <ross@burtonini.com> | 2021-08-10 17:55:06 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2021-08-12 06:28:01 +0100 |
commit | 62098f90411ca6775d3d7d70a8a641ce35dc9f84 (patch) | |
tree | a919513115e8c9c5d26abef520769ee303c6c164 /bitbake/lib/bb/utils.py | |
parent | 0975ff9b69b9ee86d6554f96b391a0186761a3cc (diff) | |
download | poky-62098f90411ca6775d3d7d70a8a641ce35dc9f84.tar.gz |
bitbake: utils: add environment updating context manager
bb.utils.environment() is a context manager to alter os.environ inside
a specific block, restoring it after the block is closed.
(Bitbake rev: 9974848f67581ff7d76cef52a94f505af99b4932)
Signed-off-by: Ross Burton <ross.burton@arm.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/utils.py')
-rw-r--r-- | bitbake/lib/bb/utils.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index e6e82d1118..70634910f7 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -1681,3 +1681,19 @@ def rename(src, dst): | |||
1681 | shutil.move(src, dst) | 1681 | shutil.move(src, dst) |
1682 | else: | 1682 | else: |
1683 | raise err | 1683 | raise err |
1684 | |||
1685 | @contextmanager | ||
1686 | def environment(**envvars): | ||
1687 | """ | ||
1688 | Context manager to selectively update the environment with the specified mapping. | ||
1689 | """ | ||
1690 | backup = dict(os.environ) | ||
1691 | try: | ||
1692 | os.environ.update(envvars) | ||
1693 | yield | ||
1694 | finally: | ||
1695 | for var in envvars: | ||
1696 | if var in backup: | ||
1697 | os.environ[var] = backup[var] | ||
1698 | else: | ||
1699 | del os.environ[var] | ||