Browse Source

utils: add umask changing context manager

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>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Ross Burton 3 years ago
parent
commit
6c601e68a2
1 changed files with 11 additions and 0 deletions
  1. 11 0
      lib/bb/utils.py

+ 11 - 0
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