- 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.
146 lines
4.8 KiB
CMake
146 lines
4.8 KiB
CMake
# 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 <template> <config>', file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
template_file = sys.argv[1]
|
|
config_file = sys.argv[2]
|
|
|
|
if not merge_config(template_file, config_file):
|
|
sys.exit(1)
|
|
")
|
|
|
|
# Setze Ausführungsrechte
|
|
file(CHMOD "${MERGE_SCRIPT}" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
|
|
|
# Führe Merge-Skript aus
|
|
execute_process(
|
|
COMMAND python3 "${MERGE_SCRIPT}" "${TEMPLATE_FILE}" "${CONFIG_FILE}"
|
|
RESULT_VARIABLE MERGE_RESULT
|
|
OUTPUT_VARIABLE MERGE_OUTPUT
|
|
ERROR_VARIABLE MERGE_ERROR
|
|
)
|
|
|
|
if(NOT MERGE_RESULT EQUAL 0)
|
|
message(WARNING "Fehler beim Mergen der Config: ${MERGE_ERROR}")
|
|
else()
|
|
message(STATUS "${MERGE_OUTPUT}")
|
|
endif()
|
|
endif()
|
|
|
|
# Setze korrekte Berechtigungen
|
|
execute_process(
|
|
COMMAND chown yourpart:yourpart "${CONFIG_FILE}"
|
|
COMMAND_ERROR_IS_FATAL FALSE
|
|
)
|
|
|
|
execute_process(
|
|
COMMAND chmod 600 "${CONFIG_FILE}"
|
|
COMMAND_ERROR_IS_FATAL FALSE
|
|
)
|
|
|
|
message(STATUS "Konfigurationsdatei-Verwaltung abgeschlossen: ${CONFIG_FILE}")
|
|
|