From 16e9caac11d7bc4003faf903f0cedf9238abbce2 Mon Sep 17 00:00:00 2001 From: Daniel Milnes Date: Sat, 6 Apr 2024 23:07:02 +0100 Subject: [PATCH] Replace Usage of Imp with Importlib This is a pretty hacky reimplementation based on https://docs.python.org/3/whatsnew/3.12.html#imp, but I think it should work. Python no longer complains that the code is invalid, however I unfortunately don't have an iDRAC easily accessible to test with. --- omsdk/sdkinfra.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/omsdk/sdkinfra.py b/omsdk/sdkinfra.py index 4b39a58..efbe0ce 100644 --- a/omsdk/sdkinfra.py +++ b/omsdk/sdkinfra.py @@ -21,7 +21,8 @@ # Authors: Vaideeswaran Ganesan # import os -import imp +import importlib.util +import importlib.machinery import logging import socket import sys, glob @@ -31,6 +32,18 @@ logger = logging.getLogger(__name__) +# https://docs.python.org/3/whatsnew/3.12.html#imp +def load_module(modname, filename, compiled): + if compiled: + loader = importlib.machinery.SourcelessFileLoader(modname, filename) + else: + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + class sdkinfra: """ Class to initilaize and load the device drivers @@ -45,9 +58,9 @@ def load_from_file(self, filepath): mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1]) logger.debug("Loading " + filepath + "...") if file_ext.lower() == '.py': - py_mod = imp.load_source(mod_name, filepath) + py_mod = load_module(mod_name, filepath, False) elif file_ext.lower() == '.pyc': - py_mod = imp.load_compiled(mod_name, filepath) + py_mod = load_module(mod_name, filepath, True) return {"name": mod_name, "module": py_mod} def importPath(self, srcdir=None):