Skip to content

Commit

Permalink
BUG - Use string substitution in normalized_channel_url (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Karetnikov authored Dec 20, 2023
1 parent de5c7b4 commit 703b001
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
34 changes: 22 additions & 12 deletions conda-store-server/conda_store_server/conda_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<channel>/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,
Expand All @@ -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/<channel>/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:
Expand Down
11 changes: 11 additions & 0 deletions conda-store-server/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

import pytest
import yarl
from conda_store_server import (
BuildKey,
action,
Expand Down Expand Up @@ -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)

0 comments on commit 703b001

Please sign in to comment.