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

runner.conda: Explicitly handle our package versions which are invalid by PEP-440 #286

Merged
merged 1 commit into from
May 31, 2023
Merged
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
63 changes: 62 additions & 1 deletion nextstrain/cli/runner/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
import subprocess
import tarfile
import traceback
from packaging.version import parse as parse_version
from functools import partial
from packaging.version import Version, InvalidVersion
from pathlib import Path, PurePosixPath
from typing import Iterable, NamedTuple, Optional
from urllib.parse import urljoin, quote as urlquote
Expand Down Expand Up @@ -596,6 +597,66 @@ def latest_package_label_version(channel: str, package: str, label: str) -> Opti
return latest_file.get("version")


def parse_version(version: str) -> Version:
"""
Parse *version* into a PEP-440-compliant :cls:`Version` object, by hook or
by crook.

If *version* isn't already PEP-440 compliant, then it is parsed as a
PEP-440 local version label after replacing with ``.`` any characters not
matching ``a-z``, ``A-Z``, ``0-9``, ``.``, ``_``, or ``-``. The comparison
semantics for local version labels amount to a string- and integer-based
comparison by parts ("segments"), which is super good enough for our
purposes here. The full local version identifier produced for versions
parsed in this way always contains a public version identifier component of
``0.dev0`` so it compares lowest against other public version identifiers.

>>> parse_version("1.2.3")
<Version('1.2.3')>
>>> parse_version("1.2.3-nope")
<Version('0.dev0+1.2.3.nope')>
>>> parse_version("20221019T172207Z")
<Version('0.dev0+20221019t172207z')>
>>> parse_version("@invalid+@")
<Version('0.dev0+invalid')>
>>> parse_version("not@@ok")
<Version('0.dev0+not.ok')>
>>> parse_version("20221019T172207Z") < parse_version("20230525T143814Z")
True
"""
try:
return Version(version)
except InvalidVersion:
# Per PEP-440
#
# > …local version labels MUST be limited to the following set of
# > permitted characters:
# >
# > ASCII letters ([a-zA-Z])
# > ASCII digits ([0-9])
# > periods (.)
# >
# > Local version labels MUST start and end with an ASCII letter or
# > digit.
#
# and
#
# > With a local version, in addition to the use of . as a separator of
# > segments, the use of - and _ is also acceptable. The normal form is
# > using the . character.
#
# and empty segments (x..z) aren't allowed either.
#
# c.f. <https://peps.python.org/pep-0440/#local-version-identifiers>
# <https://peps.python.org/pep-0440/#local-version-segments>
remove_invalid_start_end_chars = partial(re.sub, r'^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$', '')
replace_invalid_with_separators = partial(re.sub, r'[^a-zA-Z0-9._-]+', '.')

as_local_segment = lambda v: replace_invalid_with_separators(remove_invalid_start_end_chars(v))

return Version(f"0.dev0+{as_local_segment(version)}")


class PackageSpec(NamedTuple):
name: str
version_spec: Optional[str] = None
Expand Down