-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pyln: Add a version parser to pyln-testing
We changed the way we configure CLN to run in developer mode, which promptly broke anything that wasn't v23.11 and later. So we parse the version, and make it comparable, so we can determine what to do on the fly, and maintain a better backwards compatibility. Changelog-Changed: pyln-testing: pyln-testing is now version-aware and can customize the run based on the version.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
from dataclasses import dataclass | ||
import re | ||
|
||
|
||
@dataclass | ||
class Version: | ||
year: int | ||
month: int | ||
patch: int = 0 | ||
|
||
def __lt__(self, other): | ||
return [self.year, self.month, self.patch] < [other.year, other.month, other.patch] | ||
|
||
def __gt__(self, other): | ||
return other < self | ||
|
||
def __le__(self, other): | ||
return [self.year, self.month, self.patch] <= [other.year, other.month, other.patch] | ||
|
||
def __ge__(self, other): | ||
return other <= self | ||
|
||
def __eq__(self, other): | ||
return [self.year, self.month] == [other.year, other.month] | ||
|
||
@classmethod | ||
def from_str(cls, s: str) -> "Version": | ||
m = re.search(r'^v(\d+).(\d+).?(\d+)?(rc\d+)?', s) | ||
parts = [int(m.group(i)) for i in range(1, 4) if m.group(i) is not None] | ||
year, month = parts[0], parts[1] | ||
if len(parts) == 3: | ||
patch = parts[2] | ||
else: | ||
patch = 0 | ||
|
||
return Version(year=year, month=month, patch=patch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pyln.testing.version import Version | ||
|
||
|
||
def test_version_parsing(): | ||
cases = [ | ||
("v24.02", Version(24, 2)), | ||
("v23.11.2", Version(23, 11, 2)), | ||
] | ||
|
||
for test_in, test_out in cases: | ||
v = Version.from_str(test_in) | ||
assert test_out == v |