4 # Copyright (C) 2010-2018 CEA/DEN
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 class and utilities to define a version as MAJOR.MINOR.PATCH,
25 | Given a version number MAJOR.MINOR.PATCH separator "_" or "."
27 | MAJOR version when you make incompatible API changes,
28 | MINOR version when you add functionality in a backwards-compatible manner,
29 | PATCH version when you make backwards-compatible bug fixes.
35 verbose = False # True
37 #############################################
38 def only_numbers(aStr):
40 Remove non numericals characters from string,
42 :param aStr: string to work
43 :return: None if no number presence
45 res = ''.join([nb for nb in aStr if nb in '0123456789'])
51 #############################################
52 def remove_startswith(aStr, startsToCheck):
54 remove starting strings, if begining of aStr correspond
55 order of list startsToCheck matter
56 do the stuff only for the first correspondence in startsToCheck
58 for s in startsToCheck:
59 if aStr.startswith(s):
63 #############################################
64 def toList_majorMinorPatch(aStr, verbose=False):
66 Returns list of integer as [major, minor, patch] from a string,
68 | accepts '1.2.3' '1_2_3' 'version_1.2.3' 'version1.2.3' 'v1.2.3',
69 | completion '123' means '123.0.0', '1.2' means '1.2.0'
71 | raise exception if problem
73 if verbose: print("toList_majorMinorPatch('%s')" % aStr)
74 res = aStr.replace(" ", "")
76 res = remove_startswith(res, "version_ version v".split())
77 res = res.replace(".", "_").split("_")
79 msg = "Not a major_minor_patch correct syntax: '%s'" % aStr
82 msg = "An empty string is not a major_minor_patch syntax"
85 # complete MINOR.PATCH if not existing
94 msg = "major in major_minor_patch is not integer: '%s'" % aStr
97 msg = "major in major_minor_patch is negative integer: '%s'" % aStr
103 msg = "minor in major_minor_patch is not integer: '%s'" % aStr
106 msg = "minor in major_minor_patch is negative integer: '%s'" % aStr
112 msg = "patch in major_minor_patch is not integer: '%s'" % aStr
115 msg = "patch in major_minor_patch is negative integer: '%s'" % aStr
118 return [int(i) for i in res]
120 #############################################
121 def toCompactStr_majorMinorPatch(version):
124 parameter version is list of integer as [major, minor, patch]
126 | returns "789" for [7, 8, 9]
128 | minor, pach have to be integer less than 10
129 | raise exception for [7, 10, 11]
130 | (which returns "71011" as ambigous 710.1.1 for example)
132 # forbidden use from nov. 2023 and SALOME 9.10.0
133 raise Exception("obsolete toCompactStr_majorMinorPatch method: forbiden use of compact representation of '%s', fix problem in caller" % version)
136 #############################################
137 def getRange_majorMinorPatch(aStr, verbose=False):
139 extract from aStr a version range, defined as
140 '*_from_aMinVersionTag_to_aMaxVersionTag' or
141 '*version_aMinVersionTag_to_aMaxVersionTag'.
143 where aMinVersionTag and aMaxVersionTag are compatible with MinorMajorPatch class syntaxes
144 '1.2.3' or '1_2_3' etc.
145 if not found '_from_' or 'version_' first then aMinVersionTag is '0.0.0'
147 :param aStr: string to work
148 :return: list [min, max], where min, max are MinorMajorPatch instances.
149 else None if not found
151 tmp1 = aStr.lower().split("_to_")
154 return None # no '_to_'
156 msg = "more than one '_to_' is incorrect for version range: '%s'" % aStr
160 # accept older syntax as 'version_1_0_0_to_2_0_0', (as '_from_1_0_0_to_2_0_0')
161 if "version_" in tmp1[0] and "_from_" not in tmp1[0]:
162 aStr_with_from = aStr.lower().replace("version_", "_from_", 1)
164 aStr_with_from = aStr.lower()
166 # print("aStr_with_from '%s' -> '%s'" % (aStr, aStr_with_from))
168 tmp0 = aStr_with_from.split("_from_")
169 tmp1 = aStr_with_from.split("_to_")
172 msg = "more than one '_from_' is incorrect for version range: '%s'" % aStr
175 tmp2 = tmp1[0].split("_from_")
183 msg = "version min '%s' and version max '%s' in version range: '%s'" % (aMin, aMax, aStr)
187 rMin = MinorMajorPatch(aMin)
188 rMax = MinorMajorPatch(aMax)
190 msg = "problem version range in '%s'" % aStr
193 print("WARNING: problem version range in '%s'" % aStr)
197 msg = "version min '%s' > version max '%s' in version range: '%s'" % (rMin, rMax, aStr)
202 #############################################
203 class MinorMajorPatch(object):
205 class to define a version as MAJOR.MINOR.PATCH
207 | Given a version number MAJOR.MINOR.PATCH separator "_" or "."
209 | MAJOR version when you make incompatible API changes,
210 | MINOR version when you add functionality in a backwards-compatible manner,
211 | PATCH version when you make backwards-compatible bug fixes.
214 def __init__(self, version):
215 if type(version) == list:
216 aStr = '_'.join([str(i) for i in version])
217 v = toList_majorMinorPatch(aStr)
219 v = toList_majorMinorPatch(version)
224 def __repr__(self, sep="_"):
225 """example is 'version_1_2_3' """
226 res = "version_%i%s%i%s%i" % (self.major, sep, self.minor, sep, self.patch)
229 def __str__(self, sep="."):
230 """example is '1.2.3' """
231 res = "%i%s%i%s%i" % (self.major, sep, self.minor, sep, self.patch)
235 """example is '1_2_3' """
236 return self.__str__(sep="_")
238 def strClassic(self):
239 """example is '1.2.3' """
240 return self.__str__(sep=".")
242 def strCompact(self):
243 """example is '123' from '1.2.3' """
244 # forbidden use from nov. 2023 and SALOME 9.10.0
245 raise Exception("obsolete strCompact method: forbiden use of compact representation of '%s', fix problem in caller" % str(self))
246 # return toCompactStr_majorMinorPatch(self.toList())
249 """example is list of integer [1, 2, 3] from '1.2.3' """
250 return [self.major, self.minor, self.patch]
252 def __lt__(self, other):
253 res = (self.toList() < other.toList())
256 def __le__(self, other):
257 res = (self.toList() <= other.toList())
260 def __gt__(self, other):
261 res = (self.toList() > other.toList())
264 def __ge__(self, other):
265 res = (self.toList() >= other.toList())
268 def __eq__(self, other):
269 res = (self.toList() == other.toList())
272 def __ne__(self, other):
273 res = (self.toList() != other.toList())