[elbe-devel] [PATCH v3 2/3] preprocess: extend variant management functionality

Manuel Traut manut at linutronix.de
Fri Dec 22 14:37:22 CET 2017


It is not possible to use multiple sections with variant attributes like this:

<pkg-list variant='audio>
  <pkg>alsa</pkg>
  <pkg>pavucontrol</pkg>
</pkg-list>
<pkg-list variant='video>
  <pkg>totem</pkg>
  <pkg>ffmpeg</pkg>
</pkg-list>

It will work, if just --variant=audio OR --variant=video is used, but if
--variant=audio,video is specified it results in two <pkg-list> sections
and that's not allowed by dbsfed.xsd.

Allow defining mergeable sections by xpath.
A list of mergeable sections is hardcoded in elbepack/xmlpreprocess.py
If one of these sections occure multiple times the contents are merged into a
single section.

Signed-off-by: Manuel Traut <manut at linutronix.de>
---
 elbepack/xmlpreprocess.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index 8e4c190e..67d2a115 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -4,10 +4,16 @@
 # SPDX-License-Identifier: GPL-3.0
 
 import sys
+import re
 from lxml import etree
 from lxml.etree import XMLParser, parse
 from numpy import intersect1d
 
+# dictionary of sections that are allowed to exists multiple times before
+# preprocess and that childrens are merge into one section during finetuning
+mergetags = {'/ns0:RootFileSystem/target/finetuning': [],
+             '/ns0:RootFileSystem/target/pkg-list': []}
+
 
 class XMLPreprocessError(Exception):
     def __init__(self, message):
@@ -42,11 +48,37 @@ def xmlpreprocess(fname, output, variants=[]):
                     # specified remove the tag
                     else:
                         rmlist.append(tag)
+            # if there is only one pkg-list the path is:
+            # /ns0:RootFileSystem/target/pkg-list
+            # if there are multiple pkg-lists the path looks like this:
+            # /ns0:RootFileSystem/target/pkg-list[1]
+            xmlpath = xml.getpath(tag)
+            # remove indices from path
+            xmlpath = re.sub('\[[0-9]*\]*', '', xmlpath)
+            # remember mergeable sections, sections that are deleted soon don't
+            # need to be remembered
+            if xmlpath in mergetags.keys() and tag not in rmlist:
+                mergetags[xmlpath].append(tag)
 
         # postponed tag deletion
         for tag in rmlist:
             tag.getparent().remove(tag)
 
+        # if there are multiple sections because of sth like '<finetuning
+        # variant='A'> ...  and <finetuning variant='B'> and running preprocess
+        # with --variant=A,B the two sections need to be merged
+        for mergetag in mergetags.keys():
+            # if there is just one section of a type nothing needs to be done
+            if len(mergetags[mergetag]) < 2:
+                continue
+            # append all childrens of section[1..n] to section[0] and delete
+            # section[1..n]
+            for section in mergetags[mergetag]:
+                if section != mergetags[mergetag][0]:
+                    for child in section.getchildren():
+                        mergetags[mergetag][0].append(child)
+                    section.getparent().remove(section)
+
         if schema.validate(xml):
             xml.write(
                 output,
-- 
2.15.1




More information about the elbe-devel mailing list