From 8e9ce30c261aba0a246077a98da908d456a86947 Mon Sep 17 00:00:00 2001 From: cat-bro Date: Sun, 16 Apr 2023 17:27:25 +1000 Subject: [PATCH] Add helpers for tool version comparison --- tests/fixtures/mapping-rule-tool-limits.yml | 15 +++++++- tests/test_mapper_rules.py | 39 +++++++++++++++++++++ tpv/commands/test/mock_galaxy.py | 3 +- tpv/core/helpers.py | 17 +++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/mapping-rule-tool-limits.yml b/tests/fixtures/mapping-rule-tool-limits.yml index e4717ad..76a7c2e 100644 --- a/tests/fixtures/mapping-rule-tool-limits.yml +++ b/tests/fixtures/mapping-rule-tool-limits.yml @@ -26,7 +26,20 @@ tools: execute: | from galaxy.jobs.mapper import JobNotReadyException raise JobNotReadyException() - + toolshed.g2.bx.psu.edu/repos/iuc/trinity/trinity/.*: + rules: + - if: helpers.tool_version_gte(tool, '2.15.1+galaxy0') + env: + version_gte_2.15.1+galaxy0: true + - if: helpers.tool_version_gt(tool, '2.15.1+galaxy0') + env: + version_gt_2.15.1+galaxy0: true + - if: helpers.tool_version_lt(tool, '2.10.1+galaxy7') + env: + version_lt_2.10.1+galaxy7: true + - if: helpers.tool_version_lte(tool, '2.10.1+galaxy7') + env: + version_lte_2.10.1+galaxy7: true destinations: local: diff --git a/tests/test_mapper_rules.py b/tests/test_mapper_rules.py index 3f1c840..8c49be3 100644 --- a/tests/test_mapper_rules.py +++ b/tests/test_mapper_rules.py @@ -226,3 +226,42 @@ def create_job(app, mock_user, mock_tool): with self.assertRaises(JobNotReadyException): tpv_config = os.path.join(os.path.dirname(__file__), 'fixtures/mapping-rule-tool-limits.yml') self._map_to_destination(tool_total_limit_3, user_roosta, datasets, tpv_config_files=[tpv_config], app=app) + + def test_tool_version_comparison_helpers(self): + tpv_config = os.path.join(os.path.dirname(__file__), 'fixtures/mapping-rule-tool-limits.yml') + user = mock_galaxy.User('ford', 'prefect@vortex.org') + datasets = [mock_galaxy.DatasetAssociation("test", mock_galaxy.Dataset("test.txt", file_size=1*1024**3))] + + def mock_trinity_with_version(version): + return mock_galaxy.Tool( + id=f'toolshed.g2.bx.psu.edu/repos/iuc/trinity/trinity/{version}', + version=version + ) + + env_keys = [ # env keys that may be added by cooked trinity rules + 'version_gte_2.15.1+galaxy0', + 'version_gt_2.15.1+galaxy0', + 'version_lt_2.10.1+galaxy7', + 'version_lte_2.10.1+galaxy7', + ] + # trinity version 3.15.1+galaxy0 + tool = mock_trinity_with_version('3.15.1+galaxy0') + destination = self._map_to_destination(tool, user, datasets, tpv_config_files=[tpv_config]) + self.assertCountEqual( + [e.get('name') for e in destination.env if e.get('name') in env_keys], + ['version_gte_2.15.1+galaxy0', 'version_gt_2.15.1+galaxy0'], + ) + # trinity version 2.15.1+galaxy0 + tool = mock_trinity_with_version('2.15.1+galaxy0') + destination = self._map_to_destination(tool, user, datasets, tpv_config_files=[tpv_config]) + self.assertCountEqual( + [e.get('name') for e in destination.env if e.get('name') in env_keys], + ['version_gte_2.15.1+galaxy0'], + ) + # trinity version 2.10.1+galaxy6 + tool = mock_trinity_with_version('2.10.1+galaxy6') + destination = self._map_to_destination(tool, user, datasets, tpv_config_files=[tpv_config]) + self.assertCountEqual( + [e.get('name') for e in destination.env if e.get('name') in env_keys], + ['version_lt_2.10.1+galaxy7', 'version_lte_2.10.1+galaxy7'], + ) diff --git a/tpv/commands/test/mock_galaxy.py b/tpv/commands/test/mock_galaxy.py index feadf1b..565678a 100644 --- a/tpv/commands/test/mock_galaxy.py +++ b/tpv/commands/test/mock_galaxy.py @@ -44,9 +44,10 @@ def __init__(self, file_name, file_size): # Tool mock and helpers========================================= class Tool: - def __init__(self, id): + def __init__(self, id, version=None): self.id = id self.old_id = id + self.version = version self.installed_tool_dependencies = [] diff --git a/tpv/core/helpers.py b/tpv/core/helpers.py index 5da6e3c..835440b 100644 --- a/tpv/core/helpers.py +++ b/tpv/core/helpers.py @@ -1,3 +1,4 @@ +import packaging.version import random from functools import reduce from galaxy import model @@ -85,3 +86,19 @@ def tag_values_match(entity, match_tag_values=[], exclude_tag_values=[]): all([any(entity.tpv_tags.filter(tag_value=tag_value)) for tag_value in match_tag_values]) and not any([any(entity.tpv_tags.filter(tag_value=tag_value)) for tag_value in exclude_tag_values]) ) + + +def tool_version_lte(tool, version): + return packaging.version.parse(tool.version) <= packaging.version.parse(version) + + +def tool_version_lt(tool, version): + return packaging.version.parse(tool.version) < packaging.version.parse(version) + + +def tool_version_gte(tool, version): + return packaging.version.parse(tool.version) >= packaging.version.parse(version) + + +def tool_version_gt(tool, version): + return packaging.version.parse(tool.version) > packaging.version.parse(version)