[elbe-devel] [PATCH v2 2/3] xmlprepocess: search and replace urls based on ELBE_MIRROR_SED

Olivier Dion dion at linutronix.de
Wed Feb 12 16:57:19 CET 2020


On Wed, 12 Feb 2020, Torben Hohn <torben.hohn at linutronix.de> 
wrote:
> 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>
Reviewed-by: Olivier Dion <dion 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
>
>
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel
-- 
Olivier Dion
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen



More information about the elbe-devel mailing list