[elbe-devel] [PATCH 01/12] elbepack: treeutils: introduce a helper to read apt keys from XML files

Thomas Weißschuh thomas.weissschuh at linutronix.de
Mon Nov 18 15:45:18 CET 2024


apt keys span multiple lines and may get misaligned during copy and
past. Elbe has logic to normalize the key strings but that is copied and
pasted everywhere. Unify it in a helper function.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/finetuning.py          |  8 +++-----
 elbepack/init/init-elbe.sh.mako |  4 +++-
 elbepack/pbuilder.py            | 10 ++++------
 elbepack/rfs.py                 |  3 ++-
 elbepack/treeutils.py           |  4 ++++
 elbepack/virtapt.py             |  7 ++++---
 6 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/elbepack/finetuning.py b/elbepack/finetuning.py
index 8d2fda3369263a7907a507a3701ca0f64a0beae9..1fcb632d17719d7fb99b33d94d373bdc6e400250 100644
--- a/elbepack/finetuning.py
+++ b/elbepack/finetuning.py
@@ -22,6 +22,7 @@ from elbepack.packers import default_packer, packers
 from elbepack.repomanager import UpdateRepo
 from elbepack.rpcaptcache import get_rpcaptcache
 from elbepack.shellhelper import chroot, do
+from elbepack.treeutils import strip_leading_whitespace_from_lines
 
 
 class FinetuningException(Exception):
@@ -244,8 +245,7 @@ class AddFileAction(FinetuningAction):
     @staticmethod
     def decode(text, encoding):
         if encoding == 'plain':
-            msg = '\n'.join([line.lstrip(' \t')
-                             for line in text.splitlines()[1:-1]])
+            msg = strip_leading_whitespace_from_lines(text)
         elif encoding == 'raw':
             msg = '\n'.join(text.splitlines()[1:-1])
         elif encoding == 'base64':
@@ -309,9 +309,7 @@ class CmdAction(ImageFinetuningAction):
 
     def execute_img(self, _buildenv, _target, builddir, loop_dev):
 
-        script = '\n'.join(line.lstrip(' \t')
-                           for line
-                           in self.node.et.text.strip('\n').splitlines())
+        script = strip_leading_whitespace_from_lines(self.node.et.text)
 
         mnt = os.path.join(builddir, 'imagemnt')
         dev = f"{loop_dev}p{self.node.et.attrib['part']}"
diff --git a/elbepack/init/init-elbe.sh.mako b/elbepack/init/init-elbe.sh.mako
index 69ee19f0d44889b9a2f697373055267f911b2bcb..b50706e4a9617d6f0c459ced2652ad753ad3843f 100644
--- a/elbepack/init/init-elbe.sh.mako
+++ b/elbepack/init/init-elbe.sh.mako
@@ -4,6 +4,8 @@
 ##
 #! /bin/sh
 <%
+from elbepack.treeutils import strip_leading_whitespace_from_lines
+
 nicmac = prj.text('buildimage/NIC/MAC', default=defs, key='nicmac')
 %>
 
@@ -64,7 +66,7 @@ in-target haveged
 %   for node in prj.all("./finetuning/"):
 %     if "command" == node.tag:
 	cat <<ELBE_INITVM_FINETUNING_EOF > /buildenv/tmp/elbe-initvm
-${"\n".join(line.lstrip(" \t") for line in node.et.text.strip("\n").splitlines())}
+${split_leading_whitespace_from_lines(node.et.text)}
 ELBE_INITVM_FINETUNING_EOF
 	in-target sh /tmp/elbe-initvm
 %     endif
diff --git a/elbepack/pbuilder.py b/elbepack/pbuilder.py
index a7db4506c912b838d9eca66b06dc6cb61c2a99bf..180faeb2b3b15809624d388f5401fcabdc150e8b 100644
--- a/elbepack/pbuilder.py
+++ b/elbepack/pbuilder.py
@@ -6,6 +6,8 @@ import os
 import pathlib
 from urllib.request import urlopen
 
