plugin.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python -tt
  2. #
  3. # Copyright (c) 2011 Intel, Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by the Free
  7. # Software Foundation; version 2 of the License
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. # for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc., 59
  16. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. import os, sys
  18. from wic import msger
  19. from wic import pluginbase
  20. from wic.utils import errors
  21. from wic.utils.oe.misc import *
  22. __ALL__ = ['PluginMgr', 'pluginmgr']
  23. PLUGIN_TYPES = ["imager", "source"]
  24. PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts
  25. SCRIPTS_PLUGIN_DIR = "scripts" + PLUGIN_DIR
  26. class PluginMgr(object):
  27. plugin_dirs = {}
  28. # make the manager class as singleton
  29. _instance = None
  30. def __new__(cls, *args, **kwargs):
  31. if not cls._instance:
  32. cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs)
  33. return cls._instance
  34. def __init__(self):
  35. wic_path = os.path.dirname(__file__)
  36. eos = wic_path.find('scripts') + len('scripts')
  37. scripts_path = wic_path[:eos]
  38. self.scripts_path = scripts_path
  39. self.plugin_dir = scripts_path + PLUGIN_DIR
  40. self.layers_path = None
  41. def _build_plugin_dir_list(self, dl, ptype):
  42. if self.layers_path is None:
  43. self.layers_path = get_bitbake_var("BBLAYERS")
  44. layer_dirs = []
  45. if self.layers_path is not None:
  46. for layer_path in self.layers_path.split():
  47. path = os.path.join(layer_path, SCRIPTS_PLUGIN_DIR, ptype)
  48. layer_dirs.append(path)
  49. path = os.path.join(dl, ptype)
  50. layer_dirs.append(path)
  51. return layer_dirs
  52. def append_dirs(self, dirs):
  53. for path in dirs:
  54. self._add_plugindir(path)
  55. # load all the plugins AGAIN
  56. self._load_all()
  57. def _add_plugindir(self, path):
  58. path = os.path.abspath(os.path.expanduser(path))
  59. if not os.path.isdir(path):
  60. msger.debug("Plugin dir is not a directory or does not exist: %s"\
  61. % path)
  62. return
  63. if path not in self.plugin_dirs:
  64. self.plugin_dirs[path] = False
  65. # the value True/False means "loaded"
  66. def _load_all(self):
  67. for (pdir, loaded) in self.plugin_dirs.iteritems():
  68. if loaded: continue
  69. sys.path.insert(0, pdir)
  70. for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
  71. if mod and mod != '__init__':
  72. if mod in sys.modules:
  73. #self.plugin_dirs[pdir] = True
  74. msger.warning("Module %s already exists, skip" % mod)
  75. else:
  76. try:
  77. pymod = __import__(mod)
  78. self.plugin_dirs[pdir] = True
  79. msger.debug("Plugin module %s:%s imported"\
  80. % (mod, pymod.__file__))
  81. except ImportError, err:
  82. msg = 'Failed to load plugin %s/%s: %s' \
  83. % (os.path.basename(pdir), mod, err)
  84. msger.warning(msg)
  85. del(sys.path[0])
  86. def get_plugins(self, ptype):
  87. """ the return value is dict of name:class pairs """
  88. if ptype not in PLUGIN_TYPES:
  89. raise errors.CreatorError('%s is not valid plugin type' % ptype)
  90. plugins_dir = self._build_plugin_dir_list(self.plugin_dir, ptype)
  91. self.append_dirs(plugins_dir)
  92. return pluginbase.get_plugins(ptype)
  93. def get_source_plugins(self):
  94. """
  95. Return list of available source plugins.
  96. """
  97. plugins_dir = self._build_plugin_dir_list(self.plugin_dir, 'source')
  98. self.append_dirs(plugins_dir)
  99. plugins = []
  100. for _source_name, klass in self.get_plugins('source').iteritems():
  101. plugins.append(_source_name)
  102. return plugins
  103. def get_source_plugin_methods(self, source_name, methods):
  104. """
  105. The methods param is a dict with the method names to find. On
  106. return, the dict values will be filled in with pointers to the
  107. corresponding methods. If one or more methods are not found,
  108. None is returned.
  109. """
  110. return_methods = None
  111. for _source_name, klass in self.get_plugins('source').iteritems():
  112. if _source_name == source_name:
  113. for _method_name in methods.keys():
  114. if not hasattr(klass, _method_name):
  115. msger.warning("Unimplemented %s source interface for: %s"\
  116. % (_method_name, _source_name))
  117. return None
  118. func = getattr(klass, _method_name)
  119. methods[_method_name] = func
  120. return_methods = methods
  121. return return_methods
  122. pluginmgr = PluginMgr()