Skip to content

Commit

Permalink
Improve version detection for __linux virtual pkg
Browse files Browse the repository at this point in the history
Use a regex based on documented versioning schemes to extract a version
to the `__linux` virtual package; this replaces our naive scheme of
splitting on the first "-", which failed on certain vendor strings
(e.g., container-optimized OS) and didn't handle bogus version strings.

Fixes conda#10596
  • Loading branch information
chenghlee committed Apr 8, 2021
1 parent 3e84f24 commit 0c9e4c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
13 changes: 10 additions & 3 deletions conda/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import os
import re
from itertools import chain
from logging import getLogger
import platform
Expand Down Expand Up @@ -172,11 +173,17 @@ def _supplement_index_with_system(index):
libc_family, libc_version = context.libc_family_version
is_linux = context.subdir.startswith("linux-")
if is_linux:
# By convention, the kernel release string should be three or four
# numeric components, separated by dots, followed by vendor-specific
# bits. For the purposes of versioning the `__linux` virtual package,
# discard everything after the last digit of the third or fourth
# numeric component; note that this breaks version ordering for
# development (`-rcN`) kernels, but we'll deal with that later.
dist_version = os.environ.get('CONDA_OVERRIDE_LINUX', context.platform_system_release[1])
if '-' in dist_version:
dist_version = dist_version.split('-')[0]
rec = _make_virtual_package('__linux', dist_version)
m = re.match(r'\d+\.\d+(\.\d+)?(\.\d+)?', dist_version)
rec = _make_virtual_package('__linux', m.group() if m else "0")
index[rec] = rec

if not (libc_family and libc_version):
# Default to glibc when using CONDA_SUBDIR var
libc_family = "glibc"
Expand Down
36 changes: 18 additions & 18 deletions tests/core/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ def test_supplement_index_with_system_osx():


@pytest.mark.skipif(not on_linux, reason="linux-only test")
def test_supplement_index_with_system_linux():
index = {}
with env_vars({'CONDA_OVERRIDE_LINUX': '4.2.0'}):
_supplement_index_with_system(index)

linux_pkg = next(iter(_ for _ in index if _.name == '__linux'))
assert linux_pkg.version == '4.2.0'
assert linux_pkg.package_type == PackageType.VIRTUAL_SYSTEM


@pytest.mark.skipif(not on_linux, reason="linux-only test")
def test_supplement_index_with_system_linux_extended():
index = {}
with env_vars({'CONDA_OVERRIDE_LINUX': '4.2.0-42-generic'}):
_supplement_index_with_system(index)
@pytest.mark.parametrize("release_str,version", [
("1.2.3.4", "1.2.3.4"), # old numbering system
("4.2", "4.2"),
("4.2.1", "4.2.1"),
("4.2.0-42-generic", "4.2.0"),
("5.4.89+", "5.4.89"),
("5.5-rc1", "5.5"),
("9.1.a", "9.1"), # should probably be "0"
("9.1.a.2", "9.1"), # should probably be "0"
("9.a.1", "0"),
])
def test_supplement_index_with_system_linux(release_str, version):
index = {}
with env_vars({'CONDA_OVERRIDE_LINUX': release_str}):
_supplement_index_with_system(index)

linux_pkg = next(iter(_ for _ in index if _.name == '__linux'))
assert linux_pkg.version == '4.2.0'
assert linux_pkg.package_type == PackageType.VIRTUAL_SYSTEM
linux_pkg = next(iter(_ for _ in index if _.name == '__linux'))
assert linux_pkg.version == version
assert linux_pkg.package_type == PackageType.VIRTUAL_SYSTEM


@pytest.mark.skipif(on_win or on_mac, reason="linux-only test")
Expand Down

0 comments on commit 0c9e4c6

Please sign in to comment.