From 2e3c7b464b46d4c58e3a8a4f987b48dc0a18ede9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Mon, 24 Jul 2023 18:02:47 +0200 Subject: [PATCH] Import modules in OCI.new() explicitly Also, add a type hint to the return type of OCI.new() so that this can now be verified with mypy --- kiwi/oci_tools/__init__.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/kiwi/oci_tools/__init__.py b/kiwi/oci_tools/__init__.py index 8951d967618..1201592a28f 100644 --- a/kiwi/oci_tools/__init__.py +++ b/kiwi/oci_tools/__init__.py @@ -15,11 +15,11 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -import importlib from abc import ( ABCMeta, abstractmethod ) +from kiwi.oci_tools.base import OCIBase # project from kiwi.runtime_config import RuntimeConfig @@ -37,20 +37,15 @@ def __init__(self) -> None: return None # pragma: no cover @staticmethod - def new(): - name_map = { - 'umoci': 'Umoci', - 'buildah': 'Buildah' - } - runtime_config = RuntimeConfig() - tool_name = runtime_config.get_oci_archive_tool() - try: - oci_tool = importlib.import_module( - 'kiwi.oci_tools.{}'.format(tool_name) - ) - module_name = 'OCI{}'.format(name_map[tool_name]) - return oci_tool.__dict__[module_name]() - except Exception: + def new() -> OCIBase: + tool_name = RuntimeConfig().get_oci_archive_tool() + if tool_name == "umoci": + from kiwi.oci_tools.umoci import OCIUmoci + return OCIUmoci() + elif tool_name == "buildah": + from kiwi.oci_tools.buildah import OCIBuildah + return OCIBuildah() + else: raise KiwiOCIArchiveToolError( 'No support for {0} tool available'.format(tool_name) )