From 0eb3a78332bc83f82527ab61adef850cb45fbb75 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Tue, 18 Nov 2025 08:31:17 +0100 Subject: [PATCH] Enhance configuration file installation process - Implement a CMake script for intelligent merging of configuration files, ensuring only missing keys are added without overwriting existing ones. - Install a template configuration file as an example, preventing overwriting of the original during installation. --- CMakeLists.txt | 9 ++- cmake/install-config.cmake | 145 +++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 cmake/install-config.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 67e5ba0..b9afb4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,4 +109,11 @@ target_link_libraries(yourpart-daemon PRIVATE # Installation rules install(TARGETS yourpart-daemon DESTINATION /usr/local/bin) -install(FILES daemon.conf DESTINATION /etc/yourpart/) + +# Intelligente Konfigurationsdatei-Installation +# Verwendet ein CMake-Skript, das nur fehlende Keys hinzufügt, ohne bestehende zu überschreiben +# Das Skript liest das Template aus dem Source-Verzeichnis und merged es intelligent +install(SCRIPT cmake/install-config.cmake) + +# Installiere Template als Referenz (optional, wird nicht überschrieben) +install(FILES daemon.conf DESTINATION /etc/yourpart/ RENAME daemon.conf.example) diff --git a/cmake/install-config.cmake b/cmake/install-config.cmake new file mode 100644 index 0000000..87323ae --- /dev/null +++ b/cmake/install-config.cmake @@ -0,0 +1,145 @@ +# CMake-Skript für intelligente Konfigurationsdatei-Installation +# Fügt nur fehlende Keys hinzu, ohne bestehende Konfiguration zu überschreiben + +# Pfade setzen +set(CONFIG_FILE "/etc/yourpart/daemon.conf") +# Während der Installation ist CMAKE_SOURCE_DIR das Projekt-Root-Verzeichnis +# Falls nicht verfügbar, versuchen wir es über CMAKE_CURRENT_LIST_DIR +if(DEFINED CMAKE_SOURCE_DIR) + set(TEMPLATE_FILE "${CMAKE_SOURCE_DIR}/daemon.conf") +else() + get_filename_component(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE) + set(TEMPLATE_FILE "${PROJECT_ROOT}/daemon.conf") +endif() + +# Prüfe ob Template existiert +if(NOT EXISTS "${TEMPLATE_FILE}") + message(FATAL_ERROR "Template-Datei ${TEMPLATE_FILE} nicht gefunden!") +endif() + +# Prüfe ob Ziel-Verzeichnis existiert +if(NOT EXISTS "/etc/yourpart") + message(STATUS "Erstelle Verzeichnis /etc/yourpart...") + execute_process( + COMMAND ${CMAKE_COMMAND} -E make_directory "/etc/yourpart" + COMMAND_ERROR_IS_FATAL ANY + ) +endif() + +# Prüfe ob Config-Datei existiert +if(NOT EXISTS "${CONFIG_FILE}") + message(STATUS "Konfigurationsdatei existiert nicht, erstelle neue...") + execute_process( + COMMAND ${CMAKE_COMMAND} -E copy "${TEMPLATE_FILE}" "${CONFIG_FILE}" + COMMAND_ERROR_IS_FATAL ANY + ) + message(STATUS "Neue Konfigurationsdatei erstellt: ${CONFIG_FILE}") +else() + message(STATUS "Konfigurationsdatei existiert bereits, prüfe auf fehlende Keys...") + + # Verwende ein Python-Skript für intelligentes Merging + # (CMake hat keine gute Unterstützung für komplexe String-Manipulation) + # Erstelle temporäres Python-Skript im Build-Verzeichnis + set(MERGE_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/merge-config.py") + + # Erstelle Python-Skript + file(WRITE "${MERGE_SCRIPT}" +"#!/usr/bin/env python3 +import sys +import re +import os + +def merge_config(template_file, config_file): + \"\"\"Fügt fehlende Keys aus Template zur Config hinzu, ohne bestehende zu überschreiben\"\"\" + + # Lese bestehende Config + existing_keys = {} + existing_lines = [] + if os.path.exists(config_file): + with open(config_file, 'r') as f: + for line in f: + existing_lines.append(line.rstrip()) + # Extrahiere Key=Value Paare + match = re.match(r'^\\s*([^#=]+?)\\s*=\\s*(.+?)\\s*$', line) + if match: + key = match.group(1).strip() + value = match.group(2).strip() + existing_keys[key] = value + + # Lese Template + new_keys = {} + if not os.path.exists(template_file): + print(f'Fehler: Template-Datei {template_file} nicht gefunden!', file=sys.stderr) + return False + + with open(template_file, 'r') as f: + for line in f: + # Extrahiere Key=Value Paare + match = re.match(r'^\\s*([^#=]+?)\\s*=\\s*(.+?)\\s*$', line) + if match: + key = match.group(1).strip() + value = match.group(2).strip() + new_keys[key] = value + + # Füge fehlende Keys hinzu + added_count = 0 + for key, value in new_keys.items(): + if key not in existing_keys: + existing_lines.append(f'{key}={value}') + print(f'Füge fehlenden Key hinzu: {key}') + added_count += 1 + + # Schreibe aktualisierte Config + if added_count > 0: + with open(config_file, 'w') as f: + for line in existing_lines: + f.write(line + '\\n') + print(f'{added_count} neue Keys hinzugefügt') + else: + print('Keine neuen Keys hinzugefügt - Konfiguration ist aktuell') + + return True + +if __name__ == '__main__': + if len(sys.argv) != 3: + print('Verwendung: merge-config.py