Coverage for src / bioimageio / core / utils / _compare.py: 84%
19 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-30 13:23 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-30 13:23 +0000
1from difflib import HtmlDiff, unified_diff
2from typing import Sequence
4from loguru import logger
5from ruyaml import Optional
6from typing_extensions import Literal, assert_never
8from bioimageio.spec._internal.version_type import Version
11def compare(
12 a: Sequence[str],
13 b: Sequence[str],
14 name_a: str = "source",
15 name_b: str = "updated",
16 *,
17 diff_format: Literal["unified", "html"],
18):
19 if diff_format == "html":
20 diff = HtmlDiff().make_file(a, b, name_a, name_b, charset="utf-8")
21 elif diff_format == "unified":
22 diff = "\n".join(
23 unified_diff(
24 a,
25 b,
26 name_a,
27 name_b,
28 lineterm="",
29 )
30 )
31 else:
32 assert_never(diff_format)
34 return diff
37def warn_about_version(
38 name: str, specified_version: Optional[Version], actual_version: Optional[Version]
39) -> None:
40 if actual_version is None or specified_version is None:
41 logger.warning("Could not compare actual and installed {} versions.", name)
42 elif actual_version < specified_version:
43 logger.warning(
44 "Installed {} version {} is lower than specified version {}.",
45 name,
46 actual_version,
47 specified_version,
48 )
49 elif (specified_version.major, specified_version.minor) != (
50 actual_version.major,
51 actual_version.minor,
52 ):
53 logger.warning(
54 "Installed {} version {} does not match specified {}.",
55 name,
56 actual_version,
57 specified_version,
58 )