All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/4] utils: add environment updating context manager
@ 2021-08-10 16:55 Ross Burton
  2021-08-10 16:55 ` [PATCH v3 2/4] fetch2: expose environment variable names that need to be exported Ross Burton
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Ross Burton @ 2021-08-10 16:55 UTC (permalink / raw)
  To: bitbake-devel

bb.utils.environment() is a context manager to alter os.environ inside
a specific block, restoring it after the block is closed.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 bitbake/lib/bb/tests/utils.py | 18 ++++++++++++++++++
 bitbake/lib/bb/utils.py       | 16 ++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/bitbake/lib/bb/tests/utils.py b/bitbake/lib/bb/tests/utils.py
index a7ff33db527..4d5e21b99e1 100644
--- a/bitbake/lib/bb/tests/utils.py
+++ b/bitbake/lib/bb/tests/utils.py
@@ -666,3 +666,21 @@ class GetReferencedVars(unittest.TestCase):
 
         layers = [{"SRC_URI"}, {"QT_GIT", "QT_MODULE", "QT_MODULE_BRANCH_PARAM", "QT_GIT_PROTOCOL"}, {"QT_GIT_PROJECT", "QT_MODULE_BRANCH", "BPN"}, {"PN", "SPECIAL_PKGSUFFIX"}]
         self.check_referenced("${SRC_URI}", layers)
+
+
+class EnvironmentTests(unittest.TestCase):
+    def test_environment(self):
+        os.environ["A"] = "this is A"
+        self.assertIn("A", os.environ)
+        self.assertEqual(os.environ["A"], "this is A")
+        self.assertNotIn("B", os.environ)
+
+        with bb.utils.environment(B="this is B"):
+            self.assertIn("A", os.environ)
+            self.assertEqual(os.environ["A"], "this is A")
+            self.assertIn("B", os.environ)
+            self.assertEqual(os.environ["B"], "this is B")
+
+        self.assertIn("A", os.environ)
+        self.assertEqual(os.environ["A"], "this is A")
+        self.assertNotIn("B", os.environ)
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py
index e6e82d11182..70634910f7b 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]
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2021-08-18 10:07 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-10 16:55 [PATCH v3 1/4] utils: add environment updating context manager Ross Burton
2021-08-10 16:55 ` [PATCH v3 2/4] fetch2: expose environment variable names that need to be exported Ross Burton
2021-08-10 16:55 ` [PATCH v3 3/4] fetch2/wget: ensure all variables are set when calling urllib Ross Burton
2021-08-17 17:39   ` [bitbake-devel] " Enrico Scholz
2021-08-17 17:44     ` Enrico Scholz
2021-08-17 18:45     ` Enrico Scholz
2021-08-18  9:28     ` Enrico Scholz
2021-08-18 10:07       ` Ross Burton
2021-08-10 16:55 ` [PATCH v3 4/4] fetch2/wget: fetch securely by default Ross Burton
2021-08-11 13:46   ` [bitbake-devel] " Michael Opdenacker
2021-08-11 15:39     ` Peter Kjellerstedt
2021-08-13 16:41     ` Ross Burton
2021-08-13 22:00       ` Richard Purdie

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.