Option C Tools
Loading...
Searching...
No Matches
compat.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oct/core/compat.py
4
5"""
6Purpose
7-------
8Provide a version-compatibility check between OCT and the ``oc_diagnostics``
9runtime package.
10
11Responsibilities
12----------------
13- Define the supported ``oc_diagnostics`` version range.
14- Query the installed ``oc_diagnostics`` version via ``importlib.metadata``.
15- Return a human-readable warning or info string when the installed version
16 is out of range or absent; return ``None`` when compatible.
17
18Diagnostics
19-----------
20Domain: OCT-CORE
21Levels:
22 L2 — lifecycle
23 L3 — semantic details
24 L4 — deep tracing
25
26Contracts
27---------
28- Must not import ``oc_diagnostics`` at module level (avoids circular deps).
29- Must not require the ``packaging`` library; uses tuple-based comparison.
30- Must be safe to call when ``oc_diagnostics`` is not installed.
31"""
32
33from __future__ import annotations
34
35_OC_DIAGNOSTICS_COMPAT = {"min": "2.0.0", "max": "3.0.0"}
36
37
38def _parse_version_tuple(v: str) -> tuple[int, ...]:
39 """Convert a dotted version string to a tuple of ints for comparison."""
40 parts: list[int] = []
41 for segment in v.split("."):
42 digits = ""
43 for ch in segment:
44 if ch.isdigit():
45 digits += ch
46 else:
47 break
48 parts.append(int(digits) if digits else 0)
49 return tuple(parts)
50
51
52def check_oc_diagnostics_compat() -> str | None:
53 """
54 Check whether the installed ``oc_diagnostics`` version is within the
55 supported range.
56
57 Returns
58 -------
59 str or None
60 A warning/info message if out of range or not installed.
61 ``None`` if the version is compatible.
62 """
63 try:
64 from importlib.metadata import version as _pkg_version
65 installed = _pkg_version("oc_diagnostics")
66 except Exception:
67 return (
68 "Info: oc_diagnostics is not installed. "
69 "Projects created by OCT require oc_diagnostics at runtime."
70 )
71
72 try:
73 v = _parse_version_tuple(installed)
74 v_min = _parse_version_tuple(_OC_DIAGNOSTICS_COMPAT["min"])
75 v_max = _parse_version_tuple(_OC_DIAGNOSTICS_COMPAT["max"])
76 except (ValueError, TypeError):
77 return f"Warning: Could not parse oc_diagnostics version '{installed}'."
78
79 if v < v_min:
80 return (
81 f"Warning: oc_diagnostics {installed} is below the minimum "
82 f"supported version ({_OC_DIAGNOSTICS_COMPAT['min']}). "
83 f"Consider upgrading."
84 )
85
86 if v >= v_max:
87 return (
88 f"Warning: oc_diagnostics {installed} is at or above the maximum "
89 f"tested version ({_OC_DIAGNOSTICS_COMPAT['max']}). "
90 f"OCT may need an update."
91 )
92
93 return None
tuple[int,...] _parse_version_tuple(str v)
Definition compat.py:38
str|None check_oc_diagnostics_compat()
Definition compat.py:52