+from elbepack.treeutils import strip_leading_whitespace_from_lines
+
 
 def _write_env(fp, k, v):
     fp.write(f'{k}="{v}"\n')
@@ -180,11 +182,7 @@ def get_apt_keys(builddir, xml):
                 continue
 
             if url.has('raw-key'):
-
-                key = '\n'.join(line.strip(' \t')
-                                for line
-                                in url.text('raw-key').splitlines()[1:-1])
-
-                keys.append((f'elbe-xml-raw-key{i}', key))
+                keys.append((f'elbe-xml-raw-key{i}',
+                             strip_leading_whitespace_from_lines(url.text('raw-key'))))
 
     return keys
diff --git a/elbepack/rfs.py b/elbepack/rfs.py
index 2212e69d7f3db43b938f25e8e16700abfe784df1..fd368ab90d7eae3b7a2126cd0eee98fd273dd976 100644
--- a/elbepack/rfs.py
+++ b/elbepack/rfs.py
@@ -12,6 +12,7 @@ from elbepack.efilesystem import ChRootFilesystem, dpkg_architecture
 from elbepack.egpg import unarmor_openpgp_keyring
 from elbepack.shellhelper import chroot, do
 from elbepack.templates import get_preseed, preseed_to_text, write_pack_template
+from elbepack.treeutils import strip_leading_whitespace_from_lines
 
 
 def create_apt_prefs(xml, rfs):
@@ -283,7 +284,7 @@ class BuildEnv:
             # I could make a none global 'noauth' flag for mirrors
             for i, url in enumerate(self.xml.node('project/mirror/url-list')):
                 if url.has('raw-key'):
-                    key = '\n'.join(line.strip(' \t') for line in url.text('raw-key').splitlines())
+                    key = strip_leading_whitespace_from_lines(url.text('raw-key'))
                     self.add_key(unarmor_openpgp_keyring(key), f'elbe-xml-raw-key{i}.gpg')
 
     def initialize_dirs(self, build_sources=False):
diff --git a/elbepack/treeutils.py b/elbepack/treeutils.py
index 03e4e5fe80c26af8b237bf93e8cae471dc029756..2e9a2526016d9e53d2a22ebd2488da952a8e418f 100644
--- a/elbepack/treeutils.py
+++ b/elbepack/treeutils.py
@@ -37,6 +37,10 @@ def dbsfed_schema():
     return XMLSchema(schema_tree)
 
 
+def strip_leading_whitespace_from_lines(s):
+    return '\n'.join(line.strip(' \t') for line in s.strip().splitlines())
+
+
 class eiter:
     def __init__(self, it):
         self.it = it
diff --git a/elbepack/virtapt.py b/elbepack/virtapt.py
index 704f9909472ab1ca96d696d5d255e74b42c48ef7..a1a21a790a029b82561475222828e7b930df749d 100644
--- a/elbepack/virtapt.py
+++ b/elbepack/virtapt.py
@@ -16,6 +16,7 @@ import apt_pkg
 from elbepack.egpg import unarmor_openpgp_keyring
 from elbepack.filesystem import TmpdirFilesystem
 from elbepack.rfs import create_apt_prefs
+from elbepack.treeutils import strip_leading_whitespace_from_lines
 
 
 class VirtApt:
@@ -104,9 +105,9 @@ class VirtApt:
             # I could make a none global 'noauth' flag for mirrors
             for i, url in enumerate(self.xml.node('project/mirror/url-list')):
                 if url.has('raw-key'):
-                    key = '\n'.join(line.strip(' \t')
-                                    for line in url.text('raw-key').splitlines()[1:-1])
-                    self._add_key(unarmor_openpgp_keyring(key), f'elbe-virtapt-raw-key{i}.gpg')
+                    key = strip_leading_whitespace_from_lines(url.text('raw-key'))
+                    self._add_key(unarmor_openpgp_keyring(key),
+                                  f'elbe-virtapt-raw-key{i}.gpg')
 
     def _initialize_dirs(self):
         self.basefs.mkdir_p('cache/archives/partial')

-- 
2.47.0



More information about the elbe-devel mailing list