[elbe-devel] [PATCH v2 2/3] xmlprepocess: search and replace urls based on ELBE_MIRROR_SED
Torben Hohn
torben.hohn at linutronix.de
Wed Feb 12 16:11:47 CET 2020
cfg['mirrorsed'] holds a space separated list of even strings to search
for, and the replace with the uneven ones. The replacement takes place
in all <url> nodes, <primary_mirror> and an initvm specific <conf>
node.
Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
elbepack/xmlpreprocess.py | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index 1d8ca7d1e..acc24234e 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -16,6 +16,7 @@ except ImportError:
from tempfile import NamedTemporaryFile
from optparse import OptionGroup
+from itertools import islice
from lxml import etree
from lxml.etree import XMLParser, parse
@@ -37,6 +38,7 @@ mergepaths = ['//target/finetuning',
class XMLPreprocessError(Exception):
pass
+
def preprocess_pgp_key(xml):
for key in xml.iterfind('.//mirror/url-list/url/key'):
@@ -88,6 +90,37 @@ def preprocess_initvm_ports(xml):
forward.getparent().remove(forward)
+def preprocess_mirror_replacement(xml):
+ """Do search and replace on mirror urls
+ The sed patterns are a space separate list
+ in cfg['mirrorsed']
+ """
+
+ ms = cfg['mirrorsed'].split()
+ if (len(ms) % 2) == 1:
+ raise XMLPreprocessError("Uneven number of (search, replace) Values !")
+
+ # now zip even and uneven elements of mirrorsed.split()
+ replacements = zip(islice(ms, 0, None, 2), islice(ms, 1, None, 2))
+
+ # do the replace in the text nodes
+ victims = ['.//mirror/url-list/url/binary',
+ './/mirror/url-list/url/source',
+ './/mirror/url-list/url/key',
+ './/mirror/primary_host']
+
+ for v in victims:
+ for u in xml.iterfind(v):
+ for r in replacements:
+ u.text = u.text.replace(r[0], r[1])
+
+ # mirrorsite is special, because the url to be replaced is
+ # in the 'value' attrib
+ for u in xml.iterfind('//initvm/preseed/conf[@key="pbuilder/mirrorsite"]'):
+ for r in replacements:
+ u.attrib['value'] = u.attrib['value'].replace(r[0], r[1])
+
+
def xmlpreprocess(fname, output, variants=None):
# pylint: disable=too-many-locals
@@ -155,6 +188,8 @@ def xmlpreprocess(fname, output, variants=None):
# handle archivedir elements
xml = combinearchivedir(xml)
+ preprocess_mirror_replacement(xml)
+
# Change public PGP url key to raw key
preprocess_pgp_key(xml)
--
2.20.1
More information about the elbe-devel
mailing list