[elbe-devel] [PATCH 4/5] elbepack: xmlpreprocess: replace don't use run_elbe for preprocessing

Thomas Weißschuh thomas.weissschuh at linutronix.de
Wed Jul 3 11:10:58 CEST 2024


Instead of calling xmlpreprocess() through a CLI call, just call it
directly.
This preserves error information and makes the code shorter.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/initvmaction.py   |  9 ++++-----
 elbepack/pbuilderaction.py |  6 +++---
 elbepack/xmlpreprocess.py  | 38 +++++++-------------------------------
 3 files changed, 14 insertions(+), 39 deletions(-)

diff --git a/elbepack/initvmaction.py b/elbepack/initvmaction.py
index 362149c0fabb..0b0af59a7c59 100644
--- a/elbepack/initvmaction.py
+++ b/elbepack/initvmaction.py
@@ -19,7 +19,7 @@ from elbepack.elbexml import ElbeXML, ValidationError, ValidationMode
 from elbepack.filesystem import TmpdirFilesystem
 from elbepack.repodir import Repodir, RepodirError
 from elbepack.treeutils import etree
-from elbepack.xmlpreprocess import PreprocessWrapper
+from elbepack.xmlpreprocess import preprocess_file
 
 
 def is_soap_local():
@@ -453,8 +453,7 @@ def submit_with_repodir_and_dl_result(xmlfile, cdrom, opt):
 def submit_and_dl_result(xmlfile, cdrom, opt):
 
     try:
-        with PreprocessWrapper(xmlfile, opt) as ppw:
-            xmlfile = ppw.preproc
+        with preprocess_file(xmlfile, opt.variants) as xmlfile:
 
             ps = run_elbe(['control', 'create_project'], capture_output=True, encoding='utf-8')
             if ps.returncode != 0:
@@ -758,8 +757,8 @@ class CreateAction(InitVMAction):
             else:
                 cdrom_opts = []
 
-            with PreprocessWrapper(xmlfile, opt) as ppw:
-                run_elbe(['init', *init_opts, '--directory', initvmdir, *cdrom_opts, ppw.preproc],
+            with preprocess_file(xmlfile, opt.variants) as preproc:
+                run_elbe(['init', *init_opts, '--directory', initvmdir, *cdrom_opts, preproc],
                          check=True)
 
         except subprocess.CalledProcessError:
diff --git a/elbepack/pbuilderaction.py b/elbepack/pbuilderaction.py
index 36fcddfa6bd3..dccef0d7b8d6 100644
--- a/elbepack/pbuilderaction.py
+++ b/elbepack/pbuilderaction.py
@@ -8,7 +8,7 @@ import sys
 
 from elbepack.directories import run_elbe
 from elbepack.filesystem import TmpdirFilesystem
-from elbepack.xmlpreprocess import PreprocessWrapper
+from elbepack.xmlpreprocess import preprocess_file
 
 
 def cmd_exists(x):
@@ -69,7 +69,7 @@ class CreateAction(PBuilderAction):
 
         if opt.xmlfile:
             try:
-                with PreprocessWrapper(opt.xmlfile, opt) as ppw:
+                with preprocess_file(opt.xmlfile, opt.variants) as preproc:
                     ps = run_elbe(['control', 'create_project'],
                                   capture_output=True, encoding='utf-8')
                     if ps.returncode != 0:
@@ -80,7 +80,7 @@ class CreateAction(PBuilderAction):
                         sys.exit(152)
 
                     prjdir = ps.stdout.strip()
-                    ps = run_elbe(['control', 'set_xml', prjdir, ppw.preproc],
+                    ps = run_elbe(['control', 'set_xml', prjdir, preproc],
                                   capture_output=True, encoding='utf-8')
 
                     if ps.returncode != 0:
diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index dab9dcbd426c..0252614ab9b6 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -2,14 +2,13 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2017-2018 Linutronix GmbH
 
+import contextlib
 import logging
 import os
 import pathlib
 import re
-import subprocess
 import sys
 import tempfile
-import time
 import warnings
 from urllib.error import HTTPError, URLError
 from urllib.request import urlopen
@@ -25,7 +24,6 @@ with warnings.catch_warnings():
 
 from elbepack.archivedir import ArchivedirError, combinearchivedir
 from elbepack.config import cfg
-from elbepack.directories import run_elbe
 from elbepack.isooptions import iso_option_valid
 from elbepack.treeutils import dbsfed_schema, xml_bool
 from elbepack.validate import error_log_to_strings
@@ -440,31 +438,9 @@ def xmlpreprocess(xml_input_file, xml_output_file, variants=None, proxy=None, gz
     raise XMLPreprocessError('\n'.join(error_log_to_strings(schema.error_log)))
 
 
-class PreprocessWrapper:
-    def __init__(self, xmlfile, opt):
-        self.xmlfile = xmlfile
-        self.outxml = None
-        self.options = []
-
-        if opt.variant:
-            self.options.extend(['--variants', opt.variant])
-
-    def __enter__(self):
-        fname = f'elbe-{time.time_ns()}.xml'
-        self.outxml = os.path.join(tempfile.gettempdir(), fname)
-
-        ps = run_elbe(['preprocess', *self.options, '-o', self.outxml, self.xmlfile],
-                      capture_output=True, encoding='utf-8')
-        if ps.returncode != 0:
-            print('elbe preprocess failed.', file=sys.stderr)
-            print(ps.stderr, file=sys.stderr)
-            raise subprocess.CalledProcessError(ps.returncode, ps.args)
-
-        return self
-
-    def __exit__(self, _typ, _value, _traceback):
-        os.remove(self.outxml)
-
-    @property
-    def preproc(self):
-        return self.outxml
+ at contextlib.contextmanager
+def preprocess_file(xmlfile, variants):
+    with tempfile.NamedTemporaryFile(suffix='elbe.xml') as preproc:
+        xmlpreprocess(xmlfile, preproc, variants=variants)
+        preproc.seek(0)
+        yield preproc.name

-- 
2.45.2



More information about the elbe-devel mailing list