Skip to content

Commit

Permalink
Update compare versions
Browse files Browse the repository at this point in the history
  • Loading branch information
corpnewt authored Jun 7, 2018
1 parent fd8dbaf commit af4e1a7
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions Scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,41 @@ def __init__(self, name = "Python Script"):
self.colors_dict = {}
os.chdir(cwd)

def compare_versions(self, vers1, vers2):
def compare_versions(self, vers1, vers2, pad = -1):
# Helper method to compare ##.## strings
#
# vers1 < vers2 = True
# vers1 = vers2 = None
# vers1 > vers2 = False
#
# Check for equality first
if vers1 == vers2:
# Equal
return None
try:
v1_parts = [ int(x) for x in vers1.split(".") ]
v2_parts = [ int(x) for x in vers2.split(".") ]
except:
# Formatted wrong - return None
return None
total = len(v1_parts) if len(v1_parts) > len(v2_parts) else len(v2_parts)
for i in range(total):
if i >= len(v1_parts):
# v2 is longer
return True
if i >= len(v2_parts):
# v1 is longer
return False
if v1_parts[i] < v2_parts[i]:
# v2 is newer
# Must be separated with a period

# Cast as strings
vers1 = str(vers1)
vers2 = str(vers2)

# Split to lists
v1_parts = vers1.split(".")
v2_parts = vers2.split(".")

# Equalize lengths
if len(v1_parts) < len(v2_parts):
v1_parts.extend([pad for x in range(len(v2_parts))])
elif len(v2_parts) < len(v1_parts):
v2_parts.extend([pad for x in range(len(v1_parts))])

# Iterate and compare
for i in range(len(v1_parts)):
# Remove non-numeric
v1 = ''.join(c for c in v1_parts[i] if c.isdigit())
v2 = ''.join(c for c in v2_parts[i] if c.isdigit())
# If empty - make it a pad var
v1 = pad if not len(v1) else v1
v2 = pad if not len(v2) else v2
# Compare
if int(v1) < int(v2):
return True
if v1_parts[i] > v2_parts[i]:
# v1 is newer
elif int(v1) > int(v2):
return False
# Never differed - return None, must be equal
return None
Expand Down Expand Up @@ -224,4 +229,4 @@ def custom_quit(self):
print("Have a nice evening!\n\n")
else:
print("Have a nice night!\n\n")
exit(0)
exit(0)

0 comments on commit af4e1a7

Please sign in to comment.