Browse Source

added class EncFSMount

Fabian Peter Hammerle 5 years ago
parent
commit
060fd2f27e
1 changed files with 20 additions and 7 deletions
  1. 20 7
      rc.xsh

+ 20 - 7
rc.xsh

@@ -141,29 +141,41 @@ class DockerImage:
 
 class FuseMount:
 
-    def __init__(self, cmd, path, cd=False):
-        self._mount_arg_patterns = cmd
-        self._mount_path = path
+    def __init__(self, mount_arg_patterns, mount_point_path, cd=False):
+        self._mount_arg_patterns = mount_arg_patterns
+        self._mount_point_path = mount_point_path
         self._cd = cd
 
     def __enter__(self):
         import shlex
-        mount_args = [a.format(mp=shlex.quote(self._mount_path))
+        mount_args = [a.format(mp=shlex.quote(self._mount_point_path))
                       for a in self._mount_arg_patterns]
         sys.stderr.write('{}\n'.format(shlex_join(mount_args)))
         subprocess.check_call(mount_args)
         if self._cd:
             self._previous_work_dir_path = os.getcwd()
-            os.chdir(self._mount_path)
-        return self._mount_path
+            os.chdir(self._mount_point_path)
+        return self._mount_point_path
 
     def __exit__(self, exc_type, exc_value, traceback):
-        umount_args = ['fusermount', '-u', '-z', self._mount_path]
+        umount_args = ['fusermount', '-u', '-z', self._mount_point_path]
         sys.stderr.write('{}\n'.format(shlex_join(umount_args)))
         subprocess.check_call(umount_args)
         if self._cd:
             os.chdir(self._previous_work_dir_path)
 
+class EncFSMount(FuseMount):
+
+    def __init__(self, root_dir_path, mount_point_path, cd=False, extpass=None):
+        mount_arg_patterns = ['encfs', root_dir_path, mount_point_path]
+        if extpass:
+            mount_arg_patterns.extend(['--extpass', shlex_join(extpass)])
+        super().__init__(
+            mount_arg_patterns=mount_arg_patterns,
+            mount_point_path=mount_point_path,
+            cd=cd,
+        )
+
 class StdoutTee:
 
     def __init__(self, sink):
@@ -245,6 +257,7 @@ def os_read_non_blocking(fd, buffer_size_bytes=8*1024, timeout_seconds=0.1):
 
 def shlex_join(params):
     import shlex
+    assert isinstance(params, list) or isinstance(params, tuple), params
     return ' '.join(shlex.quote(p) for p in params)
 
 def timestamp_now_utc():