[elbe-devel] [PATCH 1/5] packers: add Packer class implementation

Torben Hohn torben.hohn at linutronix.de
Mon Jan 28 10:01:12 CET 2019


using tar as a packer can not be modelled using the tuples
approach.

Implement a Baseclass and a dict mapping packernames to
Packer objects. Also implement NoPacker class, which does nothing
and returns the fname right away.

Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
 elbepack/packers.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 elbepack/packers.py

diff --git a/elbepack/packers.py b/elbepack/packers.py
new file mode 100644
index 00000000..20680bc4
--- /dev/null
+++ b/elbepack/packers.py
@@ -0,0 +1,77 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (c) 2019 Torben Hohn <torben.hohn at linutronix.de>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+import os
+from elbepack.shellhelper import CommandError
+
+
+class Packer(object):
+    # pylint: disable=too-few-public-methods
+    def pack_file(self, _log, _builddir, _fname):
+        raise NotImplementedError('abstract method called')
+
+
+class NoPacker(Packer):
+    # pylint: disable=too-few-public-methods
+    def pack_file(self, _log, _builddir, fname):
+        return fname
+
+
+class InPlacePacker(Packer):
+    # pylint: disable=too-few-public-methods
+    def __init__(self, cmd, suffix):
+
+        self.cmd = cmd
+        self.suffix = suffix
+
+    def pack_file(self, log, builddir, fname):
+        try:
+            fpath = os.path.join(builddir, fname)
+            log.do('%s "%s"' % (self.cmd, fpath))
+        except CommandError:
+            # in case of an error, we just return None
+            # which means, that the orig file does not
+            # exist anymore
+            return None
+
+        return fname + self.suffix
+
+
+class TarArchiver(Packer):
+    # pylint: disable=too-few-public-methods
+    def __init__(self, flag, suffix):
+        self.flag = flag
+        self.suffix = suffix
+
+    def pack_file(self, log, builddir, fname):
+        try:
+            fpath = os.path.join(builddir, fname)
+            dirname = os.path.dirname(fpath)
+            basename = os.path.basename(fpath)
+            archname = fpath + self.suffix
+            log.do('tar cv%sf "%s" --sparse -C "%s" "%s"' % (self.flag,
+                                                             archname,
+                                                             dirname,
+                                                             basename))
+        except CommandError:
+            # in case of an error, we just return None
+            # which means, that the orig file does not
+            # exist anymore.
+            #
+            # Even if it actually exists, it might be
+            # much to big to download it and remove
+            # the sparsity.
+            return None
+
+        return fname + self.suffix
+
+
+packers = {'none': None,
+           'gzip': InPlacePacker('gzip -f', '.gz'),
+           'tar':  TarArchiver('', '.tar'),
+           'tarxz': TarArchiver('J', '.tar.xz'),
+           'targz': TarArchiver('z', '.tar.gz')}
+
+default_packer = packers['targz']
-- 
2.11.0




More information about the elbe-devel mailing list