Skip to content

Instantly share code, notes, and snippets.

@dzianisv
Created September 13, 2023 16:58
Show Gist options
  • Select an option

  • Save dzianisv/5e3db1d9d751bbe9e46eeefd578607bb to your computer and use it in GitHub Desktop.

Select an option

Save dzianisv/5e3db1d9d751bbe9e46eeefd578607bb to your computer and use it in GitHub Desktop.
compare-toml
import sys
import re
def preprocess(file):
with open(file, 'r') as f:
data = f.readlines()
return data
def compare_toml(old, new):
data1 = preprocess(old)
data2 = preprocess(new)
old_dict = {}
new_dict = {}
# parse lines into key-value pairs
for line in data1:
if re.match(r'^{{.*}}$', line.strip()): # ignore lines starting with {{ and ending with }}
continue
else:
line = line.strip().split("=")
if len(line) == 2: # if the line is a key-value pair
key, value = line[0].strip(), line[1].strip()
old_dict[key] = value
for line in data2:
if re.match(r'^{{.*}}$', line.strip()): # ignore lines starting with {{ and ending with }}
continue
else:
line = line.strip().split("=")
if len(line) == 2: # if the line is a key-value pair
key, value = line[0].strip(), line[1].strip()
new_dict[key] = value
# compare dictionaries
if old_dict == new_dict:
print("The TOML files are identical.")
else:
print("The TOML files are different.")
for key in set(old_dict.keys()).intersection(new_dict.keys()):
if key not in old_dict:
print(f"New key added: {key}")
return False
if old_dict[key] != new_dict[key]:
print(f"{key}: {old_dict[key]} != {new_dict[key]}")
return True
if len(sys.argv) != 3:
print("Usage: compare-toml-configs <our config> <new release config>")
sys.exit(1)
sys.exit(0 if compare_toml(sys.argv[1], sys.argv[2]) else 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment