[elbe-devel] [PATCH v3 4/6] Add 'plain/raw' encodings for finetuning 'add <file>'

dion at linutronix.de dion at linutronix.de
Tue May 21 14:47:18 CEST 2019


From: Olivier Dion <dion at linutronix.de>

Even though the default encoding is plain text, we should
put the option 'encoding="plain"' if the user wants to be
more explicit.  The plain encoding will strip any
whitespaces/tabs from the XML tag.  It will also strip the
first and last line of the tag.

Examples
========

------------------------------------------------------------
<file>
	Hello World!
</file>
------------------------------------------------------------

will output as
------------------------------------------------------------
"Hello World!"
------------------------------------------------------------

and
------------------------------------------------------------
<file>
	Hello World!
Foo
</file>

will output as
------------------------------------------------------------
"Hello World!\nFoo"
------------------------------------------------------------

As for'encoding="raw"', what you see is what you get.  Thus,
nothing expected the first and last line of the tag are
stripped.

Examples
========

------------------------------------------------------------
<file>
	Hello World!
</file>
------------------------------------------------------------

will output as
------------------------------------------------------------
"\tHello World!"
------------------------------------------------------------

and
------------------------------------------------------------
<file>
	Hello World!
Foo
</file>
------------------------------------------------------------

will output as
------------------------------------------------------------
"\tHello World!\nFoo"
------------------------------------------------------------

Signed-off-by: Olivier Dion <dion at linutronix.de>

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

diff --git a/elbepack/finetuning.py b/elbepack/finetuning.py
index 7f6c4a6b..b508397b 100644
--- a/elbepack/finetuning.py
+++ b/elbepack/finetuning.py
@@ -355,20 +355,27 @@ class AddFileAction(FinetuningAction):
 
     @staticmethod
     def decode(text, encoding):
-        if encoding == "base64":
-            return base64.standard_b64decode(text)
+        if encoding == "plain":
+            msg = "\n".join([line.lstrip(" \t") for line in text.splitlines()[1:-1]])
+        elif encoding == "raw":
+            msg = "\n".join(text.splitlines()[1:-1])
+        elif encoding == "base64":
+            msg = base64.standard_b64decode(text)
         else:
             raise FinetuningException("Invalid encoding %s" % encoding)
+        return msg
 
     def execute(self, log, _buildenv, target):
 
         att = self.node.et.attrib
         dst = att["dst"]
         content = self.node.et.text
+        encoding = "plain"
         owner = None
         group = None
         mode = None
 
+        if "encoding" in att: encoding = att["encoding"]
         if "owner" in att: owner = att["owner"]
         if "group" in att: group = att["group"]
         if "mode"  in att: mode  = att["mode"]
@@ -379,8 +386,7 @@ class AddFileAction(FinetuningAction):
             if E.errno is not errno.EEXIST:
                 raise
 
-        if "encoding" in att:
-            content = AddFileAction.decode(content, att["encoding"])
+        content = AddFileAction.decode(content, encoding)
 
         if "append" in att and att["append"] == "true":
             target.append_file(dst, content)
-- 
2.21.0




More information about the elbe-devel mailing list