Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better pep440 #60170

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 20 additions & 84 deletions python/pyplugin_installer/version_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@
from qgis.core import Qgis

import re

from packaging.version import Version, InvalidVersion

# ------------------------------------------------------------------------ #


def normalizeVersion(s):
"""remove possible prefix from given string and convert to uppercase"""

prefixes = [
"VERSION",
"VER.",
Expand All @@ -78,96 +79,31 @@ def normalizeVersion(s):
return s


# ------------------------------------------------------------------------ #
def classifyCharacter(c):
"""return 0 for delimiter, 1 for digit and 2 for alphabetic character"""
if c in [".", "-", "_", " "]:
return 0
if c.isdigit():
return 1
else:
return 2


# ------------------------------------------------------------------------ #
def chopString(s):
"""convert string to list of numbers and words"""
l = [s[0]]
for i in range(1, len(s)):
if classifyCharacter(s[i]) == 0:
pass
elif classifyCharacter(s[i]) == classifyCharacter(s[i - 1]):
l[len(l) - 1] += s[i]
else:
l += [s[i]]
return l


# ------------------------------------------------------------------------ #
def compareElements(s1, s2):
"""compare two particular elements"""
# check if the matter is easy solvable:
if s1 == s2:
return 0
# try to compare as numeric values (but only if the first character is not 0):
if (
s1
and s2
and s1.isnumeric()
and s2.isnumeric()
and s1[0] != "0"
and s2[0] != "0"
):
if float(s1) == float(s2):
return 0
elif float(s1) > float(s2):
return 1
else:
return 2
# if the strings aren't numeric or start from 0, compare them as a strings:
# but first, set ALPHA < BETA < PREVIEW < RC < TRUNK < [NOTHING] < [ANYTHING_ELSE]
if s1 not in ["A", "ALPHA", "B", "BETA", "PREVIEW", "RC", "TRUNK"]:
s1 = "Z" + s1
if s2 not in ["A", "ALPHA", "B", "BETA", "PREVIEW", "RC", "TRUNK"]:
s2 = "Z" + s2
# the final test:
if s1 > s2:
return 1
else:
return 2


# ------------------------------------------------------------------------ #
def compareVersions(a, b):
"""Compare two version numbers. Return 0 if a==b or error, 1 if a>b and 2 if b>a"""
if not a or not b:
return 0
a = normalizeVersion(a)
b = normalizeVersion(b)
if a == b:
return 0
# convert the strings to lists
v1 = chopString(a)
v2 = chopString(b)
# set the shorter string as a base
l = len(v1)
if l > len(v2):
l = len(v2)
# try to determine within the common length
for i in range(l):
if compareElements(v1[i], v2[i]):
return compareElements(v1[i], v2[i])
# if the lists are identical till the end of the shorter string, try to compare the odd tail
# with the simple space (because the 'alpha', 'beta', 'preview' and 'rc' are LESS then nothing)
if len(v1) > l:
return compareElements(v1[l], " ")
if len(v2) > l:
return compareElements(" ", v2[l])
# if everything else fails...
if a > b:
return 1
else:
return 2

try:
# PEP 440 comparison
va, vb = Version(a), Version(b)
if va < vb:
return 2
elif va > vb:
return 1
else:
return 0
except InvalidVersion:
# blunt str comparison
if a < b:
return 2
elif a > b:
return 1
else:
return 0


"""
Expand Down
53 changes: 50 additions & 3 deletions tests/src/python/test_versioncompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,63 @@ def testCompareVersions(self):
b = "1.0.0-2"
self.assertEqual(compareVersions(a, b), 2)

# test versions with suffixes
a = "1.0.0a"
b = "1.0.0b"
# test versions with long pre-release suffixes
a = "1.0.0alpha"
b = "1.0.0beta"
self.assertEqual(compareVersions(a, b), 2)

# test versions with PEP440 suffixes
a = "1.0.0a2"
b = "1.0.0b1"
self.assertEqual(compareVersions(a, b), 2)

# test versions with post suffixes
a = "1.0"
b = "1.0post1"
self.assertEqual(compareVersions(a, b), 2)

# test versions with different lengths
a = "1.0"
b = "1.0.1"
self.assertEqual(compareVersions(a, b), 2)
a = "2.0"
self.assertEqual(compareVersions(a, b), 1)

# test versions with suffixes in different cases
a = "1.0.0-201609011405-2690BD9"
b = "1.0.0-201609011405-2690bd9"
self.assertEqual(compareVersions(a, b), 0)

# test versions with different lengths
a = "1.0a1"
b = "1.0.1post2"
self.assertEqual(compareVersions(a, b), 2)
a = "2.0.1"
self.assertEqual(compareVersions(a, b), 1)

# test shorthand alphas
a = "1.0a1"
b = "1.0alpha1"
self.assertEqual(compareVersions(a, b), 0)
b = "1.0.alpha1"
self.assertEqual(compareVersions(a, b), 0)
b = "1.0.alpha.1"
self.assertEqual(compareVersions(a, b), 0)

# test partial versions
a = "1"
b = "1.0"
self.assertEqual(compareVersions(a, b), 0)
b = "1.0.0"
self.assertEqual(compareVersions(a, b), 0)
b = "1.0.0post1"
self.assertEqual(compareVersions(a, b), 2)

# PEP440 test (failling without packaging)
a = "1.0a1"
b = "1.0.0alpha1"
self.assertEqual(compareVersions(a, b), 0)


if __name__ == "__main__":
unittest.main()
Loading