Skip to content
Closed
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
23 changes: 23 additions & 0 deletions python/tank/util/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,29 @@ def normalize_version_format(version: str) -> str:
return version


def format_version_range(min_version, max_version=None):
"""
Format a version range as a human-readable string.

:param str min_version: The minimum version of the range.
:param str max_version: The maximum version of the range, or None for open-ended.
:returns: A formatted string such as ">=1.2.3" or ">=1.2.3, <2.0.0".
:rtype: str
"""
min_version = str(min_version)

if max_version is None:
return ">=%s" % min_version

if is_version_newer_or_equal(min_version, max_version):
raise ValueError(
"min_version (%s) must be less than max_version (%s)"
% (min_version, max_version)
)

return ">=%s, <%s" % (min_version, max_version)


def _compare_versions(a, b):
"""
Tests if version a is newer than version b.
Expand Down
24 changes: 24 additions & 0 deletions tests/util_tests/test_format_version_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2024 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.

from tank_test.tank_test_base import setUpModule # noqa
from tank_test.tank_test_base import ShotgunTestBase
from tank.util.version import format_version_range


class TestFormatVersionRange(ShotgunTestBase):
def setUp(self):
super().setUp()

def test_open_ended_range(self):
self.assertEqual(format_version_range("1.2.3"), ">=1.2.3")

def test_bounded_range(self):
self.assertEqual(format_version_range("1.2.3", "2.0.0"), ">=1.2.3, <2.0.0")