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

Use string substitution in normalized_channel_url #710

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
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
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 @@ -346,3 +347,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)
Loading