-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerge.py
65 lines (50 loc) · 2.09 KB
/
merge.py
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
import sys
import io
import re
def main(args):
if len(args) < 3:
print("You must supply the input file and destination file.")
print("Ex: merge.py my-worldserver.conf worldserver.conf")
return
path_file_in = args[1]
path_file_out = args[2]
with open(path_file_in, "rt") as file_in:
with open(path_file_out, "rt") as file_out:
lines = file_in.readlines()
config_current = file_out.read()
line_number = 0
for line in lines:
line_number = line_number + 1
# Ignore empty lines
if not line.strip():
continue
if line.strip().startswith("#"):
continue
kvp = line.split('=')
if len(kvp) < 2:
print(f"Invalid key-value-pair found on line {line_number}.")
continue
key = line.split('=')[0].strip()
value = line.split('=')[1].strip()
if not key:
print(f"Invalid key (empty) found on line {line_number}.")
continue
if not value:
print(f"Invalid value (empty) found on line {line_number}.")
continue
pattern = f"{key}\s*=\s*.+"
if not re.search(pattern, config_current):
print(f"Key '{key}' not found, appending to '{path_file_out}'..")
config_current = f"{config_current}\n{key} = {value}"
continue
config_modified = re.sub(pattern, f"{key} = {value}", config_current)
if config_modified == config_current:
print(f"No changes were made for '{key}', value is up-to-date.")
continue
else:
print(f"Merging key '{key}' into '{path_file_out}'.")
config_current = config_modified
with open(path_file_out, "wt") as file_out:
file_out.write(config_current)
if __name__ == "__main__":
main(sys.argv)