pluginbase.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2011 Intel, Inc.
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. __all__ = ['ImagerPlugin', 'SourcePlugin']
  8. import os
  9. import logging
  10. from collections import defaultdict
  11. from importlib.machinery import SourceFileLoader
  12. from wic import WicError
  13. from wic.misc import get_bitbake_var
  14. PLUGIN_TYPES = ["imager", "source"]
  15. SCRIPTS_PLUGIN_DIR = ["scripts/lib/wic/plugins", "lib/wic/plugins"]
  16. logger = logging.getLogger('wic')
  17. PLUGINS = defaultdict(dict)
  18. class PluginMgr:
  19. _plugin_dirs = []
  20. @classmethod
  21. def get_plugins(cls, ptype):
  22. """Get dictionary of <plugin_name>:<class> pairs."""
  23. if ptype not in PLUGIN_TYPES:
  24. raise WicError('%s is not valid plugin type' % ptype)
  25. # collect plugin directories
  26. if not cls._plugin_dirs:
  27. cls._plugin_dirs = [os.path.join(os.path.dirname(__file__), 'plugins')]
  28. layers = get_bitbake_var("BBLAYERS") or ''
  29. for layer_path in layers.split():
  30. for script_plugin_dir in SCRIPTS_PLUGIN_DIR:
  31. path = os.path.join(layer_path, script_plugin_dir)
  32. path = os.path.abspath(os.path.expanduser(path))
  33. if path not in cls._plugin_dirs and os.path.isdir(path):
  34. cls._plugin_dirs.insert(0, path)
  35. if ptype not in PLUGINS:
  36. # load all ptype plugins
  37. for pdir in cls._plugin_dirs:
  38. ppath = os.path.join(pdir, ptype)
  39. if os.path.isdir(ppath):
  40. for fname in os.listdir(ppath):
  41. if fname.endswith('.py'):
  42. mname = fname[:-3]
  43. mpath = os.path.join(ppath, fname)
  44. logger.debug("loading plugin module %s", mpath)
  45. SourceFileLoader(mname, mpath).load_module()
  46. return PLUGINS.get(ptype)
  47. class PluginMeta(type):
  48. def __new__(cls, name, bases, attrs):
  49. class_type = type.__new__(cls, name, bases, attrs)
  50. if 'name' in attrs:
  51. PLUGINS[class_type.wic_plugin_type][attrs['name']] = class_type
  52. return class_type
  53. class ImagerPlugin(metaclass=PluginMeta):
  54. wic_plugin_type = "imager"
  55. def do_create(self):
  56. raise WicError("Method %s.do_create is not implemented" %
  57. self.__class__.__name__)
  58. class SourcePlugin(metaclass=PluginMeta):
  59. wic_plugin_type = "source"
  60. """
  61. The methods that can be implemented by --source plugins.
  62. Any methods not implemented in a subclass inherit these.
  63. """
  64. @classmethod
  65. def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
  66. bootimg_dir, kernel_dir, native_sysroot):
  67. """
  68. Called after all partitions have been prepared and assembled into a
  69. disk image. This provides a hook to allow finalization of a
  70. disk image e.g. to write an MBR to it.
  71. """
  72. logger.debug("SourcePlugin: do_install_disk: disk: %s", disk_name)
  73. @classmethod
  74. def do_stage_partition(cls, part, source_params, creator, cr_workdir,
  75. oe_builddir, bootimg_dir, kernel_dir,
  76. native_sysroot):
  77. """
  78. Special content staging hook called before do_prepare_partition(),
  79. normally empty.
  80. Typically, a partition will just use the passed-in parame e.g
  81. straight bootimg_dir, etc, but in some cases, things need to
  82. be more tailored e.g. to use a deploy dir + /boot, etc. This
  83. hook allows those files to be staged in a customized fashion.
  84. Not that get_bitbake_var() allows you to acces non-standard
  85. variables that you might want to use for this.
  86. """
  87. logger.debug("SourcePlugin: do_stage_partition: part: %s", part)
  88. @classmethod
  89. def do_configure_partition(cls, part, source_params, creator, cr_workdir,
  90. oe_builddir, bootimg_dir, kernel_dir,
  91. native_sysroot):
  92. """
  93. Called before do_prepare_partition(), typically used to create
  94. custom configuration files for a partition, for example
  95. syslinux or grub config files.
  96. """
  97. logger.debug("SourcePlugin: do_configure_partition: part: %s", part)
  98. @classmethod
  99. def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
  100. oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
  101. native_sysroot):
  102. """
  103. Called to do the actual content population for a partition i.e. it
  104. 'prepares' the partition to be incorporated into the image.
  105. """
  106. logger.debug("SourcePlugin: do_prepare_partition: part: %s", part)
  107. @classmethod
  108. def do_post_partition(cls, part, source_params, creator, cr_workdir,
  109. oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
  110. native_sysroot):
  111. """
  112. Called after the partition is created. It is useful to add post
  113. operations e.g. security signing the partition.
  114. """
  115. logger.debug("SourcePlugin: do_post_partition: part: %s", part)