blob: 9c00ee1e38871f800149bea5a3af14dbe5a6a700 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import os
import sys
import re
def update_optiscaler_config(file_path):
if not os.path.exists(file_path):
print(f"Error: File '{file_path}' not found.")
return
with open(file_path, 'r') as f:
lines = f.readlines()
# Get environment variables starting with OptiScaler_
env_vars = {k: v for k, v in os.environ.items() if k.startswith("OptiScaler_")}
for env_name, env_value in env_vars.items():
# Split OptiScaler_Section_Key
parts = env_name.split('_', 2)
if len(parts) < 3:
continue
section_target = parts[1]
key_target = parts[2]
found_section = False
updated_key = False
# Regex to match [Section] and Key=Value
section_pattern = re.compile(rf'^\s*\[{re.escape(section_target)}\]\s*')
key_pattern = re.compile(rf'^(\s*{re.escape(key_target)}\s*)=.*')
for i, line in enumerate(lines):
# Track if we are inside the correct section
if section_pattern.match(line):
found_section = True
continue
# If we hit a new section before finding the key, the key doesn't exist in the target section
if found_section and line.strip().startswith('[') and not section_pattern.match(line):
break
# Replace the value if the key is found within the correct section
if found_section and key_pattern.match(line):
lines[i] = key_pattern.sub(r'\1=' + env_value, line)
updated_key = True
print(f"Updated: [{section_target}] {key_target} = {env_value}")
break
# If section exists but key doesn't, append key to the end of the section
# Not really needed but supported just in case
if found_section and not updated_key:
for i, line in enumerate(lines):
if section_pattern.match(line):
lines.insert(i + 1, f"{key_target}={env_value}\n")
print(f"Added new key: [{section_target}] {key_target} = {env_value}")
break
# Write the modified content back
with open(file_path, 'w') as f:
f.writelines(lines)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python update-optiscaler-config.py <path_to_ini>")
else:
update_optiscaler_config(sys.argv[1])
|