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

Initialize mamba context with Solver.channels explicitly #525

Merged
merged 27 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e016dce
Initialize mamba context with Solver arguments explicitly
jaimergp Sep 12, 2024
6f862f3
add news
jaimergp Sep 12, 2024
b1f493d
Merge branch 'main' into explicit-channels
jaimergp Sep 19, 2024
282348c
fix windows workflow?
jaimergp Sep 19, 2024
055f146
Merge branch 'explicit-channels' of github.com:conda/conda-libmamba-s…
jaimergp Sep 19, 2024
27920c9
use bash?
jaimergp Sep 19, 2024
c32e3b0
try this way
jaimergp Sep 19, 2024
e0a0274
hardcode path
jaimergp Sep 19, 2024
487b654
conda run?
jaimergp Sep 19, 2024
8f26a43
this is not needed at all? :thinking:
jaimergp Sep 19, 2024
462d9c2
only install these when needed
jaimergp Sep 19, 2024
f2b9435
do not run these unless condition matches
jaimergp Sep 19, 2024
d52f1a3
fix exit codes
jaimergp Sep 20, 2024
6197009
revert this other bit?
jaimergp Sep 20, 2024
0fa32c4
via python?
jaimergp Sep 20, 2024
1a80269
revert accidental commits
jaimergp Sep 21, 2024
2852b1b
tidy up
jaimergp Sep 21, 2024
6594b23
pass arguments explicitly
jaimergp Sep 21, 2024
6c4e693
Try with two steps?
jaimergp Sep 23, 2024
f5bc2f1
Merge branch 'main' into explicit-channels
jaimergp Sep 24, 2024
752a9d2
debug
jaimergp Sep 24, 2024
9185611
try a different action
jaimergp Sep 24, 2024
d2ca357
Workaround for strange target_prefix = "/" -> "\\conda-bld" conversio…
jaimergp Sep 24, 2024
6770d82
annotations
jaimergp Sep 24, 2024
db174dc
fix typing
jaimergp Sep 24, 2024
8a0ca65
Merge branch 'main' into explicit-channels
jaimergp Sep 24, 2024
c6298e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
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
29 changes: 23 additions & 6 deletions conda_libmamba_solver/mamba_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@
# Decide what to do with it when we split into a plugin
# 2022.02.15: updated vendored parts to v0.21.2
# 2022.11.14: only keeping channel prioritization and context initialization logic now
# 2024.09.24: parameterize init_api_context

from __future__ import annotations

import logging
from functools import lru_cache
from importlib.metadata import version
from typing import Dict
from pathlib import Path
from typing import TYPE_CHECKING, Iterable

import libmambapy as api
from conda.base.constants import ChannelPriority
from conda.base.context import context
from conda.common.compat import on_win

if TYPE_CHECKING:
from .index import _ChannelRepoInfo


log = logging.getLogger(f"conda.{__name__}")

Expand All @@ -33,7 +42,7 @@ def _get_base_url(url, name=None):
return tmp


def set_channel_priorities(index: Dict[str, "_ChannelRepoInfo"], has_priority: bool = None):
def set_channel_priorities(index: dict[str, _ChannelRepoInfo], has_priority: bool = None):
"""
This function was part of mamba.utils.load_channels originally.
We just split it to reuse it a bit better.
Expand Down Expand Up @@ -84,7 +93,11 @@ def set_channel_priorities(index: Dict[str, "_ChannelRepoInfo"], has_priority: b
return index


def init_api_context() -> api.Context:
def init_api_context(
channels: Iterable[str] | None = None,
platform: str = None,
target_prefix: str = None,
) -> api.Context:
# This function has to be called BEFORE 1st initialization of the context
api.Context.use_default_signal_handler(False)
api_ctx = api.Context()
Expand All @@ -100,7 +113,11 @@ def init_api_context() -> api.Context:
# Prefix params
api_ctx.prefix_params.conda_prefix = context.conda_prefix
api_ctx.prefix_params.root_prefix = context.root_prefix
api_ctx.prefix_params.target_prefix = context.target_prefix
if on_win and target_prefix == "/":
# workaround for strange bug in libmamba transforming "/"" into "\\conda-bld" :shrug:
target_prefix = Path.cwd().parts[0]
target_prefix = target_prefix if target_prefix is not None else context.target_prefix
api_ctx.prefix_params.target_prefix = target_prefix

# Networking params -- we always operate offline from libmamba's perspective
api_ctx.remote_fetch_params.user_agent = context.user_agent
Expand All @@ -118,8 +135,8 @@ def init_api_context() -> api.Context:
api_ctx.use_only_tar_bz2 = context.use_only_tar_bz2

# Channels and platforms
api_ctx.platform = context.subdir
api_ctx.channels = context.channels
api_ctx.platform = platform if platform is not None else context.subdir
api_ctx.channels = list(channels) if channels is not None else context.channels
api_ctx.channel_alias = str(_get_base_url(context.channel_alias.url(with_credentials=True)))

RESERVED_NAMES = {"local", "defaults"}
Expand Down
9 changes: 8 additions & 1 deletion conda_libmamba_solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def solve_final_state(
return none_or_final_state

# From now on we _do_ require a solver and the index
init_api_context()
subdirs = self.subdirs
if self._called_from_conda_build():
log.info("Using solver via 'conda.plan.install_actions' (probably conda build)")
Expand Down Expand Up @@ -216,6 +215,14 @@ def solve_final_state(
channel = Channel(**{k: v for k, v in channel.dump().items() if k != "platform"})
deduped_channels[channel] = None
all_channels = tuple(deduped_channels)

# Now have all the info we need to initialize the libmamba context
init_api_context(
channels=[c.canonical_name for c in all_channels],
platform=next(s for s in self.subdirs if s != "noarch"),
target_prefix=str(self.prefix),
)

with Spinner(
self._spinner_msg_metadata(all_channels, conda_bld_channels=conda_bld_channels),
enabled=not context.verbosity and not context.quiet,
Expand Down
19 changes: 19 additions & 0 deletions news/525-explicit-context
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Use `Solver` instance configuration to initialize the `libmamba` context without implicitly relying on the `conda` context settings. (#525)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
Loading