Debian Codegraph @

python/src/feature_check/version.py

raw
1 # SPDX-FileCopyrightText: Peter Pentchev <[email protected]>
2 # SPDX-License-Identifier: BSD-2-Clause
3
4 """Version string parsing for the feature-check Python library."""
5
6 from __future__ import annotations
7
8 from typing import NamedTuple
9
10
11 class VersionComponent(NamedTuple):
12 """Represent a single version component: a numerical part and a freeform string one."""
13
14 num: int | None
15 rest: str
16
17 def __str__(self) -> str:
18 """Provide a string representation of the version component."""
19 return (str(self.num) if self.num is not None else "") + self.rest
20
21 def cmp(self, other: object) -> int: # noqa: C901,PLR0911,PLR0912
22 """Compare two components, return None if they are equal."""
23 if not isinstance(other, VersionComponent):
24 raise NotImplementedError(repr(other))
25
26 if self.num is not None:
27 if other.num is not None:
28 if self.num < other.num:
29 return -1
30 if self.num > other.num:
31 return 1
32
33 if self.rest is not None:
34 if other.rest is not None:
35 if self.rest < other.rest:
36 return -1
37 if self.rest > other.rest:
38 return 1
39 return 0
40
41 return 1
42
43 if other.rest is not None:
44 return -1
45
46 return 0
47
48 return 1
49
50 if other.num is not None:
51 return -1
52
53 if self.rest is not None:
54 if other.rest is not None:
55 if self.rest < other.rest:
56 return -1
57 if self.rest > other.rest:
58 return 1
59 return 0
60
61 return -1
62
63 return 0
64
65 def __lt__(self, other: object) -> bool:
66 """Check whether this version component is less than the other one."""
67 return self.cmp(other) < 0
68
69 def __le__(self, other: object) -> bool:
70 """Check whether this version component is less than or equal to the other one."""
71 return self.cmp(other) <= 0
72
73 def __eq__(self, other: object) -> bool:
74 """Check whether this version component is equal to the other one."""
75 return self.cmp(other) == 0
76
77 def __ne__(self, other: object) -> bool:
78 """Check whether this version component is not equal to the other one."""
79 return self.cmp(other) != 0
80
81 def __ge__(self, other: object) -> bool:
82 """Check whether this version component is greater than or equal to the other one."""
83 return self.cmp(other) >= 0
84
85 def __gt__(self, other: object) -> bool:
86 """Check whether this version component is greater than the other one."""
87 return self.cmp(other) > 0
88
89 def __hash__(self) -> int:
90 """Return an integer key for hashable collections."""
91 return hash((self.num, self.rest))
92
93
94 class Version(NamedTuple):
95 """A version string: many components, possibly other attributes."""
96
97 value: str
98 components: list[VersionComponent]
99
100
101 def _version_compare_split_empty(
102 spl_a: list[VersionComponent],
103 spl_b: list[VersionComponent],
104 ) -> int | None:
105 """Check if any of the split version numbers is empty."""
106 if not spl_a:
107 if not spl_b:
108 return 0
109 if spl_b[0].num is None:
110 return 1
111 return -1
112 if not spl_b:
113 if spl_a[0].num is None:
114 return -1
115 return 1
116
117 return None
118
119
120 def _version_compare_split(spl_a: list[VersionComponent], spl_b: list[VersionComponent]) -> int:
121 """Compare two version numbers already split into component lists.
122
123 Returns -1, 0, or 1 for the first version being less than, equal to,
124 or greater than the second one.
125 """
126 res = _version_compare_split_empty(spl_a, spl_b)
127 if res is not None:
128 return res
129
130 (comp_a, comp_b) = (spl_a.pop(0), spl_b.pop(0))
131 res = comp_a.cmp(comp_b)
132 if res != 0:
133 return res
134
135 return _version_compare_split(spl_a, spl_b)
136
137
138 def version_compare(ver_a: Version, ver_b: Version) -> int:
139 """Compare two version strings.
140
141 Returns -1, 0, or 1 for the first version being less than, equal to,
142 or greater than the second one.
143 """
144 return _version_compare_split(ver_a.components, ver_b.components)