blob: bfb73ca6155673411f61635fa661ce7f014fd3ee [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/env python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008__all__ = ['ImagerPlugin', 'SourcePlugin']
Patrick Williamsc0f7c042017-02-23 20:41:17 -06009
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010import os
11import logging
12
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013from collections import defaultdict
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014from importlib.machinery import SourceFileLoader
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016from wic import WicError
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017from wic.misc import get_bitbake_var
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018
19PLUGIN_TYPES = ["imager", "source"]
20
21SCRIPTS_PLUGIN_DIR = "scripts/lib/wic/plugins"
22
23logger = logging.getLogger('wic')
24
25PLUGINS = defaultdict(dict)
26
27class 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 Williamsc124f4f2015-09-15 14:41:29 -050059
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060class PluginMeta(type):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060061 def __new__(cls, name, bases, attrs):
62 class_type = type.__new__(cls, name, bases, attrs)
63 if 'name' in attrs:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064 PLUGINS[class_type.wic_plugin_type][attrs['name']] = class_type
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
Patrick Williamsc0f7c042017-02-23 20:41:17 -060066 return class_type
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068class ImagerPlugin(metaclass=PluginMeta):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 wic_plugin_type = "imager"
70
Brad Bishop6e60e8b2018-02-01 10:27:11 -050071 def do_create(self):
72 raise WicError("Method %s.do_create is not implemented" %
73 self.__class__.__name__)
74
75class SourcePlugin(metaclass=PluginMeta):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 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 Bishop6e60e8b2018-02-01 10:27:11 -050091 logger.debug("SourcePlugin: do_install_disk: disk: %s", disk_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
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 Bishop6e60e8b2018-02-01 10:27:11 -0500108 logger.debug("SourcePlugin: do_stage_partition: part: %s", part)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
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 Bishop6e60e8b2018-02-01 10:27:11 -0500119 logger.debug("SourcePlugin: do_configure_partition: part: %s", part)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120
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 Bishop6e60e8b2018-02-01 10:27:11 -0500129 logger.debug("SourcePlugin: do_prepare_partition: part: %s", part)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400130
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)