]> git.ralfj.de Git - bubblebox.git/blobdiff - bubblebox.py
extend host_access helper to also support non-id bind mounts
[bubblebox.git] / bubblebox.py
index d7c6cb0b326d4f73212573a91bb06061b4e40d97..5b8232258d2376f772c3f9ec0610ec640809bfae 100644 (file)
@@ -122,6 +122,13 @@ class Access:
     Write = 1
     Device = 2
 
     Write = 1
     Device = 2
 
+    def WriteTo(host_path):
+        '''Bind-mount a particular host path here (writable)'''
+        access = Access()
+        access.flag = "--bind"
+        access.host_path = host_path
+        return access
+
     def flag(val):
         if val == Access.Read:
             return "--ro-bind"
     def flag(val):
         if val == Access.Read:
             return "--ro-bind"
@@ -131,6 +138,7 @@ class Access:
             return "--dev-bind"
         else:
             raise Exception(f"invalid access value: {val}")
             return "--dev-bind"
         else:
             raise Exception(f"invalid access value: {val}")
+
 def host_access(dirs):
     def expand(root, names):
         """`names` is one or more strings that can contain globs. Expand them all relative to `root`."""
 def host_access(dirs):
     def expand(root, names):
         """`names` is one or more strings that can contain globs. Expand them all relative to `root`."""
@@ -144,7 +152,10 @@ def host_access(dirs):
             path = path.replace("//", "/")
             path = path.removesuffix("/.")
             # glob expansion
             path = path.replace("//", "/")
             path = path.removesuffix("/.")
             # glob expansion
-            yield from glob.glob(path)
+            globbed = glob.glob(path)
+            if len(globbed) == 0:
+                raise Exception(f"Path does not exist: {path}")
+            yield from globbed
     def recursive_host_access(root, dirs, out):
         for names, desc in dirs.items():
             for path in expand(root, names):
     def recursive_host_access(root, dirs, out):
         for names, desc in dirs.items():
             for path in expand(root, names):
@@ -152,8 +163,11 @@ def host_access(dirs):
                     # Recurse into children
                     recursive_host_access(path, desc, out)
                 else:
                     # Recurse into children
                     recursive_host_access(path, desc, out)
                 else:
+                    assert isinstance(desc, Access) or isinstance(desc, int), f"unexpected access object: {desc}"
                     # Allow access to this path
                     # Allow access to this path
-                    out.extend((Access.flag(desc), path, path))
+                    host_path = desc.host_path if isinstance(desc, Access) else path
+                    flag = desc.flag if isinstance(desc, Access) else Access.flag(desc)
+                    out.extend((flag, path, host_path))
     # Start the recursive traversal
     out = []
     recursive_host_access("", dirs, out)
     # Start the recursive traversal
     out = []
     recursive_host_access("", dirs, out)
@@ -162,5 +176,8 @@ def host_access(dirs):
 def home_access(dirs):
     return host_access({ HOME: dirs })
 
 def home_access(dirs):
     return host_access({ HOME: dirs })
 
+def home_symlink(dest, link):
+    return bwrap_flags("--symlink", HOME + dest, HOME + link)
+
 # Profile the profiles when importing bubblebox.
 import profiles
 # Profile the profiles when importing bubblebox.
 import profiles