# Assuming this is in builder/core/packages_install.py # -*- coding: utf-8 -*- """ SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team (peppermintosteam@proton.me) SPDX-License-Identifier: GPL-3.0-or-later This module is responsible for installing common packages into the root filesystem (rootfs) of the PeppermintOS build. It reads the list of packages to be installed from a YAML file and uses the XBPS package manager to perform the installation according to the host-execution strategy. Credits: - PeppermintOS Team (peppermintosteam@proton.me) - Development and maintenance of the project. License: This code is distributed under the GNU General Public License version 3 or later (GPL-3.0-or-later). For more details, please refer to the LICENSE file included in the project or visit: https://www.gnu.org/licenses/gpl-3.0.html """ import yaml import logging import sys import os # BASE_DIR and sys.path.insert might be needed at the top of this file too if it's run standalone # BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # sys.path.insert(0, BASE_DIR) try: # Import modules used by this function from builder.core.command_runner import run_command # xbps_commands is likely not needed here as construct_xbps_install_args handles it # from builder.core.xbps_commands import xbps_commands from builder.configs import logger_config # paths is passed as argument, so not needed as direct import # from builder.core.bootstrap.paths import paths from builder.core.config_loader import load_yaml_config # Needed to load common packages YAML from builder.core import bootstrap # Import bootstrap module for construct_xbps_install_args logger = logger_config.setup_logger('packages_install') # Setup logger for this module except ImportError as e: # Handle import error for this module try: basic_logger = logging.getLogger(__name__) logging.basicConfig(level=logging.ERROR) basic_logger.error(f"Error importing necessary modules for packages_install: {e}. Ensure your environment is set up correctly.") except Exception: print(f"Error importing necessary modules for packages_install: {e}. Ensure your environment is set up correctly.") sys.exit(1) # MODIFIED FUNCTION SIGNATURE to accept 5 arguments, using names from the call in iso_builder_main # Note: The first argument name 'rootfs_path' is kept for clarity but the actual rootfs path # will be obtained from the 'paths' dictionary passed as the first argument. def install_common_packages_rootfs_yaml(paths, common_packages_yaml_path, target_architecture, host_architecture, repositories_data): """ Installs common packages into the rootfs from a YAML file, using the host-execution strategy. Args: paths (dict): Dictionary of build paths. Required to get rootfs path and cache dir. common_packages_yaml_path (str): Path to the YAML file containing the common package lists. Expected structure: {: [package list]} target_architecture (str): The target architecture (e.g., 'aarch64'). host_architecture (str): The architecture of the host system. repositories_data (list): List of repository dictionaries from YAML config. Expected structure: [{'name': '...', 'uri': '...', 'architectures': [...]}, ...] Raises: subprocess.CalledProcessError: If the xbps-install command fails. ValueError: If configuration data is missing or incorrect. KeyError: If required paths are not found. """ # Re-initialize logger if needed, or rely on module-level logger # logger = logging.getLogger(__name__) # Remove this line if logger is setup at module level logger.info(f"=> Installing common packages into ROOTFS ({target_architecture}) using YAML: {common_packages_yaml_path}") # Ensure paths dict is available if not isinstance(paths, dict): logger.error("Paths dictionary is missing or incorrect.") raise ValueError("Paths dictionary is required.") # Get the actual rootfs path from the paths dictionary rootfs_path = paths.get("ROOTFS") if not rootfs_path: logger.error(f"Path for environment 'rootfs' not found in paths dictionary.") raise KeyError(f"Path for environment 'rootfs' not found.") # Load common packages configuration from the specified YAML path # Assumes common_packages_yaml_path points to a YAML structured like {arch: [package list]} try: common_packages_config = load_yaml_config(common_packages_yaml_path, os.path.basename(common_packages_yaml_path)) except Exception as e: # load_yaml_config should handle FileNotFoundError/YAMLError logger.error(f"Error loading common packages YAML '{common_packages_yaml_path}': {e}") raise # Re-raise the exception # Get the list of common packages specifically for the target architecture # The YAML structure is expected to be {: [package1, package2, ...]} common_packages_for_arch = common_packages_config.get(target_architecture, []) if not common_packages_for_arch: logger.warning(f"No common packages defined for architecture {target_architecture} in {common_packages_yaml_path}. Skipping installation.") return # Exit the function # Construct the xbps-install command arguments using the helper from bootstrap module # construct_xbps_install_args expects the package list in the structure {'env_name': {'arch': [package_list]}} # So, we adapt the common package list for the target architecture into this structure temporarily. # We pass "rootfs" as env_name because we are installing into the rootfs environment. adapted_package_structure = {"rootfs": {target_architecture: common_packages_for_arch}} # construct_xbps_install_args handles repository filtering, -r, -c args internally # It needs paths, target_architecture, repositories_data, and the adapted package structure common_packages_args = bootstrap.construct_xbps_install_args( "rootfs", # Environment name for paths/package lookup within the structure paths, # Paths dictionary (needed by construct_xbps_install_args for rootfs path and cache) target_architecture, # Target architecture repositories_data, # All repository data adapted_package_structure # Pass common packages in the structure expected by construct_xbps_install_args ) if not common_packages_args: logger.warning("Skipping common packages installation due to failure in constructing xbps-install arguments.") # The error was likely logged within construct_xbps_install_args return # Exit the function # Execute the xbps-install command directly on the host, targeting the rootfs # Prepend the xbps-install binary path on the host common_install_command = ["/usr/bin/xbps-install"] + common_packages_args logger.info(f"Executing common packages installation command (host): {' '.join(common_install_command)}") try: run_command(common_install_command) # Use the existing run_command helper logger.info(f"=> Installation of common packages into ROOTFS completed.") except Exception as e: # Catching general Exception, consider more specific CalledProcessError logger.error(f"Error executing XBPS_INSTALL_CMD for common packages: {e}") raise # Re-raise the exception to stop the build # Note: The hardcoded package_categories list and the loop over it are removed # as the packages are now expected to be listed directly under the architecture key in the YAML. # The loading of repos_yaml_file inside this function is removed as repositories_data is passed.