Skip to content

Commit 7eba313

Browse files
committed
adding merge script (untested)
1 parent 8f7c4be commit 7eba313

2 files changed

Lines changed: 118 additions & 11 deletions

File tree

utils/merge_yaml.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import yaml
2+
3+
import sys
4+
from pathlib import Path
5+
6+
# Add parent directory to sys.path
7+
parent = Path(__file__).resolve().parent.parent
8+
sys.path.insert(0, str(parent))
9+
10+
from .validate_yaml import read_data, validate_data, PROBLEMS_FILE
11+
12+
TEMPLATE_FILE = "template.yaml"
13+
14+
15+
def validity_check(old_data, new_data):
16+
# Check there are not duplicate keys
17+
duplicates = len(set(old_data[0].keys()) & set(new_data[0].keys())) > 0
18+
if duplicates:
19+
print(
20+
"::error::Duplicate problem names found between existing and new problems."
21+
)
22+
return False
23+
24+
# Validate new data
25+
is_valid = validate_data(new_data)
26+
if not is_valid:
27+
print("::error::New problems YAML validation failed")
28+
return False
29+
30+
return True
31+
32+
33+
def write_data(filepath, data):
34+
try:
35+
with open(filepath, "w") as f:
36+
yaml.safe_dump(data, f)
37+
print(f"::notice::Wrote data to {filepath}.")
38+
except FileNotFoundError:
39+
print(f"::error::File not found: {filepath}")
40+
return False
41+
except yaml.YAMLError as e:
42+
print(f"::error::YAML syntax error: {e}")
43+
return False
44+
return True
45+
46+
47+
def update_existing_data(existing_data, new_data):
48+
existing_data.update(new_data)
49+
50+
write_success = write_data(PROBLEMS_FILE, existing_data)
51+
return write_success
52+
53+
54+
def reset_to_template(file_path):
55+
# Reset the content of the file to a template
56+
template_data_status, template_data = read_data(TEMPLATE_FILE)
57+
if template_data_status != 0:
58+
return template_data_status
59+
write_success = write_data(file_path, template_data)
60+
return write_success
61+
62+
63+
def merge_new_problems(new_problems_yaml_path: str):
64+
existing_data_status, existing_data = read_data(PROBLEMS_FILE)
65+
if not existing_data_status:
66+
return False
67+
new_data_status, new_data = read_data(new_problems_yaml_path)
68+
if not new_data_status:
69+
return False
70+
# Validate data
71+
is_valid = validity_check(existing_data, new_data)
72+
if not is_valid:
73+
return False
74+
75+
# All valid, we can now just merge the dicts
76+
assert existing_data is not None
77+
assert new_data is not None
78+
updated = update_existing_data(existing_data, new_data)
79+
if not updated:
80+
return False
81+
82+
# Reset the template content
83+
reset_status = reset_to_template(new_problems_yaml_path)
84+
if not reset_status:
85+
return False
86+
87+
print(f"::notice::Merged {len(new_data)} new problems into {PROBLEMS_FILE}.")
88+
return True
89+
90+
91+
if __name__ == "__main__":
92+
if len(sys.argv) != 2:
93+
print("Usage: python merge_yaml.py <new_problems.yaml>")
94+
sys.exit(1)
95+
new_problems_yaml_path = sys.argv[1]
96+
status = merge_new_problems(new_problems_yaml_path)
97+
if not status:
98+
sys.exit(1)
99+
else:
100+
sys.exit(0)

utils/validate_yaml.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,36 @@ def check_novelty(data):
9393
return True
9494

9595

96-
def validate_yaml(filepath):
97-
status, data = read_data(filepath)
98-
if status != 0:
99-
sys.exit(1)
96+
def validate_data(data) -> bool:
10097
if not check_format(data):
101-
sys.exit(1)
102-
assert data is not None
98+
return False
10399

104100
for i, new_data in enumerate(data): # Iterate through each top-level entry
105101
# Check required and unique fields
106102
if not check_fields(new_data) or not check_novelty(new_data):
107103
print(f"::error::Validation failed for entry {i+1}.")
108-
sys.exit(1)
104+
return False
109105

110106
# YAML is valid if we reach this point
111-
print("YAML syntax is valid.")
112-
sys.exit(0)
107+
print("::notice::YAML syntax is valid.")
108+
return True
109+
110+
111+
def validate_yaml(filepath: str) -> bool:
112+
status, data = read_data(filepath)
113+
if status != 0:
114+
return False
115+
return validate_data(data)
113116

114117

115118
if __name__ == "__main__":
116-
if len(sys.argv) < 2:
119+
if len(sys.argv) != 2:
117120
print("::error::Usage: python validate_yaml.py <yourfile.yaml>")
118121
sys.exit(1)
119122

120123
filepath = sys.argv[1]
121-
validate_yaml(filepath)
124+
valid = validate_yaml(filepath)
125+
if valid:
126+
sys.exit(0)
127+
else:
128+
sys.exit(1)

0 commit comments

Comments
 (0)