[elbe-devel] [PATCH v2 17/22] filesystem: add support for reading and uncompressing gziped files

Torben Hohn torben.hohn at linutronix.de
Tue Jul 2 12:55:39 CEST 2019


Prepare to read gzipped changelog files.
Add Filesystem.open_gz() and add gzip:bool param to Filesystem.read_file()


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

diff --git a/elbepack/filesystem.py b/elbepack/filesystem.py
index 450f15248..5c766ba4e 100644
--- a/elbepack/filesystem.py
+++ b/elbepack/filesystem.py
@@ -12,6 +12,7 @@ import shutil
 from glob import glob
 from tempfile import mkdtemp
 from string import digits
+import gzip
 
 def size_to_int(size):
     if size[-1] in digits:
@@ -66,6 +67,9 @@ class Filesystem(object):
     def open(self, path, mode="r"):
         return open(self.fname(path), mode)
 
+    def open_gz(self, path, mode="r"):
+        return gzip.open(self.fname(path), mode)
+
     def isdir(self, path):
         return os.path.isdir(self.fname(path))
 
@@ -212,10 +216,16 @@ class Filesystem(object):
         if mode is not None:
             self.chmod(path, mode)
 
-    def read_file(self, path):
-        fp = self.open(path, "r")
-        retval = fp.read()
-        fp.close()
+    def read_file(self, path, gzip=False):
+        if gzip:
+            print('read gzip '+path)
+            fp = self.open_gz(path, "r")
+        else:
+            fp = self.open(path, "r")
+
+        with fp:
+            retval = fp.read()
+
         return retval
 
     def mkdir_p(self, newdir, mode=0o755):
-- 
2.11.0




More information about the elbe-devel mailing list