From 11e773aa71e8a2b6f9987535389727b89a060302 Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Wed, 17 Jul 2024 14:21:29 +0300 Subject: [PATCH] Merge pull request #1058 from ekatef/implement_config_versioning * Read the actual config version form config.default * Add an import * Add a check for the configs versions * Update a version in the config * Revert changes in a config version * Move a versions check into helpers * Revise naming * Revise an error message * Add a release note * Implement Davide's suggestion Co-authored-by: Davide Fioriti <67809479+davide-f@users.noreply.github.com> * Remove import --------- Co-authored-by: Davide Fioriti <67809479+davide-f@users.noreply.github.com> --- Snakefile | 6 +++++- doc/release_notes.rst | 2 ++ scripts/_helpers.py | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Snakefile b/Snakefile index 561be22ff..9610490a5 100644 --- a/Snakefile +++ b/Snakefile @@ -11,11 +11,12 @@ from shutil import copyfile, move from snakemake.remote.HTTP import RemoteProvider as HTTPRemoteProvider -from _helpers import create_country_list, get_last_commit_message +from _helpers import create_country_list, get_last_commit_message, check_config_version from build_demand_profiles import get_load_paths_gegis from retrieve_databundle_light import datafiles_retrivedatabundle from pathlib import Path + HTTP = HTTPRemoteProvider() if "config" not in globals() or not config: # skip when used as sub-workflow @@ -25,6 +26,9 @@ if "config" not in globals() or not config: # skip when used as sub-workflow configfile: "config.yaml" +check_config_version(config=config) + + configfile: "configs/bundle_config.yaml" diff --git a/doc/release_notes.rst b/doc/release_notes.rst index ba63c2955..acf736f5a 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -48,6 +48,8 @@ E.g. if a new rule becomes available describe how to use it `snakemake -j1 run_t * Create files where the code outputs the value of the objective function. `PR #1033 `__ +* Introduce versioning of the configuration files. `PR #1058 `__ + * Fix bug for hydro inflow normalization for gadm regions (alternative clustering). `PR #1057 `__ * Minor bug-fixing for s_nom_min. `PR #961 `__ diff --git a/scripts/_helpers.py b/scripts/_helpers.py index 4fdf000b9..2307e6d4a 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -27,6 +27,28 @@ REGIONS_CONFIG = "regions_definition_config.yaml" +def check_config_version(config, fp_config="config.default.yaml"): + """ + Check that a version of the local config.yaml matches to the actual config + version as defined in config.default.yaml. + """ + + # using snakemake capabilities to deal with yanl configs + with open(fp_config, "r") as f: + actual_config = yaml.safe_load(f) + actual_config_version = actual_config.get("version") + + current_config_version = config.get("version") + + if actual_config_version != current_config_version: + logger.error( + f"The current version of 'config.yaml' doesn't match to the code version:\n\r" + f" {current_config_version} provided, {actual_config_version} expected.\n\r" + f"That can lead to weird errors during execution of the workflow.\n\r" + f"Please update 'config.yaml' according to 'config.default.yaml' ." + ) + + def handle_exception(exc_type, exc_value, exc_traceback): """ Customise errors traceback.