[elbe-devel] [PATCH 4/7] Add implementation of 'finetuning/file' XML tag

dion at linutronix.de dion at linutronix.de
Fri May 17 11:16:58 CEST 2019


From: Olivier Dion <dion at linutronix.de>

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!
=====
- Whitespaces/tabs are not trimmed from the XML tag

- 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 failed if destination is an existing
  directory

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/finetuning.py | 55 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 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'
-- 
2.21.0




More information about the elbe-devel mailing list