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 | |
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>
-rw-r--r-- | bitbake/lib/bb/tests/utils.py | 18 | ||||
-rw-r--r-- | bitbake/lib/bb/utils.py | 16 |
2 files changed, 34 insertions, 0 deletions
diff --git a/bitbake/lib/bb/tests/utils.py b/bitbake/lib/bb/tests/utils.py index a7ff33db52..4d5e21b99e 100644 --- a/bitbake/lib/bb/tests/utils.py +++ b/bitbake/lib/bb/tests/utils.py | |||
@@ -666,3 +666,21 @@ class GetReferencedVars(unittest.TestCase): | |||
666 | 666 | ||
667 | layers = [{"SRC_URI"}, {"QT_GIT", "QT_MODULE", "QT_MODULE_BRANCH_PARAM", "QT_GIT_PROTOCOL"}, {"QT_GIT_PROJECT", "QT_MODULE_BRANCH", "BPN"}, {"PN", "SPECIAL_PKGSUFFIX"}] | 667 | layers = [{"SRC_URI"}, {"QT_GIT", "QT_MODULE", "QT_MODULE_BRANCH_PARAM", "QT_GIT_PROTOCOL"}, {"QT_GIT_PROJECT", "QT_MODULE_BRANCH", "BPN"}, {"PN", "SPECIAL_PKGSUFFIX"}] |
668 | self.check_referenced("${SRC_URI}", layers) | 668 | self.check_referenced("${SRC_URI}", layers) |
669 | |||
670 | |||
671 | class EnvironmentTests(unittest.TestCase): | ||
672 | def test_environment(self): | ||
673 | os.environ["A"] = "this is A" | ||
674 | self.assertIn("A", os.environ) | ||
675 | self.assertEqual(os.environ["A"], "this is A") | ||
676 | self.assertNotIn("B", os.environ) | ||
677 | |||
678 | with bb.utils.environment(B="this is B"): | ||
679 | self.assertIn("A", os.environ) | ||
680 | self.assertEqual(os.environ["A"], "this is A") | ||
681 | self.assertIn("B", os.environ) | ||
682 | self.assertEqual(os.environ["B"], "this is B") | ||
683 | |||
684 | self.assertIn("A", os.environ) | ||
685 | self.assertEqual(os.environ["A"], "this is A") | ||
686 | self.assertNotIn("B", os.environ) | ||
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] | ||