11
11
12
12
from .utils import logger , fail
13
13
14
+
14
15
@dataclass
15
16
class Require :
17
+ """Requirement."""
18
+
16
19
name : str
17
20
min_version : tuple
18
21
mandatory : bool
@@ -28,6 +31,17 @@ class Require:
28
31
)
29
32
30
33
34
+ def _version_tuple (version_str ):
35
+ """Translate version into a tuple of int."""
36
+ if not version_str :
37
+ return None
38
+ if "-" in version_str :
39
+ version_str , * _ = version_str .rpartition ("-" )
40
+ try :
41
+ return tuple (int (i ) for i in version_str .split ("." ))
42
+ except ValueError :
43
+ return None
44
+
31
45
32
46
def check (name , min_version = None , mandatory = True ):
33
47
"""Check whether an app exists and whether its version is correct."""
@@ -43,27 +57,21 @@ def error(*args):
43
57
44
58
# Get version
45
59
result = subprocess .run (
46
- [app , "--version" ],
47
- capture_output = True ,
48
- text = True ,
60
+ [app , "--version" ], capture_output = True , text = True , check = False
49
61
)
50
62
output = result .stdout .strip () or result .stderr .strip ()
51
63
# Match version patterns like 1.2.3, 4.5, 2.0.1-alpha, etc.
52
- match = re .search (r'\d+\.\d+(?:\.\d+)?(?:[-.\w]*)?' , output )
53
- if match :
54
- version = match .group (0 )
55
- if '-' in version :
56
- version , * _ = version .rpartition ("-" )
57
- version = tuple (int (i ) for i in version .split ("." ))
58
- version_str = "." .join (str (i ) for i in version )
64
+ if match := re .search (r"\d+\.\d+(?:\.\d+)?(?:[-.\w]*)?" , output ):
65
+ version_str = match .group (0 )
59
66
else :
60
- version = None
61
67
version_str = None
62
68
63
69
# Check Version, if necessary
64
70
if min_version :
65
71
min_version_str = "." .join (str (i ) for i in min_version )
66
- if not version :
72
+
73
+ # Translate version
74
+ if not (version := _version_tuple (version_str )):
67
75
return error ("Cannot read '%s' version" , name )
68
76
if version < min_version :
69
77
return error (
0 commit comments