Coverage for pyquickhelper/texthelper/version_helper.py: 100%
42 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Helpers about versionning.
5"""
8def numeric_module_version(vers):
9 """
10 Converts a string into a tuple with numbers wherever possible.
12 @param vers string
13 @return tuple
14 """
15 if isinstance(vers, tuple):
16 return vers
17 spl = str(vers).split(".")
18 r = []
19 for _ in spl:
20 try:
21 i = int(_)
22 r.append(i)
23 except ValueError:
24 r.append(_)
25 return tuple(r)
28def compare_module_version(num, vers):
29 """
30 Compares two versions.
32 @param num first version
33 @param vers second version
34 @return -1, 0, 1
36 This function implements something similar to
37 *StrictVersion* (from *distutils*) but
38 probably more simple.
39 """
40 if num is None:
41 if vers is None:
42 return 0
43 else:
44 return 1
45 if vers is None:
46 return -1
48 if not isinstance(vers, tuple):
49 vers = numeric_module_version(vers)
50 if not isinstance(num, tuple):
51 num = numeric_module_version(num)
53 if len(num) == len(vers):
54 for a, b in zip(num, vers):
55 if isinstance(a, int) and isinstance(b, int):
56 if a < b:
57 return -1
58 elif a > b:
59 return 1
60 else:
61 a = str(a)
62 b = str(b)
63 if a < b:
64 return -1
65 if a > b:
66 return 1 # pragma: no cover
67 return 0
68 if len(num) < len(vers):
69 num = num + (0,) * (len(vers) - len(num))
70 return compare_module_version(num, vers)
71 vers = vers + (0,) * (len(num) - len(vers))
72 return compare_module_version(num, vers)