diff --git a/conda-store-server/conda_store_server/conda_utils.py b/conda-store-server/conda_store_server/conda_utils.py index a2a88d868..9db4f5ee7 100644 --- a/conda-store-server/conda_store_server/conda_utils.py +++ b/conda-store-server/conda_store_server/conda_utils.py @@ -109,6 +109,27 @@ def conda_lock(specification: "CondaSpecification", conda_exe: str = "mamba"): return {"conda": conda_packages, "pip": pip_packages} +def get_channel_url(channel: str) -> yarl.URL: + # the conda main channel does not have a channeldata.json within + # the https://conda.anaconda.org//channeldata.json + # so we replace the given url with it's equivalent alias + channel_replacements = { + "https://conda.anaconda.org/main/": yarl.URL( + "https://repo.anaconda.com/pkgs/main" + ) + } + + # Note: this doesn't use the / operator to append the trailing / character + # because it's ignored on Windows. Instead, string substitution is used if + # the / character is not present + if channel.endswith("/"): + normalized_channel_url = yarl.URL(channel) + else: + normalized_channel_url = yarl.URL(f"{channel}/") + + return channel_replacements.get(str(normalized_channel_url), yarl.URL(channel)) + + def download_repodata( channel: str, last_update: datetime.datetime = None, @@ -124,18 +145,7 @@ def download_repodata( """ subdirs = set(subdirs or [conda_platform(), "noarch"]) - # the conda main channel does not have a channeldata.json within - # the https://conda.anaconda.org//channeldata.json - # so we replace the given url with it's equivalent alias - channel_replacements = { - "https://conda.anaconda.org/main/": yarl.URL( - "https://repo.anaconda.com/pkgs/main" - ) - } - normalized_channel_url = yarl.URL(channel) / "./" - channel_url = channel_replacements.get( - str(normalized_channel_url), yarl.URL(channel) - ) + channel_url = get_channel_url(channel) headers = {} if last_update: diff --git a/conda-store-server/tests/test_actions.py b/conda-store-server/tests/test_actions.py index feda88127..8d79e3ea4 100644 --- a/conda-store-server/tests/test_actions.py +++ b/conda-store-server/tests/test_actions.py @@ -5,6 +5,7 @@ import sys import pytest +import yarl from conda_store_server import ( BuildKey, action, @@ -369,3 +370,13 @@ def lockfile_url(build_key): assert lockfile_url(build_key) == build.conda_lock_key assert lockfile_url(build_key) == res.headers['location'] assert res.status_code == 307 + + +def test_get_channel_url(): + conda_main = "https://conda.anaconda.org/main" + repo_main = "https://repo.anaconda.com/pkgs/main" + example = "https://example.com" + + assert conda_utils.get_channel_url(conda_main) == yarl.URL(repo_main) + assert conda_utils.get_channel_url(f"{conda_main}/") == yarl.URL(repo_main) + assert conda_utils.get_channel_url(example) == yarl.URL(example)