migration ini

This commit is contained in:
qiaofeng1227 2023-10-27 16:31:14 +08:00
parent 0e09042023
commit 01aecb500b
2 changed files with 14 additions and 25 deletions

View file

@ -1,4 +1,4 @@
# modify time: 2023102712, you can modify here to trigger Docker Build action
# modify time: 202310271640, you can modify here to trigger Docker Build action
FROM python:3.10-bullseye AS buildstage
LABEL maintainer="Websoft9<help@websoft9.com>"

View file

@ -1,5 +1,3 @@
#!/bin/bash
echo "start to migrate config.ini"
migrate_ini() {
@ -8,7 +6,7 @@ migrate_ini() {
local target_ini="$1"
local template_ini="$2"
/usr/local/bin/python3 - <<EOF
/usr/local/bin/python3 - <<EOF
import configparser
import os
import sys
@ -17,14 +15,8 @@ import sys
target_parser = configparser.ConfigParser()
template_parser = configparser.ConfigParser()
# Check if the files exist and are not empty
if not os.path.isfile(target_ini) or os.stat(target_ini).st_size == 0:
print(f"Error: {target_ini} does not exist or is empty.")
sys.exit(1)
if not os.path.isfile(template_ini) or os.stat(template_ini).st_size == 0:
print(f"Error: {template_ini} does not exist or is empty.")
sys.exit(1)
target_ini = os.environ['TARGET_INI']
template_ini = os.environ['TEMPLATE_INI']
try:
target_parser.read(target_ini)
@ -33,25 +25,22 @@ except configparser.MissingSectionHeaderError:
print("Error: The provided files are not valid INI files.")
sys.exit(1)
# Traverse each section and property in template_ini
for section in template_parser.sections():
if not target_parser.has_section(section):
# If the section does not exist in target_ini, add the whole section from template_ini
target_parser.add_section(section)
for key, value in template_parser.items(section):
target_parser.set(section, key, value)
else:
# If the section exists in target_ini, only add the keys that don't exist in target_ini
for key, value in template_parser.items(section):
if not target_parser.has_option(section, key):
target_parser.set(section, key, value)
# use target_parser to override template_parser
for section in target_parser.sections():
if template_parser.has_section(section):
for key, value in target_parser.items(section):
if template_parser.has_option(section, key):
template_parser.set(section, key, value)
with open(target_ini, 'w') as f:
template_parser.write(f)
EOF
}
migrate_ini "/websoft9/apphub/src/config/config.ini" "/websoft9/config/config.ini"
export TARGET_INI="/websoft9/apphub/src/config/config.ini"
export TEMPLATE_INI="/websoft9/config/config.ini"
migrate_ini "$TARGET_INI" "$TEMPLATE_INI"
if [ $? -eq 0 ]; then
echo "Success to update config.ini"