[elbe-devel] [PATCH v2 3/6] Add definition/implementation of 'finetuning/file' XML tag

dion at linutronix.de dion at linutronix.de
Fri May 17 16:36:56 CEST 2019


From: Olivier Dion <dion at linutronix.de>

The tag 'file' can be used under the tag 'finetuning'.

The implementation will ensure that the path to the
destination exists by using 'mkdir_p'.

The user can provide the attribute 'append="true"' if she
wants to append to the destination instead of truncate it
(the default).

The user can also provide the attribute 'encoding'.  If
that's the case, the content of the XML tag is decode
according to the type of encoding before written to the
file.  As for now, the only encoding supported are plain
text (the default) and base64.

The user can provide the attribute 'owner', 'group' or
'mode' to the XML tag, using the 'chown', 'chgrp' and
'chmod' syntaxes.

NOTE!
=====

- The 'owner' and 'group' attributes might stop the
  operation if the user or group doesn't exist.  e.g. if the
  group doesn't exist and thus 'chgrp' failed, 'chmod' will
  never be done because of the exception throwed by Python

- The whole operation can fail if the destination is an
  existing directory

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/finetuning.py | 55 ++++++++++++++++++++++++++++++++++++++++++
 schema/dbsfed.xsd      | 36 +++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)

diff --git a/elbepack/finetuning.py b/elbepack/finetuning.py
index 97342862..7f6c4a6b 100644
--- a/elbepack/finetuning.py
+++ b/elbepack/finetuning.py
@@ -9,6 +9,8 @@
 from __future__ import print_function
 
 import os
+import errno
+import base64
 
 from shutil import rmtree
 from gpg import core
@@ -344,6 +346,59 @@ class AddGroupAction(FinetuningAction):
 FinetuningAction.register(AddGroupAction)
 
 
+class AddFileAction(FinetuningAction):
+
+    tag = 'file'
+
+    def __init__(self, node):
+        FinetuningAction.__init__(self, node)
+
+    @staticmethod
+    def decode(text, encoding):
+        if encoding == "base64":
+            return base64.standard_b64decode(text)
+        else:
+            raise FinetuningException("Invalid encoding %s" % encoding)
+
+    def execute(self, log, _buildenv, target):
+
+        att = self.node.et.attrib
+        dst = att["dst"]
+        content = self.node.et.text
+        owner = None
+        group = None
+        mode = None
+
+        if "owner" in att: owner = att["owner"]
+        if "group" in att: group = att["group"]
+        if "mode"  in att: mode  = att["mode"]
+
+        try:
+            target.mkdir_p(os.path.dirname(dst))
+        except OSError as E:
+            if E.errno is not errno.EEXIST:
+                raise
+
+        if "encoding" in att:
+            content = AddFileAction.decode(content, att["encoding"])
+
+        if "append" in att and att["append"] == "true":
+            target.append_file(dst, content)
+        else:
+            target.write_file(dst, None, content)
+
+        if owner is not None:
+            log.chroot(target.path, 'chown "%s" "%s"' % (owner, dst))
+
+        if group is not None:
+            log.chroot(target.path, 'chgrp "%s" "%s"' % (group, dst))
+
+        if mode is not None:
+            log.chroot(target.path, 'chmod "%s" "%s"' % (mode, dst))
+
+FinetuningAction.register(AddFileAction)
+
+
 class RawCmdAction(FinetuningAction):
 
     tag = 'raw_cmd'
diff --git a/schema/dbsfed.xsd b/schema/dbsfed.xsd
index 333c0a8b..991a8d6f 100644
--- a/schema/dbsfed.xsd
+++ b/schema/dbsfed.xsd
@@ -1737,6 +1737,13 @@
           </documentation>
         </annotation>
       </element>
+      <element name="file" type="rfs:file" minOccurs="0">
+        <annotation>
+          <documentation>
+	    write or append text to a file
+          </documentation>
+        </annotation>
+      </element>
       <element name="rm" type="rfs:rm" minOccurs="0">
         <annotation>
           <documentation>
@@ -2085,6 +2092,35 @@
     </simpleContent>
   </complexType>
 
+    <complexType name="file">
+    <annotation>
+      <documentation>
+        write or append text to a file, the following parameters
+        are avaible:
+
+        'dst' - the destination to write to
+        'encoding' - the (optional) encoding of the content
+        'append="True"' - append the content to file instead of writing
+        'owner' - (optional) set file owner
+        'group' - (optional) set group ownership
+        'mode' - (optional) set file mode bits
+
+        The value of the tag is the raw/encoded content to
+        write/append to the destination
+      </documentation>
+    </annotation>
+    <simpleContent>
+      <extension base="rfs:string">
+        <attribute name="dst"      type="string"  use="required" />
+        <attribute name="encoding" type="string"  use="optional" />
+        <attribute name="append"   type="boolean" use="optional" />
+        <attribute name="owner"    type="string"  use="optional" />
+        <attribute name="group"    type="string"  use="optional" />
+        <attribute name="mode"     type="string"  use="optional" />
+      </extension>
+    </simpleContent>
+  </complexType>
+
   <complexType name="adduser">
     <annotation>
       <documentation>
-- 
2.21.0




More information about the elbe-devel mailing list