Skip to content

Commit f4513b6

Browse files
olehermanseminggo
authored andcommitted
New version_compare() function (#425)
Useful for comparing dot separated version numbers like these: 3 3.10 3.10.1 Needs quite a bit of logic to handle some corner cases correctly: 3.1.4 != 3.14 2.7.0 == 2.7 9.9.9 < 9.99 Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
1 parent 1722fd7 commit f4513b6

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

bin/cocos.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -717,29 +717,51 @@ def get_xcode_version():
717717

718718
return version
719719

720-
def version_minimum(a, b):
721-
'''Compares two version numbers to see if a >= b
720+
def version_compare(a, op, b):
721+
'''Compares two version numbers to see if a op b is true
722722
723-
expects two dot separated version numbers
724-
can be string, float or int
723+
op is operator
724+
op can be ">", "<", "==", "!=", ">=", "<="
725+
a and b are version numbers (dot separated)
726+
a and b can be string, float or int
727+
728+
Please note that: 3 == 3.0 == 3.0.0 ... ("==" is not a simple string cmp)
725729
'''
726-
if a == b or str(a).strip() == str(b).strip():
727-
return True
730+
allowed = [">", "<", "==", "!=", ">=", "<="]
731+
if op not in allowed:
732+
raise ValueError("op must be one of {}".format(allowed))
733+
734+
# Use recursion to simplify operators:
735+
if op[0] == "<": # Reverse args and inequality sign:
736+
return version_compare(b, op.replace("<",">"), a)
737+
if op == ">=":
738+
return version_compare(a,"==",b) or version_compare(a,">",b)
739+
if op == "!=":
740+
return not version_compare(a,"==",b)
741+
742+
# We now have 1 of 2 base cases, "==" or ">":
743+
assert op in ["==", ">"]
728744

729745
a = [int(x) for x in str(a).split(".")]
730746
b = [int(x) for x in str(b).split(".")]
747+
731748
for i in range(max(len(a), len(b))):
732-
ai, bi = 0, 0
749+
ai, bi = 0, 0 # digits
733750
if len(a) > i:
734751
ai = a[i]
735752
if len(b) > i:
736753
bi = b[i]
737754
if ai > bi:
738-
return True
755+
if op == ">":
756+
return True
757+
else: # op "=="
758+
return False
739759
if ai < bi:
760+
# Both "==" and ">" are False:
740761
return False
741-
742-
return True
762+
if op == ">":
763+
return False # op ">" and all digits were equal
764+
return True # op "==" and all digits were equal
743765

744766
def copy_files_in_dir(src, dst):
745767

plugins/plugin_compile/project_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ def build_ios(self):
809809
# should generate .xcarchive first, then generate .ipa
810810
xcode_version = cocos.get_xcode_version()
811811

812-
use_new_ipa_method = cocos.version_minimum(xcode_version, 8.3)
812+
use_new_ipa_method = cocos.version_compare(xcode_version,">=",8.3)
813813

814814
if self._sign_id is not None:
815815
if use_new_ipa_method:

0 commit comments

Comments
 (0)