|
| 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) |
0 commit comments