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