Simplify code for versioncomparator.
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-import re
-import sys
-PY3 = sys.version_info[0] >= 3
-if PY3:
- def cmp(a, b):
- return (a > b) - (a < b)
+from pkg_resources import parse_version
- # mixin class for Python3 supporting __cmp__
- class PY3__cmp__:
- def __eq__(self, other):
- return self.__cmp__(other) == 0
- def __ne__(self, other):
- return self.__cmp__(other) != 0
- def __gt__(self, other):
- return self.__cmp__(other) > 0
- def __lt__(self, other):
- return self.__cmp__(other) < 0
- def __ge__(self, other):
- return self.__cmp__(other) >= 0
- def __le__(self, other):
- return self.__cmp__(other) <= 0
-else:
- class PY3__cmp__:
- pass
def cmp_versions(version1, version2):
- if version1 == version2:
- return 0
if version1 is None:
+ if version2 is None:
+ return 0
+ else:
return -1
if version2 is None:
return 1
- l1 = _version_string_to_list(version1)
- l2 = _version_string_to_list(version2)
- d = len(l1) - len(l2)
- if d > 0:
- l2 += ['' for _ in range(d)]
- if d < 0:
- l1 += ['' for _ in range(-d)]
- return cmp(l1, l2)
-
-_PREVIEW_VERSION = re.compile(r'(\d+)(\.\d+)*(a|b|rc)(\d*)$')
-_PREVIEW_PREFERENCE = {'a':-3, 'b':-2, 'rc':-1}
-
-
-def _version_string_to_list(version_string):
- if version_string == 'trunk':
- return ['-100']
- version_list = version_string.split('.')
- if _PREVIEW_VERSION.match(version_string):
- m = _PREVIEW_VERSION.match(version_list[-1])
- version_list[-1] = m.group(1)
- version_list += [_PREVIEW_PREFERENCE[m.group(3)]]
- version_list += [m.group(4)]
- return _strip_leading_zeros(version_list)
-
-
-def _strip_leading_zeros(version_list):
- while version_list and version_list[-1] == '0':
- version_list.pop()
- return version_list
+ if parse_version(version1) == parse_version(version2):
+ return 0
+ elif parse_version(version1) > parse_version(version2):
+ return 1
+ return -1
