# -*- coding: utf-8 -*- """ SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team (peppermintosteam@proton.me) SPDX-License-Identifier: GPL-3.0-or-later This module provides functions to load YAML configuration files used in the PeppermintOS build process. It includes functions to load a single YAML file and to load all common configuration files. """ import yaml import logging import os import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, BASE_DIR) try: from builder.core.bootstrap.paths import paths from builder.configs import logger_config except ImportError as e: print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly.") sys.exit(1) logger = logger_config.setup_logger('config_loader') def load_yaml_config(yaml_file_path, log_filename): """Loads a YAML configuration file.""" try: with open(yaml_file_path, 'r') as yaml_file: config = yaml.safe_load(yaml_file) logger.debug(f"YAML configuration loaded from: {yaml_file_path}") return config except FileNotFoundError: logger.error(f"YAML configuration file not found: {yaml_file_path}") return None except yaml.YAMLError as e: logger.error(f"Error reading YAML file: {yaml_file_path} - {e}") return None def load_all_configurations(architecture, config_file_path=None): """Loads all YAML configuration files.""" logger.info("=> Loading YAML configuration files...") configs = {} # Arquitetures architectures_config_path = paths["ARCHITECTURES_CONFIG_YAML"] configs['architectures'] = load_yaml_config(architectures_config_path, "architectures_config.yaml") if configs['architectures'] is None: logger.error(f"=> Failed to load architectures config: {architectures_config_path}") return None # Desktops desktops_config_path = paths["DESKTOPS_CONFIG_YAML"] desktops_config_raw = load_yaml_config(desktops_config_path, "desktops_config.yaml") if desktops_config_raw is None: logger.error(f"=> Failed to load desktops config: {desktops_config_path}") return None desktops = desktops_config_raw.get("desktops") if not desktops: logger.error("=> No desktops found in desktops_config.yaml!") return None configs['desktops'] = {'desktops': desktops} # Check if the specified desktop is in the keychain for desktop_id in configs['desktops']: if desktop_id not in configs['desktops']: logger.error(f"=> Desktop '{desktop_id}' not found!") return None # Kernels kernels_config_path = paths["KERNELS_CONFIG_YAML"] configs['kernels'] = load_yaml_config(kernels_config_path, "kernels_config.yaml") if configs['kernels'] is None: logger.error(f"=> Failed to load kernels config: {kernels_config_path}") return None # Common packages common_packages_config_path = paths["COMMON_PACKAGES_YAML"] configs['common_packages'] = load_yaml_config(common_packages_config_path, "common_packages.yaml") if configs['common_packages'] is None: logger.error(f"=> Failed to load common packages config: {common_packages_config_path}") return None # ISO config if config_file_path: iso_config_path = config_file_path log_filename = os.path.basename(config_file_path) else: config_file_name = f"iso_build_config_{architecture}.yaml" iso_config_path = os.path.join(paths["ISO_BUILD_CONFIG_YAML"], config_file_name) log_filename = config_file_name configs['iso_config'] = load_yaml_config(iso_config_path, log_filename) if configs['iso_config'] is None: logger.error(f"=> Failed to load ISO build config: {iso_config_path}") return None logger.info("=> YAML configuration files loaded successfully.") return configs