[5/7] utils: add umask changing context manager

Submitted by Ross Burton on Sept. 28, 2020, 4:18 p.m. | Patch ID: 176863

Details

Message ID 20200928161900.452970-5-ross.burton@arm.com
State New
Headers show

Commit Message

Ross Burton Sept. 28, 2020, 4:18 p.m.
Add a umask context manager which can be used to temporarily change the
umask in a 'with' block.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 bitbake/lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

Patch hide | download patch | download mbox

diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py
index 0b79f92e25..f73d31fb73 100644
--- a/bitbake/lib/bb/utils.py
+++ b/bitbake/lib/bb/utils.py
@@ -944,6 +944,17 @@  def which(path, item, direction = 0, history = False, executable=False):
         return "", hist
     return ""
 
+@contextmanager
+def umask(new_mask):
+    """
+    Context manager to set the umask to a specific mask, and restore it afterwards.
+    """
+    current_mask = os.umask(new_mask)
+    try:
+        yield
+    finally:
+        os.umask(current_mask)
+
 def to_boolean(string, default=None):
     if not string:
         return default