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