[elbe-devel] [PATCH v3 1/4] filesystem: implement ImgMountFilesystem context manager

Torben Hohn torben.hohn at linutronix.de
Tue Jan 8 11:56:48 CET 2019


python context manager allow to make sure that umount is called.

Implement a class deriving from Filesystem that allows to
mount a device or image using a ContextManager.

Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
 elbepack/filesystem.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/elbepack/filesystem.py b/elbepack/filesystem.py
index 36e006e6..6ced9ddb 100644
--- a/elbepack/filesystem.py
+++ b/elbepack/filesystem.py
@@ -254,5 +254,22 @@ class TmpdirFilesystem (Filesystem):
             shutil.rmtree(self.path, True)
 
 
+class ImgMountFilesystem(Filesystem):
+    def __init__(self, mntpoint, dev, log):
+        Filesystem.__init__(self, mntpoint)
+
+        self.dev = dev
+        self.log = log
+
+    def __enter__(self):
+        cmd = 'mount "%s" "%s"' % (self.dev, self.path)
+        self.log.do(cmd)
+
+        return self
+
+    def __exit__(self, typ, value, traceback):
+        self.log.do('umount "%s"' % self.path)
+
+
 hostfs = Filesystem('/')
 wdfs = Filesystem(os.getcwd())
-- 
2.11.0




More information about the elbe-devel mailing list