[elbe-devel] [PATCH v2 2/7] xmlpreprocess: add PreprocessWrapper to consolidate calling 'elbe preprocess'

Torben Hohn torben.hohn at linutronix.de
Mon Oct 15 12:43:15 CEST 2018


prepare to add 'elbe preprocess' support to elbe pbuilder.
This class allows for having elbe proeprocess support in both 'elbe initvm'
and 'elbe preprocess' without code duplication.

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 51bc6bdc..bd082779 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -1,13 +1,21 @@
 # ELBE - Debian Based Embedded Rootfilesystem Builder
 # Copyright (c) 2017 Benedikt Spranger <b.spranger at linutronix.de>
 # Copyright (c) 2017 Manuel Traut <manut at linutronix.de>
+# Copyright (c) 2018 Torben Hohn <torbenh at linutronix.de>
 #
 # SPDX-License-Identifier: GPL-3.0-or-later
 
+from __future__ import print_function
+
 import sys
+from tempfile import NamedTemporaryFile
+
 from lxml import etree
 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
 
 # list of sections that are allowed to exists multiple times before
 # preprocess and that childrens are merge into one section during preprocess
@@ -112,3 +120,30 @@ def xmlpreprocess(fname, output, variants=None):
         errors.append("%s:%d error %s" % (err.filename, err.line, err.message))
 
     raise XMLPreprocessError(errors)
+
+
+class PreprocessWrapper(object):    # pylint: disable=too-few-public-methods
+    def __init__(self, xmlfile):
+        self.xmlfile = xmlfile
+        self.outxml = None
+
+    def __enter__(self):
+        self.outxml = NamedTemporaryFile(prefix='elbe', suffix='xml')
+
+        cmd = '%s preprocess -o %s %s' % (elbe_exe,
+                                          self.outxml.name,
+                                          self.xmlfile)
+        ret, _, err = command_out_stderr(cmd)
+        if ret != 0:
+            print("elbe preprocess failed.", file=sys.stderr)
+            print(err, file=sys.stderr)
+            raise CommandError(cmd, ret)
+
+        return self
+
+    def __exit__(self, _typ, _value, _traceback):
+        self.outxml = None
+
+    @property
+    def preproc(self):
+        return self.outxml.name
-- 
2.11.0




More information about the elbe-devel mailing list