Skip to content

Commit

Permalink
Don't crash autoupdate on non-PEP440-compliant tool versions
Browse files Browse the repository at this point in the history
Fix errors like:

```
File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/commands/cmd_autoupdate.py", line 120, in cli
    tools_to_update = autoupdate.get_tools_to_update(ctx, workflow, tools_to_skip)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/autoupdate.py", line 363, in get_tools_to_update
    return outdated_tools(ctx, wf_dict, ts, tools_to_skip)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/autoupdate.py", line 349, in outdated_tools
    outdated_tools_rec(wf_dict)
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/autoupdate.py", line 337, in outdated_tools_rec
    outdated_tool_dict.update(check_tool_step(tool_id))
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/autoupdate.py", line 324, in check_tool_step
    updated_tool_id = get_newest_tool_id(matching_tool_ids)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/autoupdate.py", line 290, in get_newest_tool_id
    return sorted(
           ^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/planemo/autoupdate.py", line 292, in <lambda>
    key=lambda n: packaging.version.parse(n.split("/")[-1]),
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/packaging/version.py", line 54, in parse
    return Version(version)
           ^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/packaging/version.py", line 200, in __init__
    raise InvalidVersion(f"Invalid version: '{version}'")
packaging.version.InvalidVersion: Invalid version: '0.8.1+galaxy0+galaxy0'
```

by replacing an invalid version with version "0".
  • Loading branch information
nsoranzo committed Jan 8, 2024
1 parent f6ce25c commit 84f5137
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions planemo/autoupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from planemo.io import (
error,
info,
warn,
)
from planemo.workflow_lint import (
find_repos_from_tool_id,
Expand Down Expand Up @@ -78,6 +79,18 @@ def get_tokens(xml_tree: ElementTree) -> Dict[str, Dict[str, Optional[str]]]:
return tokens


def parse_version(version: str) -> packaging.version.Version:
"""
Parse a version string into a Version object.
If the version string is not PEP 440 compliant, return Version("0").
"""
try:
return packaging.version.parse(version)
except packaging.version.InvalidVersion as e:
warn(e)
return packaging.version.Version("0")


def check_conda(package_name: str, ctx: "PlanemoCliContext", **kwds) -> str:
"""
Get the most up-to-date conda version for a package.
Expand All @@ -89,7 +102,7 @@ def check_conda(package_name: str, ctx: "PlanemoCliContext", **kwds) -> str:
search_results = itertools.chain.from_iterable(
n["versions"] for n in r.json() if n["name"] == package_name and n["owner"] in kwds["conda_ensure_channels"]
)
return sorted(search_results, key=packaging.version.parse, reverse=True)[0]
return sorted(search_results, key=parse_version, reverse=True)[0]

target = conda_util.CondaTarget(package_name)
best_search_results = conda_util.best_search_result(target, conda_context=conda_context)
Expand Down Expand Up @@ -289,7 +302,7 @@ def _update_wf(config: "LocalGalaxyConfig", workflow_id: str, instance: bool = F
def get_newest_tool_id(tool_ids: List[str]) -> str:
return sorted(
tool_ids,
key=lambda n: packaging.version.parse(n.split("/")[-1]),
key=lambda n: parse_version(n.split("/")[-1]),
)[-1]


Expand Down

0 comments on commit 84f5137

Please sign in to comment.