Skip to content

Commit

Permalink
runner.conda: Explicitly handle our package versions which are invali…
Browse files Browse the repository at this point in the history
…d by PEP-440

Our nextstrain-base Conda package versions are not PEP-440 compliant, so
we inadvertently relied on packaging.version.parse_version() falling
back to its LegacyVersion class when it encountered an InvalidVersion
error.  LegacyVersion was dropped, however, in packaging 22.0 (December
2022).¹  This resulted in seeing the InvalidVersion error during runtime
update, e.g.:

    packaging.version.InvalidVersion: Invalid version: '20221019T172207Z'

To avoid that, we start handling InvalidVersion ourselves and produce a
valid proxy version instead with reasonable comparison semantics similar
to LegacyVersion.

The new version handling is more than strictly necessary for our current
package versions (which could just be simpler string comparisons), but
it allows us more leeway to change package versioning in the future if
need be without affecting existing installs of Nextstrain CLI.  I didn't
want to vendor LegacyVersion itself because it's fairly complex and
tangled with the implementation of packaging.version.

Resolves <#285>.

¹ <https://packaging.pypa.io/en/latest/changelog.html>
  • Loading branch information
tsibley committed May 31, 2023
1 parent dbf8e0c commit 797f30a
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion nextstrain/cli/runner/conda.py
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

0 comments on commit 797f30a

Please sign in to comment.