171 lines
8.4 KiB
Python
171 lines
8.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team
|
|
(peppermintosteam@proton.me)
|
|
|
|
SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
This module provides a function to install a specified desktop environment and enable
|
|
associated services within a chroot environment. It utilizes YAML configuration files
|
|
to determine the packages and services required for each desktop environment.
|
|
|
|
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 os
|
|
import sys
|
|
import logging
|
|
import yaml
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
try:
|
|
from builder.core import xbps_commands
|
|
from builder.core.command_runner import run_command
|
|
from builder.core.enable_services import enable_services_in_chroot
|
|
from builder.core.bootstrap.paths import paths
|
|
from builder.core.xbps_commands import xbps_commands
|
|
from builder.core.config_loader import load_yaml_config
|
|
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('install_desktop')
|
|
|
|
|
|
def install_desktop_environment(arch, desktop_environment_name, desktops_config, target_env='rootfs'):
|
|
"""
|
|
Installs the specified desktop environment and enables associated services in the chroot system,
|
|
using the desktop configuration from the YAML.
|
|
|
|
Args:
|
|
arch (str): Architecture (e.g., 'x86_64').
|
|
desktop_environment_name (str): Name of the desktop environment (e.g., 'xfce', 'mate', 'gnome', 'kde').
|
|
desktops_config (dict): Desktop configuration loaded from the YAML (desktops_config.yaml file).
|
|
target_env (str): Target environment to install the desktop ('rootfs', 'pep-host', 'pep-target').
|
|
Defaults to 'rootfs'. Note: Desktop environments are typically installed only in 'rootfs'.
|
|
"""
|
|
logger.info(f"=> Starting installation of desktop environment: {desktop_environment_name} in environment: {target_env}...")
|
|
logger.info(f"=> Installing desktop environment: {desktop_environment_name} ({target_env})")
|
|
|
|
if target_env == 'rootfs':
|
|
target_path = paths['ROOTFS']
|
|
else:
|
|
logger.error(f"Invalid target environment for desktop installation: {target_env}. Installing in ROOTFS by default.")
|
|
target_path = paths['ROOTFS']
|
|
target_env = 'rootfs'
|
|
|
|
desktop_packages_group = None
|
|
desktop_yaml_file = None
|
|
total_desktop_packages_list = []
|
|
services_to_enable_list = []
|
|
|
|
repos_yaml_file = paths['REPOS_YAML_FILE']
|
|
try:
|
|
with open(repos_yaml_file, 'r') as f:
|
|
repo_data = yaml.safe_load(f)
|
|
repositories = repo_data.get('repositories', [])
|
|
except FileNotFoundError:
|
|
logger.error(f"Repository YAML file not found: {repos_yaml_file}")
|
|
return
|
|
except yaml.YAMLError as e:
|
|
logger.error(f"Error reading the repository YAML file: {repos_yaml_file} - {e}")
|
|
return
|
|
|
|
if desktops_config and 'desktops' in desktops_config and desktop_environment_name.lower() in desktops_config['desktops']:
|
|
desktop_info = desktops_config['desktops'][desktop_environment_name.lower()]
|
|
desktop_packages_group = desktop_info.get('package_group')
|
|
desktop_yaml_file = desktop_info.get('yaml_file')
|
|
lista_pacotes_totais_desktop = []
|
|
services_to_enable_list = []
|
|
|
|
if desktop_yaml_file:
|
|
logger.info(f"=> Ficheiro YAML de desktop especificado em desktops_config.yaml: {desktop_yaml_file}")
|
|
desktop_yaml_full_path = os.path.join(BASE_DIR, desktop_yaml_file)
|
|
desktop_packages_config = load_yaml_config(desktop_yaml_full_path, os.path.basename(desktop_yaml_full_path))
|
|
if desktop_packages_config:
|
|
logger.debug(f"=> Conteúdo do ficheiro de desktop YAML carregado com sucesso de: {desktop_yaml_full_path}")
|
|
desktop_env_config = desktop_packages_config.get('desktop_environment', {})
|
|
chaves_pacotes_desktop = [
|
|
'desktop_packages',
|
|
'login_manager_packages',
|
|
'default_packages',
|
|
'services_enable'
|
|
]
|
|
for chave in chaves_pacotes_desktop:
|
|
if chave in desktop_env_config:
|
|
valor_chave = desktop_env_config[chave]
|
|
if chave in ['desktop_packages', 'login_manager_packages', 'default_packages']:
|
|
if isinstance(valor_chave, list):
|
|
total_desktop_packages_list.extend(valor_chave)
|
|
else:
|
|
logger.warning(f"Chave '{chave}' em {desktop_yaml_file} não contém uma lista de pacotes. Ignorando.")
|
|
elif chave == 'services_enable':
|
|
if isinstance(valor_chave, list):
|
|
services_to_enable_list.extend(valor_chave)
|
|
else:
|
|
logger.warning(f"Chave '{chave}' em {desktop_yaml_file} não contém uma lista de serviços. Ignorando.")
|
|
else:
|
|
logger.warning(f"Chave '{chave}' não encontrada em {desktop_yaml_file}. Ignorando.")
|
|
|
|
if not total_desktop_packages_list:
|
|
logger.warning(f"Nenhuma lista de pacotes de desktop encontrada no YAML: {desktop_yaml_file}. A prosseguir sem install pacotes de desktop (YAML).")
|
|
else:
|
|
logger.error(f"=> Falha ao carregar a configuração do desktop YAML de: {desktop_yaml_full_path}")
|
|
desktop_packages_group = None
|
|
|
|
if total_desktop_packages_list:
|
|
try:
|
|
logger.info(f"=> Desktop YAML package list extracted successfully. Building XBPS_INSTALL_CMD command...")
|
|
|
|
cache_dir = os.path.join(paths["ISO_CACHE_DIR"], arch)
|
|
|
|
xbps_install_desktop_command = [
|
|
xbps_commands["XBPS_INSTALL_CMD"], '-S', '-y',
|
|
'-r', target_path,
|
|
'-c', cache_dir,
|
|
]
|
|
|
|
repo_args = ""
|
|
for repo_dict in repositories:
|
|
repo_url = repo_dict['uri']
|
|
repo_args += f"-R {repo_url} "
|
|
repo_urls = repo_args.strip().split(" ")
|
|
xbps_install_desktop_command.extend(repo_urls)
|
|
|
|
xbps_install_desktop_command.extend(total_desktop_packages_list)
|
|
|
|
command_str_desktop = " ".join(xbps_install_desktop_command)
|
|
logger.info(f"=> XBPS_INSTALL_CMD command generated for desktop packages (YAML) with cache: {command_str_desktop}")
|
|
|
|
logger.info(f"=> Executing XBPS_INSTALL_CMD for desktop packages (YAML) [TARGETED AT: {target_path}] (Environment: {target_env})")
|
|
run_command(xbps_install_desktop_command)
|
|
logger.info(f"=> Installation of desktop packages (YAML) completed successfully in environment: {target_env}.")
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error executing XBPS_INSTALL_CMD for desktop packages (YAML) in environment: {target_env}: {e}")
|
|
raise
|
|
elif desktop_packages_group:
|
|
logger.info(f"=> Desktop packages already installed using package group (fallback). Skipping YAML installation.")
|
|
else:
|
|
logger.warning(f"=> Neither YAML package list nor desktop package group found. No desktop installation performed.")
|
|
|
|
if services_to_enable_list:
|
|
logger.info("=> Services to enable found in the desktop YAML configuration.")
|
|
try:
|
|
enable_services_in_chroot(target_path, services_to_enable_list)
|
|
logger.info(f"=> Services specified in the desktop YAML enabled successfully in environment: {target_env}.")
|
|
except Exception as e:
|
|
logger.error(f"Error enabling services specified in the desktop YAML in environment: {target_env}: {e}")
|
|
raise
|
|
else:
|
|
logger.warning("=> No service list to enable found in the desktop YAML configuration. Skipping service activation.")
|
|
|
|
logger.info(f"=> Installation of desktop environment: {desktop_environment_name} ({target_env}) completed.")
|
|
logger.info(f"--------------------------------------------------------------------\n") |