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

Fix version sorting when comparing str and int with python 3 #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions python/tk_multi_launchapp/base_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# not expressly granted therein are reserved by Shotgun Software Inc.

import os
import re
import sys
from distutils.version import LooseVersion

import sgtk
from sgtk import TankError
Expand Down Expand Up @@ -435,7 +435,7 @@ def launch_from_path_and_context(self, path, context, version=None):

def _sort_versions(self, versions):
"""
Uses standard python modules to determine how to sort arbitrary version numbers.
Sort arbitrary version numbers.
A version number consists of a series of numbers, separated by either periods or
strings of letters. When comparing version numbers, the numeric components will
be compared numerically, and the alphabetic components lexically. For example:
Expand All @@ -449,10 +449,18 @@ def _sort_versions(self, versions):
:returns: List of sorted versions in descending order. The highest version is
at index 0.
"""
# Cast the incoming version strings as LooseVersion instances to sort using
# the LooseVersion.__cmp__ method.
sort_versions = [LooseVersion(version) for version in versions]
sort_versions.sort(reverse=True)

# Convert the LooseVersions back to strings on return.
return [str(version) for version in sort_versions]
def _iter_parts(string):
for part in re.findall("[a-zA-Z_]+|[0-9]+", string):
try:
v = int(part)
yield 0
yield v
except ValueError:
yield 1
yield part

return sorted(versions,
key=lambda v:tuple(_iter_parts(str(v))),
reverse=True
)