[elbe-devel] [PATCH 1/1] Validation should preprocess the XML files

Bastian Germann bage at linutronix.de
Wed Jul 31 12:22:09 CEST 2019


> From: Olivier Dion <dion at linutronix.de>
> 
> See <https://github.com/Linutronix/elbe/issues/229>.
> 
> Note that error_log_to_strings was moved to xmlpreprocess to avoid
> circular dependency.
> 
> The XML preprocessor would need some good refactoring ..
> 
> Signed-off-by: Olivier Dion <dion at linutronix.de>

Reviewed-by: Bastian Germann <bage at linutronix.de>

> ---
>  elbepack/validate.py      | 44 ++++++++++++++------------------------------
>  elbepack/xmlpreprocess.py | 27 ++++++++++++++++++++++++---
>  2 files changed, 38 insertions(+), 33 deletions(-)
> 
> diff --git a/elbepack/validate.py b/elbepack/validate.py
> index c1e5c771..d2c60c40 100644
> --- a/elbepack/validate.py
> +++ b/elbepack/validate.py
> @@ -11,28 +11,9 @@ import sys
>  from lxml import etree
>  from lxml.etree import XMLParser, parse
>  
> +from elbepack.shellhelper import CommandError
> +from elbepack.xmlpreprocess import PreprocessWrapper, error_log_to_strings
>  
> -def error_log_to_strings(error_log):
> -    errors = []
> -    uses_xinclude = False
> -    uses_norecommend = False
> -
> -    for err in error_log:
> -        errors.append("%s:%d error %s" % (err.filename, err.line, err.message))
> -        if "http://www.w3.org/2003/XInclude" in err.message:
> -            uses_xinclude = True
> -        if "norecommend" in err.message:
> -            uses_norecommend = True
> -
> -    if uses_xinclude:
> -        errors.append("\nThere are XIncludes in the XML file. "
> -                      "Run 'elbe preprocess' first!\n")
> -    if uses_norecommend:
> -        errors.append("\nThe XML file uses <norecommend />. "
> -                      "This function was broken all the time and did the "
> -                      "opposite. If you want to retain the original "
> -                      "behaviour, please specify <install-recommends /> !\n")
> -    return errors
>  
>  def validate_xml(fname):
>      if os.path.getsize(fname) > (1 << 30):
> @@ -45,15 +26,18 @@ def validate_xml(fname):
>      schema = etree.XMLSchema(schema_tree)
>  
>      try:
> -        xml = parse(fname, parser=parser)
> -
> -        if schema.validate(xml):
> -            return validate_xml_content(xml)
> -    except etree.XMLSyntaxError:
> -        return ["XML Parse error\n" + str(sys.exc_info()[1])]
> -    except BaseException:
> -        return ["Unknown Exception during validation\n" +
> -                str(sys.exc_info()[1])]
> +        with PreprocessWrapper(fname) as xml_p:
> +            xml = parse(xml_p.preproc, parser=parser)
> +            try:
> +                if schema.validate(xml):
> +                    return validate_xml_content(xml)
> +            except etree.XMLSyntaxError:
> +                return ["XML Parse error\n" + str(sys.exc_info()[1])]
> +            except BaseException:
> +                return ["Unknown Exception during validation\n" +
> +                        str(sys.exc_info()[1])]
> +    except CommandError as E:
> +        return ["Fail preprocessor\n%r" % E]
>  
>      # We have errors, return them in string form...
>      return error_log_to_strings(schema.error_log)
> diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
> index 304b1b37..64186945 100644
> --- a/elbepack/xmlpreprocess.py
> +++ b/elbepack/xmlpreprocess.py
> @@ -19,7 +19,6 @@ from lxml.etree import XMLParser, parse
>  from elbepack.archivedir import ArchivedirError, combinearchivedir
>  from elbepack.directories import elbe_exe
>  from elbepack.shellhelper import command_out_stderr, CommandError
> -from elbepack.validate import error_log_to_strings
>  
>  # list of sections that are allowed to exists multiple times before
>  # preprocess and that childrens are merge into one section during preprocess
> @@ -137,12 +136,12 @@ def xmlpreprocess(fname, output, variants=None):
>  
>  
>  class PreprocessWrapper(object):    # pylint: disable=too-few-public-methods
> -    def __init__(self, xmlfile, opt):
> +    def __init__(self, xmlfile, opt=None):
>          self.xmlfile = xmlfile
>          self.outxml = None
>          self.options = ""
>  
> -        if opt.variant:
> +        if opt and opt.variant:
>              self.options += ' --variants "%s"' % opt.variant
>  
>      def __enter__(self):
> @@ -178,3 +177,25 @@ class PreprocessWrapper(object):    # pylint: disable=too-few-public-methods
>      @property
>      def preproc(self):
>          return self.outxml.name
> +
> +def error_log_to_strings(error_log):
> +    errors = []
> +    uses_xinclude = False
> +    uses_norecommend = False
> +
> +    for err in error_log:
> +        errors.append("%s:%d error %s" % (err.filename, err.line, err.message))
> +        if "http://www.w3.org/2003/XInclude" in err.message:
> +            uses_xinclude = True
> +        if "norecommend" in err.message:
> +            uses_norecommend = True
> +
> +    if uses_xinclude:
> +        errors.append("\nThere are XIncludes in the XML file. "
> +                      "Run 'elbe preprocess' first!\n")
> +    if uses_norecommend:
> +        errors.append("\nThe XML file uses <norecommend />. "
> +                      "This function was broken all the time and did the "
> +                      "opposite. If you want to retain the original "
> +                      "behaviour, please specify <install-recommends /> !\n")
> +    return errors
> 



More information about the elbe-devel mailing list