Skip to content

Commit

Permalink
feat: support custom distro
Browse files Browse the repository at this point in the history
Resolve the [issue 97](https://github.com/cpb-/yocto-cooker/issues/97) by supporting
a custom distro class with the existing 'base-distribution' menu field.
  • Loading branch information
PierreLoupG committed Mar 12, 2024
1 parent 0eecc01 commit c710ce3
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions cooker/cooker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import re
import sys
import importlib.util
from urllib.parse import urlparse
import jsonschema
import pkg_resources
Expand Down Expand Up @@ -482,19 +483,8 @@ class CookerCommands:
def __init__(self, config, menu):
self.config = config
self.menu = menu
if menu is not None:
distros = {
'poky': PokyDistro,
'arago': AragoDistro,
}
name = menu.setdefault('base-distribution', 'poky')
try:
self.distro = distros[name.lower()]
except:
fatal_error('base-distribution {} is unknown, please add a `base-distribution.py` file next your menu.'.format(name))

# Update distro if custom distro is defined in menu
self.update_override_distro()
self.load_distro()

def init(self, menu_name, layer_dir=None, build_dir=None, dl_dir=None, sstate_dir=None):
""" cooker-command 'init': (re)set the configuration file """
Expand All @@ -513,6 +503,7 @@ def init(self, menu_name, layer_dir=None, build_dir=None, dl_dir=None, sstate_di
self.config.set_sstate_dir(sstate_dir)

self.config.save()
self.load_distro()

def update(self):
info('Update layers in project directory')
Expand Down Expand Up @@ -1053,6 +1044,38 @@ def update_override_distro(self):
# Template conf must be a tuple
self.distro.TEMPLATE_CONF = (override_distro.get("template_conf", self.distro.TEMPLATE_CONF),)

def load_distro(self):
# Load the custom distro only after cooker config file is generated
# from init command (needs the menu location to find the distro file)

if not self.config.empty():
distros = {
'poky': PokyDistro,
'arago': AragoDistro,
}
name = self.menu.setdefault('base-distribution', 'poky').lower()

if name not in distros:
distro_dir = os.path.dirname(self.config.menu())
distro_file = os.path.join(distro_dir, '{}.py'.format(name))
distro_class_name = '{}Distro'.format(name.capitalize())
if CookerCall.os.file_exists(distro_file):
distro_spec = importlib.util.spec_from_file_location("distro", distro_file)
distro_module = importlib.util.module_from_spec(distro_spec)
distro_spec.loader.exec_module(distro_module)
try:
distro_class = getattr(distro_module, distro_class_name)
except AttributeError as e:
fatal_error('distribution file `{}.py` does not contain a {} class'.format(name, distro_class_name))
distros[name] = distro_class
else:
fatal_error('base-distribution `{0}` is unknown, please add a `{0}.py` file next your menu.'.format(name))

self.distro = distros[name]

# Update distro if custom distro is defined in menu
self.update_override_distro()


class CookerCall:
"""
Expand Down

0 comments on commit c710ce3

Please sign in to comment.