Skip to content

Commit

Permalink
Merge pull request #7888 from Vishal-NEC/Fix_for_7777
Browse files Browse the repository at this point in the history
fix_for_Plugin_Toolkit_check_ckan_version #7777
  • Loading branch information
smotornyuk committed Nov 17, 2023
2 parents 8e5d925 + 250d648 commit 8ad28ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions changes/7777.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updated the ckan.plugins.toolkit.check_ckan_version() to use packaging.version for version comparison/testing,
Remove ckan.plugins.toolkit._version_str_2_list() method because of no use.
42 changes: 22 additions & 20 deletions ckan/plugins/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ckan
import ckan.lib.base as base
from ckan.lib.base import render, abort
import packaging.version as pv

from ckan.logic import ( # noqa: re-export
get_action,
Expand Down Expand Up @@ -190,15 +191,6 @@ def add_resource(path: str, name: str):
create_library(name, absolute_path)


def _version_str_2_list(v_str: str):
"""convert a version string into a list of ints
eg 1.6.1b --> [1, 6, 1]"""
import re

v_str = re.sub(r"[^0-9.]", "", v_str)
return [int(part) for part in v_str.split(".")]


def check_ckan_version(
min_version: Optional[str] = None, max_version: Optional[str] = None):
"""Return ``True`` if the CKAN version is greater than or equal to
Expand All @@ -220,17 +212,27 @@ def check_ckan_version(
:type max_version: string
"""
current = _version_str_2_list(ckan.__version__)

if min_version:
min_required = _version_str_2_list(min_version)
if current < min_required:
return False
if max_version:
max_required = _version_str_2_list(max_version)
if current > max_required:
return False
return True
current = pv.parse(ckan.__version__)
try:
at_least_min = (
min_version is None
or current >= pv.parse(min_version)
)
except pv.InvalidVersion:
raise ValueError(
f"min_version '{min_version}' is not a valid version identifier"
)

try:
at_most_max = (
max_version is None
or current <= pv.parse(max_version)
)
except pv.InvalidVersion:
raise ValueError(
f"max_version '{max_version}' is not a valid version identifier"
)
return at_least_min and at_most_max


def requires_ckan_version(min_version: str, max_version: Optional[str] = None):
Expand Down
6 changes: 6 additions & 0 deletions ckan/tests/plugins/test_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
("1.5.1", "max", "1.6.1", True),
("1.5.1", "max", "1.5.0", False),
("1.5.1", "max", "1.5.2", True),
("2.10.0a0", "min", "2.10.2", False),
("2.10.0", "min", "2.10.0a1", True),
("2.10.1b0", "min", "2.10.2", False),
("2.10.1b0", "max", "2.10.1", True),
("2.10.0a0", "max", "2.10.2", True),
("2.10.0", "max", "2.10.0a1", False),
],
)
def test_check_ckan_version(version, bound, value, expected, monkeypatch):
Expand Down

0 comments on commit 8ad28ae

Please sign in to comment.