# 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