Browse Source

added class FuseMount

Fabian Peter Hammerle 5 years ago
parent
commit
f5539da90c
1 changed files with 25 additions and 0 deletions
  1. 25 0
      rc.xsh

+ 25 - 0
rc.xsh

@@ -139,6 +139,31 @@ class DockerImage:
         sys.stderr.write('{}\n'.format(shlex_join(params)))
         subprocess.run(params, check=True)
 
+class FuseMount:
+
+    def __init__(self, cmd, path, cd=False):
+        self._mount_arg_patterns = cmd
+        self._mount_path = path
+        self._cd = cd
+
+    def __enter__(self):
+        import shlex
+        mount_args = [a.format(mp=shlex.quote(self._mount_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
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        umount_args = ['fusermount', '-u', '-z', self._mount_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 StdoutTee:
 
     def __init__(self, sink):