[elbe-devel] [PATCH 2/2] xmlprepocess: search and replace urls based on ELBE_MIRROR_SED
Olivier Dion
dion at linutronix.de
Wed Feb 12 15:07:42 CET 2020
Torben Hohn <torben.hohn at linutronix.de> writes:
> 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..91e9206e6 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
> @@ -87,6 +88,38 @@ def preprocess_initvm_ports(xml):
> host.text == cfg['soapport'] and benv.text == '7588'):
> 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))
> +
> + for u in xml.iterfind('.//mirror/url-list/url/binary'):
> + for r in replacements:
> + u.text = u.text.replace(r[0], r[1])
> +
> + for u in xml.iterfind('.//mirror/url-list/url/source'):
> + for r in replacements:
> + u.text = u.text.replace(r[0], r[1])
> +
> + for u in xml.iterfind('.//mirror/url-list/url/key'):
> + for r in replacements:
> + u.text = u.text.replace(r[0], r[1])
> +
> + for u in xml.iterfind('.//mirror/primary_host'):
> + for r in replacements:
> + u.text = u.text.replace(r[0], r[1])
I would go with an array and a loop.
----------------------------------------------------------------------
victims = [".//mirror/url-list/url/binary",
".//mirror/url-list/url/source",
# ...
]
for v in victims:
for u in xml.iterfind(v):
for r in remplacements:
u.text = u.text.replace(r[0], r[1])
----------------------------------------------------------------------
> +
> + 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):
>
> @@ -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
>
>
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel
More information about the elbe-devel
mailing list