From 62098f90411ca6775d3d7d70a8a641ce35dc9f84 Mon Sep 17 00:00:00 2001 From: Ross Burton Date: Tue, 10 Aug 2021 17:55:06 +0100 Subject: 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 Signed-off-by: Richard Purdie --- bitbake/lib/bb/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'bitbake/lib/bb/utils.py') 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): shutil.move(src, dst) else: raise err + +@contextmanager +def environment(**envvars): + """ + Context manager to selectively update the environment with the specified mapping. + """ + backup = dict(os.environ) + try: + os.environ.update(envvars) + yield + finally: + for var in envvars: + if var in backup: + os.environ[var] = backup[var] + else: + del os.environ[var] -- cgit v1.2.3-54-g00ecf