|
| 1 | +""" |
| 2 | +This module provides functions to compare the BO4E JSON schemas of different versions. |
| 3 | +It also contains functions to query GitHub for the latest BO4E versions to compare with the schemas of the current |
| 4 | +work tree. |
| 5 | +Additionally, it implements a little cache functionality to avoid multiple downloads of the same versions e.g. |
| 6 | +if you're testing locally. |
| 7 | +""" |
| 8 | + |
| 9 | +import itertools |
| 10 | +import logging |
| 11 | +import re |
| 12 | +import shutil |
| 13 | +from pathlib import Path |
| 14 | +from typing import Any as _Any |
| 15 | +from typing import Iterable |
| 16 | + |
| 17 | +import bost.operations |
| 18 | +from bost import main as bost_main |
| 19 | +from bost.operations import update_references as bost_update_references |
| 20 | +from bost.pull import OWNER, REPO, SchemaMetadata, get_source_repo |
| 21 | + |
| 22 | +from . import change_schemas, diff, loader, matrix |
| 23 | + |
| 24 | +BO4E_BASE_DIR = Path(__file__).parents[2] / "tmp/bo4e_json_schemas" |
| 25 | +LOCAL_JSON_SCHEMA_DIR = Path(__file__).parents[2] / "json_schemas" |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def pull_bo4e_version(version: str, output: Path, gh_token: str | None = None) -> None: |
| 30 | + """ |
| 31 | + Pull the BO4E version from the given version string. |
| 32 | + """ |
| 33 | + bost_main( |
| 34 | + output=output, |
| 35 | + target_version=version, |
| 36 | + update_refs=True, |
| 37 | + set_default_version=False, |
| 38 | + clear_output=True, |
| 39 | + token=gh_token, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def update_references(path: Path, version: str) -> None: |
| 44 | + """ |
| 45 | + Update the references in the given path. This step is needed for the local build. |
| 46 | + """ |
| 47 | + schema_namespace = {} |
| 48 | + for schema_path in loader.get_namespace(path): |
| 49 | + local_path = Path(path, *schema_path).with_suffix(".json") |
| 50 | + schema_namespace[schema_path[-1]] = SchemaMetadata( |
| 51 | + class_name=schema_path[-1], |
| 52 | + download_url="", |
| 53 | + module_path=schema_path, |
| 54 | + file_path=local_path, |
| 55 | + cached_path=local_path, |
| 56 | + token=None, |
| 57 | + ) |
| 58 | + for schema_metadata in schema_namespace.values(): |
| 59 | + bost_update_references(schema_metadata, schema_namespace, version) |
| 60 | + schema_metadata.save() |
| 61 | + |
| 62 | + |
| 63 | +def pull_or_reuse_bo4e_version(version: str, gh_token: str | None = None, from_local: bool = False) -> Path: |
| 64 | + """ |
| 65 | + Pull the BO4E version from the given version string or reuse the version if it was already pulled before. |
| 66 | + If version is None use the BO4E version of the checkout working directory by assuming the compiled json |
| 67 | + schemas in /json_schemas. |
| 68 | + Returns the path of the bo4e directory. |
| 69 | + """ |
| 70 | + bo4e_dir = BO4E_BASE_DIR / version |
| 71 | + |
| 72 | + if from_local: |
| 73 | + if not any(LOCAL_JSON_SCHEMA_DIR.rglob("*.json")): |
| 74 | + raise ValueError( |
| 75 | + "No local json schemas found in /json_schemas. " |
| 76 | + "Please ensure that the json schemas are build on beforehand." |
| 77 | + ) |
| 78 | + if bo4e_dir.exists(): |
| 79 | + shutil.rmtree(bo4e_dir) |
| 80 | + shutil.copytree(LOCAL_JSON_SCHEMA_DIR, bo4e_dir) |
| 81 | + update_references(bo4e_dir, version) |
| 82 | + elif any(bo4e_dir.rglob("*.json")): |
| 83 | + return bo4e_dir |
| 84 | + else: |
| 85 | + pull_bo4e_version(version, bo4e_dir, gh_token) |
| 86 | + return bo4e_dir |
| 87 | + |
| 88 | + |
| 89 | +def compare_bo4e_versions( |
| 90 | + version_old: str, version_new: str, gh_token: str | None = None, from_local: bool = False |
| 91 | +) -> Iterable[change_schemas.Change]: |
| 92 | + """ |
| 93 | + Compare the old version with the new version. |
| 94 | + If version_new is None use the BO4E version of the checkout working directory by assuming the compiled json |
| 95 | + schemas in /json_schemas. |
| 96 | + """ |
| 97 | + dir_old_schemas = pull_or_reuse_bo4e_version(version_old, gh_token) |
| 98 | + dir_new_schemas = pull_or_reuse_bo4e_version(version_new, gh_token, from_local=from_local) |
| 99 | + print(f"Comparing {version_old} with {version_new}") |
| 100 | + yield from diff.diff_schemas(dir_old_schemas, dir_new_schemas) |
| 101 | + |
| 102 | + |
| 103 | +def compare_bo4e_versions_iteratively( |
| 104 | + versions: Iterable[str], cur_version: str | None = None, gh_token: str | None = None |
| 105 | +) -> dict[tuple[str, str], Iterable[change_schemas.Change]]: |
| 106 | + """ |
| 107 | + Compare the versions iteratively. Each version at index i will be compared to the version at index i+1. |
| 108 | + Additionally, if cur_version is provided, the last version in the list will be compared to the version |
| 109 | + in the checkout working directory. The value of cur_version will be used to set the key in the returned |
| 110 | + dict. |
| 111 | + Note: |
| 112 | + - versions must contain at least one element. |
| 113 | + - versions should be sorted in ascending order. |
| 114 | + - if using cur_version, ensure that the json schemas of the checkout working directory |
| 115 | + were build on beforehand. They should be located in /json_schemas. |
| 116 | + """ |
| 117 | + print(f"Comparing versions {versions} with cur_version {cur_version}") |
| 118 | + changes = {} |
| 119 | + last_version: str = "" # This value is never used but makes mypy and pylint happy |
| 120 | + for version_old, version_new in itertools.pairwise(versions): |
| 121 | + last_version = version_new |
| 122 | + changes[version_old, version_new] = compare_bo4e_versions(version_old, version_new, gh_token) |
| 123 | + if cur_version is not None: |
| 124 | + changes[last_version, cur_version] = compare_bo4e_versions(last_version, cur_version, gh_token, from_local=True) |
| 125 | + print("Comparisons finished.") |
| 126 | + return changes |
| 127 | + |
| 128 | + |
| 129 | +REGEX_RELEASE_VERSION = re.compile(r"^v(\d{6}\.\d+\.\d+)$") |
| 130 | +REGEX_RELEASE_CANDIDATE_VERSION = re.compile(r"^v(\d{6}\.\d+\.\d+)-rc\d+$") |
| 131 | + |
| 132 | + |
| 133 | +def get_last_n_release_versions(n: int, include_rc: bool = False, gh_token: str | None = None) -> Iterable[str]: |
| 134 | + """ |
| 135 | + Get the last n release versions from the BO4E repository. |
| 136 | + """ |
| 137 | + repo = get_source_repo(gh_token) |
| 138 | + releases = repo.get_releases() |
| 139 | + counter = 0 |
| 140 | + |
| 141 | + for release in releases: |
| 142 | + if not REGEX_RELEASE_VERSION.fullmatch(release.tag_name) and ( |
| 143 | + not include_rc or not REGEX_RELEASE_CANDIDATE_VERSION.fullmatch(release.tag_name) |
| 144 | + ): |
| 145 | + continue |
| 146 | + counter += 1 |
| 147 | + yield release.tag_name |
| 148 | + if counter >= n: |
| 149 | + return |
| 150 | + |
| 151 | + logger.warning("Only %d matching releases found. Returning all releases.", counter) |
| 152 | + |
| 153 | + |
| 154 | +def get_all_release_versions_since_20240100(include_rc: bool = False, gh_token: str | None = None) -> Iterable[str]: |
| 155 | + """ |
| 156 | + Get all release versions since v202401.0.0 from the BO4E repository. |
| 157 | + """ |
| 158 | + repo = get_source_repo(gh_token) |
| 159 | + releases = repo.get_releases() |
| 160 | + version_threshold = "v202401.0.0" |
| 161 | + |
| 162 | + for release in releases: |
| 163 | + if not REGEX_RELEASE_VERSION.fullmatch(release.tag_name) and ( |
| 164 | + not include_rc or not REGEX_RELEASE_CANDIDATE_VERSION.fullmatch(release.tag_name) |
| 165 | + ): |
| 166 | + continue |
| 167 | + yield release.tag_name |
| 168 | + if release.tag_name == version_threshold: |
| 169 | + return |
| 170 | + |
| 171 | + logger.warning("Threshold version %s not found. Returned all matching releases.", version_threshold) |
| 172 | + |
| 173 | + |
| 174 | +def _monkey_patch_bost_regex_if_local_testing(version: str) -> None: |
| 175 | + regex_expected_version = re.compile(r"^v\d+\.\d+\.\d+(?:-rc\d+)?$") |
| 176 | + if not regex_expected_version.fullmatch(version): |
| 177 | + bost.operations.REF_ONLINE_REGEX = re.compile( |
| 178 | + rf"^https://raw\.githubusercontent\.com/(?:{OWNER.upper()}|{OWNER.lower()}|Hochfrequenz)/{REPO}/" |
| 179 | + rf"(?P<version>[^/]+)/" |
| 180 | + r"src/bo4e_schemas/(?P<sub_path>(?:\w+/)*)(?P<model>\w+)\.json#?$" |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +def create_tables_for_doc( |
| 185 | + compatibility_matrix_output_file: Path, |
| 186 | + gh_version: str, |
| 187 | + *, |
| 188 | + gh_token: str | None = None, |
| 189 | + last_n_versions: int = 2, |
| 190 | +) -> None: |
| 191 | + """ |
| 192 | + Creates the compatibility matrix for the documentation. The output is a csv file. This can be referenced |
| 193 | + inside Sphinx documentation. See https://sublime-and-sphinx-guide.readthedocs.io/en/latest/tables.html#csv-files |
| 194 | + for more information. |
| 195 | + If you have problems with rate limiting, please set gh_token. |
| 196 | + The compatibility matrix will be built for last_n_versions + the current version in the checkout working directory. |
| 197 | + If you set last_n_versions = 0 all versions since v202401.0.0 will be compared. |
| 198 | + Note: The matrix will never contain the first version as column. Each column is a comparison to the version before. |
| 199 | + Note: Release candidates are excluded. |
| 200 | + """ |
| 201 | + _monkey_patch_bost_regex_if_local_testing(gh_version) |
| 202 | + logger.info("Retrieving the last %d release versions", last_n_versions) |
| 203 | + if last_n_versions > 0: |
| 204 | + versions = list(reversed(list(get_last_n_release_versions(last_n_versions, gh_token=gh_token)))) |
| 205 | + else: |
| 206 | + versions = list(reversed(list(get_all_release_versions_since_20240100(gh_token=gh_token)))) |
| 207 | + logger.info("Comparing versions iteratively: %s", " -> ".join([*versions, gh_version])) |
| 208 | + changes_iterables = compare_bo4e_versions_iteratively(versions, gh_version, gh_token=gh_token) |
| 209 | + logger.info("Building namespaces") |
| 210 | + changes = {key: list(value) for key, value in changes_iterables.items()} |
| 211 | + namespaces = {version: list(loader.get_namespace(BO4E_BASE_DIR / version)) for version in versions} |
| 212 | + namespaces[gh_version] = list(loader.get_namespace(BO4E_BASE_DIR / gh_version)) |
| 213 | + logger.info("Creating compatibility matrix") |
| 214 | + matrix.create_compatibility_matrix_csv( |
| 215 | + compatibility_matrix_output_file, [*versions, gh_version], namespaces, changes |
| 216 | + ) |
| 217 | + |
| 218 | + |
| 219 | +def test_create_tables_for_doc() -> None: |
| 220 | + """ |
| 221 | + Test the create_tables_for_doc function locally without building the entire documentation. |
| 222 | + Needs the JSON schemas to be present in /json_schemas with TARGET_VERSION set to "local". |
| 223 | + """ |
| 224 | + create_tables_for_doc( |
| 225 | + Path(__file__).parents[1] / "compatibility_matrix.csv", |
| 226 | + "local", |
| 227 | + last_n_versions=3, |
| 228 | + ) |
0 commit comments