oeqa/runtime: load modules using importlib
PATCH 1/3 oeqa/runtime: load modules using importlib
Instead of using __import__() which is low-level and discouraged, use importlib.
Signed-off-by Ross Burton <[email protected]>
--- meta/lib/oeqa/runtime/context.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
meta/lib/oeqa/runtime/context.py +5 -9
@@ -175,16 +175,12 @@ class OERuntimeTestContextExecutor(OETestContextExecutor): # Search for and return a controller or None from given module name @staticmethod def _loadControllerFromModule(target, modulename): - obj = None - # import module, allowing it to raise import exception - module = __import__(modulename, globals(), locals(), [target]) - # look for target class in the module, catching any exceptions as it - # is valid that a module may not have the target class. try: - obj = getattr(module, target) - except: - obj = None - return obj + import importlib + module = importlib.import_module(modulename) + return getattr(module, target) + except AttributeError: + return None @staticmethod def readPackagesManifest(manifest):
Signature · 1 line
2.25.1
PATCH 2/3 oeqa/runtime: search sys.path explicitly for modules
The controller module loading code needs to be told what directories to search for modules via the target_modules_path keyword argument, which is set to BBPATH.
However, as the actual module loading is done via importlib this relies on the paths being on sys.path, which it is as base.bbclass puts each layer's lib/ in sys.path.
Simplify the code by removing this indirection, and simply search sys.path directly.
Signed-off-by Ross Burton <[email protected]>
--- meta/lib/oeqa/runtime/context.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
meta/lib/oeqa/runtime/context.py +7 -8
@@ -5,6 +5,7 @@ # import os +import sys from oeqa.core.context import OETestContext, OETestContextExecutor from oeqa.core.target.ssh import OESSHTarget
@@ -119,8 +120,7 @@ class OERuntimeTestContextExecutor(OETestContextExecutor): # XXX: Don't base your targets on this code it will be refactored # in the near future. # Custom target module loading - target_modules_path = kwargs.get('target_modules_path', '') - controller = OERuntimeTestContextExecutor.getControllerModule(target_type, target_modules_path) + controller = OERuntimeTestContextExecutor.getControllerModule(target_type) target = controller(logger, target_ip, server_ip, **kwargs) return target
@@ -130,15 +130,15 @@ class OERuntimeTestContextExecutor(OETestContextExecutor): # AttributeError raised if not found. # ImportError raised if a provided module can not be imported. @staticmethod - def getControllerModule(target, target_modules_path): - controllerslist = OERuntimeTestContextExecutor._getControllerModulenames(target_modules_path) + def getControllerModule(target): + controllerslist = OERuntimeTestContextExecutor._getControllerModulenames() controller = OERuntimeTestContextExecutor._loadControllerFromName(target, controllerslist) return controller # Return a list of all python modules in lib/oeqa/controllers for each # layer in bbpath @staticmethod - def _getControllerModulenames(target_modules_path): + def _getControllerModulenames(): controllerslist = []
@@ -153,9 +153,8 @@ class OERuntimeTestContextExecutor(OETestContextExecutor): else: raise RuntimeError("Duplicate controller module found for %s. Layers should create unique controller module names" % module) - extpath = target_modules_path.split(':') - for p in extpath: - controllerpath = os.path.join(p, 'lib', 'oeqa', 'controllers') + for p in sys.path: + controllerpath = os.path.join(p, 'oeqa', 'controllers') if os.path.exists(controllerpath): add_controller_list(controllerpath) return controllerslist
Signature · 1 line
2.25.1
PATCH 3/3 testimage: remove target_modules_path
In reply to Ross BurtonThere is no need to pass target_modules_path anymore, so remove it.
Signed-off-by Ross Burton <[email protected]>
--- meta/classes/testimage.bbclass | 4 ---- 1 file changed, 4 deletions(-)
meta/classes/testimage.bbclass +0 -4
@@ -316,10 +316,6 @@ def testimage_main(d): if d.getVar("TESTIMAGE_BOOT_PATTERNS"): target_kwargs['boot_patterns'] = get_testimage_boot_patterns(d) - # TODO: Currently BBPATH is needed for custom loading of targets. - # It would be better to find these modules using instrospection. - target_kwargs['target_modules_path'] = d.getVar('BBPATH') - # hardware controlled targets might need further access target_kwargs['powercontrol_cmd'] = d.getVar("TEST_POWERCONTROL_CMD") or None target_kwargs['powercontrol_extra_args'] = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS") or ""
Signature · 1 line
2.25.1