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

Torben Hohn torben.hohn at linutronix.de
Wed Feb 12 15:38:51 CET 2020


On Wed, Feb 12, 2020 at 09:07:42AM -0500, Olivier Dion wrote:
> 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])
> ----------------------------------------------------------------------

indeed, this grew slowly, and i didnt notice ;D

> 
> > +
> > +    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

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner



More information about the elbe-devel mailing list