[elbe-devel] [PATCH 10/21] elbepack: soapclient: make set_xml a method on ElbeSoapClient

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Aug 6 11:18:08 CEST 2024


This is useful functionality that should be usable without going through
"elbe control".
Make it a library function that can be called from other parts of elbe
and call it from "elbe control".

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/control.py | 46 +-------------------------------------------
 elbepack/soapclient.py       | 37 +++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 45 deletions(-)

diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
index e7a15471bf3d..0bb87236d416 100644
--- a/elbepack/commands/control.py
+++ b/elbepack/commands/control.py
@@ -15,7 +15,6 @@ from suds import WebFault
 
 from elbepack.cli import add_argument, add_arguments_from_decorated_function
 from elbepack.config import add_arguments_soapclient
-from elbepack.elbexml import ElbeXML, ValidationMode
 from elbepack.soapclient import ElbeSoapClient
 
 
@@ -102,50 +101,7 @@ def _delete_project(client, args):
 @_add_project_dir_argument
 @add_argument('xml')
 def _set_xml(client, args):
-    builddir = args.project_dir
-    filename = args.xml
-
-    try:
-        x = ElbeXML(
-            filename,
-            skip_validate=True,
-            url_validation=ValidationMode.NO_CHECK)
-    except IOError:
-        print(f'{filename} is not a valid elbe xml file')
-        sys.exit(177)
-
-    if not x.has('target'):
-        print("<target> is missing, this file can't be built in an initvm",
-              file=sys.stderr)
-        sys.exit(178)
-
-    size = 1024 * 1024
-    part = 0
-    with open(filename, 'rb') as fp:
-        while True:
-
-            xml_base64 = binascii.b2a_base64(fp.read(size))
-
-            if not isinstance(xml_base64, str):
-                xml_base64 = xml_base64.decode('ascii')
-
-            # finish upload
-            if len(xml_base64) == 1:
-                part = client.service.upload_file(builddir,
-                                                  'source.xml',
-                                                  xml_base64,
-                                                  -1)
-            else:
-                part = client.service.upload_file(builddir,
-                                                  'source.xml',
-                                                  xml_base64,
-                                                  part)
-            if part == -1:
-                print('project busy, upload not allowed')
-                return part
-            if part == -2:
-                print('upload of xml finished')
-                return 0
+    client.set_xml(args.project_dir, args.xml)
 
 
 @add_argument('--build-bin', action='store_true', dest='build_bin',
diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
index 1c59032340ec..b32805d4147d 100644
--- a/elbepack/soapclient.py
+++ b/elbepack/soapclient.py
@@ -14,6 +14,7 @@ from urllib.error import URLError
 from suds.client import Client
 
 from elbepack.cli import CliError
+from elbepack.elbexml import ElbeXML, ValidationMode
 from elbepack.version import elbe_version
 
 
@@ -151,3 +152,39 @@ class ElbeSoapClient:
         prj = self.service.get_project(project_dir)
         if prj.status != 'build_done':
             raise CliError(191, f'Project build was not successful, current status: {prj.status}')
+
+    def set_xml(self, builddir, filename):
+        x = ElbeXML(
+            filename,
+            skip_validate=True,
+            url_validation=ValidationMode.NO_CHECK)
+
+        if not x.has('target'):
+            raise ValueError("<target> is missing, this file can't be built in an initvm")
+
+        size = 1024 * 1024
+        part = 0
+        with open(filename, 'rb') as fp:
+            while True:
+
+                xml_base64 = binascii.b2a_base64(fp.read(size))
+
+                if not isinstance(xml_base64, str):
+                    xml_base64 = xml_base64.decode('ascii')
+
+                # finish upload
+                if len(xml_base64) == 1:
+                    part = self.service.upload_file(builddir,
+                                                    'source.xml',
+                                                    xml_base64,
+                                                    -1)
+                else:
+                    part = self.service.upload_file(builddir,
+                                                    'source.xml',
+                                                    xml_base64,
+                                                    part)
+                if part == -1:
+                    raise RuntimeError('project busy, upload not allowed')
+                if part == -2:
+                    _logger.debug('upload of xml finished')
+                    return

-- 
2.46.0



More information about the elbe-devel mailing list