[elbe-devel] [PATCH 2/3] Add a E.L.B.E. preprocessor command

Benedikt Spranger b.spranger at linutronix.de
Thu Mar 23 12:47:28 CET 2017


Resolving Xincludes or other XML lowlevel features like external resources,
XLink or other may disturb the internel E.L.B.E. XML handling. Add a
preprocessor command to resolve these features and create a flat E.L.B.E.
XML file.

Signed-off-by: Benedikt Spranger <b.spranger at linutronix.de>
---
 elbepack/commands/preprocess.py | 41 +++++++++++++++++++++++++++++++
 elbepack/initvmaction.py        | 12 ++++++++++
 elbepack/xmlpreprocess.py       | 53 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 elbepack/commands/preprocess.py
 create mode 100644 elbepack/xmlpreprocess.py

diff --git a/elbepack/commands/preprocess.py b/elbepack/commands/preprocess.py
new file mode 100644
index 0000000..06f538e
--- /dev/null
+++ b/elbepack/commands/preprocess.py
@@ -0,0 +1,41 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (C) 2017  Linutronix GmbH
+#
+# This file is part of ELBE.
+#
+# ELBE is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# ELBE is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import print_function
+
+import sys
+from optparse import OptionParser
+from elbepack.xmlpreprocess import XMLPreprocessError, xmlpreprocess
+
+def run_command( argv ):
+    oparser = OptionParser( usage="usage: %prog preprocess [options] <xmlfile>")
+    oparser.add_option ("-o", "--output", dest="output",
+                        default="preprocess.xml",
+                        help="preprocessed output file", metavar="<xmlfile>")
+    (opt,args) = oparser.parse_args(argv)
+
+    if len(args) != 1:
+        print("Wrong number of arguments", file=sys.stderr)
+        oparser.print_help()
+        sys.exit(20)
+
+    try:
+        xmlpreprocess(args[0], opt.output)
+    except XMLPreprocessError as e:
+        print(e, file=sys.stderr)
+        sys.exit(20)
diff --git a/elbepack/initvmaction.py b/elbepack/initvmaction.py
index ed28e1c..bb865c6 100644
--- a/elbepack/initvmaction.py
+++ b/elbepack/initvmaction.py
@@ -27,6 +27,8 @@ from elbepack.shellhelper import CommandError, system, command_out_stderr
 from elbepack.filesystem  import wdfs, TmpdirFilesystem, Filesystem
 from elbepack.elbexml     import ElbeXML, ValidationError
 
+from tempfile import NamedTemporaryFile
+
 import sys
 import time
 import os
@@ -506,6 +508,16 @@ class SubmitAction(InitVMAction):
                 print ('Unknown file ending (use either xml or iso)', file=sys.stderr)
                 sys.exit (20)
 
+            outxml = NamedTemporaryFile(prefix='elbe', suffix='xml')
+            cmd = '%s preprocess -o %s %s' % (elbe_exe, outxml.name, xmlfile)
+            ret, msg, err = command_out_stderr (cmd)
+            if ret != 0:
+                print ("elbe preprocess failed.", file=sys.stderr)
+                print (err, file=sys.stderr)
+                print ("Giving up", file=sys.stderr)
+                sys.exit(20)
+            xmlfile = outxml.name
+
             ret, prjdir, err = command_out_stderr ('%s control create_project' % (elbe_exe))
             if ret != 0:
                 print ("elbe control create_project failed.", file=sys.stderr)
diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
new file mode 100644
index 0000000..4fa4cfd
--- /dev/null
+++ b/elbepack/xmlpreprocess.py
@@ -0,0 +1,53 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (C) 2017  Linutronix GmbH
+#
+# This file is part of ELBE.
+#
+# ELBE is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# ELBE is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import sys
+import elbepack
+from lxml import etree
+from lxml.etree import XMLParser,parse
+
+class XMLPreprocessError(Exception):
+    def __init__ (self, message):
+        Exception.__init__(self, message)
+
+def xmlpreprocess(fname, output):
+    schema_file = os.path.join(elbepack.__path__[0], "dbsfed.xsd")
+    parser = XMLParser(huge_tree=True)
+    schema_tree = etree.parse(schema_file)
+    schema = etree.XMLSchema(schema_tree)
+
+    try:
+        xml = parse(fname,parser=parser)
+        xml.xinclude()
+
+        if schema.validate(xml):
+            xml.write(output, encoding="UTF-8", pretty_print=True, compression=9)
+            return
+
+    except etree.XMLSyntaxError:
+        raise XMLPreprocessError("XML Parse error\n" + str(sys.exc_info()[1]))
+    except:
+        XMLPreprocessError("Unknown Exception during validation\n" + str(sys.exc_info()[1]))
+
+    # We have errors, return them in string form...
+    errors = []
+    for err in schema.error_log:
+        errors.append("%s:%d error %s" % (err.filename, err.line, err.message))
+
+    raise XMLPreprocessError(errors)
-- 
2.11.0





More information about the elbe-devel mailing list