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"])
52 #############################################
53 def remove_startswith(aStr, startsToCheck):
55 remove starting strings, if begining of aStr correspond
56 order of list startsToCheck matter
57 do the stuff only for the first correspondence in startsToCheck
59 for s in startsToCheck:
60 if aStr.startswith(s):
65 #############################################
66 def toList_majorMinorPatch(aStr, verbose=False):
68 Returns list of integer as [major, minor, patch] from a string,
70 | accepts '1.2.3' '1_2_3' 'version_1.2.3' 'version1.2.3' 'v1.2.3',
71 | completion '123' means '123.0.0', '1.2' means '1.2.0'
73 | raise exception if problem
76 print("toList_majorMinorPatch('%s')" % aStr)
77 res = aStr.replace(" ", "")
79 res = remove_startswith(res, "version_ version v".split())
80 res = res.replace(".", "_").split("_")
82 msg = "Not a major_minor_patch correct syntax: '%s'" % aStr
85 msg = "An empty string is not a major_minor_patch syntax"
88 # complete MINOR.PATCH if not existing
97 msg = "major in major_minor_patch is not integer: '%s'" % aStr
100 msg = "major in major_minor_patch is negative integer: '%s'" % aStr
106 msg = "minor in major_minor_patch is not integer: '%s'" % aStr
109 msg = "minor in major_minor_patch is negative integer: '%s'" % aStr
115 msg = "patch in major_minor_patch is not integer: '%s'" % aStr
118 msg = "patch in major_minor_patch is negative integer: '%s'" % aStr
121 return [int(i) for i in res]
124 #############################################
125 def toCompactStr_majorMinorPatch(version):
128 parameter version is list of integer as [major, minor, patch]
130 | returns "789" for [7, 8, 9]
132 | minor, pach have to be integer less than 10
133 | raise exception for [7, 10, 11]
134 | (which returns "71011" as ambigous 710.1.1 for example)
136 # forbidden use from nov. 2023 and SALOME 9.10.0
138 "obsolete toCompactStr_majorMinorPatch method: forbiden use of compact representation of '%s', fix problem in caller"
143 #############################################
144 def getRange_majorMinorPatch(aStr, verbose=False):
146 extract from aStr a version range, defined as
147 '*_from_aMinVersionTag_to_aMaxVersionTag' or
148 '*version_aMinVersionTag_to_aMaxVersionTag'.
150 where aMinVersionTag and aMaxVersionTag are compatible with MinorMajorPatch class syntaxes
151 '1.2.3' or '1_2_3' etc.
152 if not found '_from_' or 'version_' first then aMinVersionTag is '0.0.0'
154 :param aStr: string to work
155 :return: list [min, max], where min, max are MinorMajorPatch instances.
156 else None if not found
158 tmp1 = aStr.lower().split("_to_")
161 return None # no '_to_'
163 msg = "more than one '_to_' is incorrect for version range: '%s'" % aStr
167 # accept older syntax as 'version_1_0_0_to_2_0_0', (as '_from_1_0_0_to_2_0_0')
168 if "version_" in tmp1[0] and "_from_" not in tmp1[0]:
169 aStr_with_from = aStr.lower().replace("version_", "_from_", 1)
171 aStr_with_from = aStr.lower()
173 # print("aStr_with_from '%s' -> '%s'" % (aStr, aStr_with_from))
175 tmp0 = aStr_with_from.split("_from_")
176 tmp1 = aStr_with_from.split("_to_")
179 msg = "more than one '_from_' is incorrect for version range: '%s'" % aStr
182 tmp2 = tmp1[0].split("_from_")
190 msg = "version min '%s' and version max '%s' in version range: '%s'" % (
198 rMin = MinorMajorPatch(aMin)
199 rMax = MinorMajorPatch(aMax)
201 msg = "problem version range in '%s'" % aStr
204 print("WARNING: problem version range in '%s'" % aStr)
208 msg = "version min '%s' > version max '%s' in version range: '%s'" % (
218 #############################################
219 class MinorMajorPatch(object):
221 class to define a version as MAJOR.MINOR.PATCH
223 | Given a version number MAJOR.MINOR.PATCH separator "_" or "."
225 | MAJOR version when you make incompatible API changes,
226 | MINOR version when you add functionality in a backwards-compatible manner,
227 | PATCH version when you make backwards-compatible bug fixes.
230 def __init__(self, version):
231 if type(version) == list:
232 aStr = "_".join([str(i) for i in version])
233 v = toList_majorMinorPatch(aStr)
235 v = toList_majorMinorPatch(version)
240 def __repr__(self, sep="_"):
241 """example is 'version_1_2_3'"""
242 res = "version_%i%s%i%s%i" % (self.major, sep, self.minor, sep, self.patch)
245 def __str__(self, sep="."):
246 """example is '1.2.3'"""
247 res = "%i%s%i%s%i" % (self.major, sep, self.minor, sep, self.patch)
251 """example is '1_2_3'"""
252 return self.__str__(sep="_")
254 def strClassic(self):
255 """example is '1.2.3'"""
256 return self.__str__(sep=".")
258 def strCompact(self):
259 """example is '123' from '1.2.3'"""
260 # forbidden use from nov. 2023 and SALOME 9.10.0
262 "obsolete strCompact method: forbiden use of compact representation of '%s', fix problem in caller"
265 # return toCompactStr_majorMinorPatch(self.toList())
268 """example is list of integer [1, 2, 3] from '1.2.3'"""
269 return [self.major, self.minor, self.patch]
271 def __lt__(self, other):
272 res = self.toList() < other.toList()
275 def __le__(self, other):
276 res = self.toList() <= other.toList()
279 def __gt__(self, other):
280 res = self.toList() > other.toList()
283 def __ge__(self, other):
284 res = self.toList() >= other.toList()
287 def __eq__(self, other):
288 res = self.toList() == other.toList()
291 def __ne__(self, other):
292 res = self.toList() != other.toList()