testtargetloader.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import types
  5. import bb
  6. import os
  7. # This class is responsible for loading a test target controller
  8. class TestTargetLoader:
  9. # Search oeqa.controllers module directory for and return a controller
  10. # corresponding to the given target name.
  11. # AttributeError raised if not found.
  12. # ImportError raised if a provided module can not be imported.
  13. def get_controller_module(self, target, bbpath):
  14. controllerslist = self.get_controller_modulenames(bbpath)
  15. bb.note("Available controller modules: %s" % str(controllerslist))
  16. controller = self.load_controller_from_name(target, controllerslist)
  17. return controller
  18. # Return a list of all python modules in lib/oeqa/controllers for each
  19. # layer in bbpath
  20. def get_controller_modulenames(self, bbpath):
  21. controllerslist = []
  22. def add_controller_list(path):
  23. if not os.path.exists(os.path.join(path, '__init__.py')):
  24. bb.fatal('Controllers directory %s exists but is missing __init__.py' % path)
  25. files = sorted([f for f in os.listdir(path) if f.endswith('.py') and not f.startswith('_')])
  26. for f in files:
  27. module = 'oeqa.controllers.' + f[:-3]
  28. if module not in controllerslist:
  29. controllerslist.append(module)
  30. else:
  31. bb.warn("Duplicate controller module found for %s, only one added. Layers should create unique controller module names" % module)
  32. for p in bbpath:
  33. controllerpath = os.path.join(p, 'lib', 'oeqa', 'controllers')
  34. bb.debug(2, 'Searching for target controllers in %s' % controllerpath)
  35. if os.path.exists(controllerpath):
  36. add_controller_list(controllerpath)
  37. return controllerslist
  38. # Search for and return a controller from given target name and
  39. # set of module names.
  40. # Raise AttributeError if not found.
  41. # Raise ImportError if a provided module can not be imported
  42. def load_controller_from_name(self, target, modulenames):
  43. for name in modulenames:
  44. obj = self.load_controller_from_module(target, name)
  45. if obj:
  46. return obj
  47. raise AttributeError("Unable to load {0} from available modules: {1}".format(target, str(modulenames)))
  48. # Search for and return a controller or None from given module name
  49. def load_controller_from_module(self, target, modulename):
  50. obj = None
  51. # import module, allowing it to raise import exception
  52. module = __import__(modulename, globals(), locals(), [target])
  53. # look for target class in the module, catching any exceptions as it
  54. # is valid that a module may not have the target class.
  55. try:
  56. obj = getattr(module, target)
  57. if obj:
  58. from oeqa.targetcontrol import BaseTarget
  59. if( not issubclass(obj, BaseTarget)):
  60. bb.warn("Target {0} found, but subclass is not BaseTarget".format(target))
  61. except:
  62. obj = None
  63. return obj