From 58276a08c1c39ee3125e2063e470435a9a7ac117 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Sun, 27 Oct 2024 12:29:59 +0000 Subject: [PATCH 1/4] wip --- asv/plugins/_mamba_helpers.py | 4 ++-- asv/plugins/mamba.py | 30 ++++++++++++++++++------ test/test_environment_bench.py | 42 +++++++++++++++++++--------------- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/asv/plugins/_mamba_helpers.py b/asv/plugins/_mamba_helpers.py index bf8518901..3a5b09b04 100644 --- a/asv/plugins/_mamba_helpers.py +++ b/asv/plugins/_mamba_helpers.py @@ -152,7 +152,7 @@ def load_channels( ) print("Cache path: ", subdir.cache_path()) - repo = subdir.create_repo(pool) + repo = subdir.create_repo() repo.set_priority(priority, subpriority) repos.append(repo) @@ -165,7 +165,7 @@ def __init__(self, channels, platform, context, output_folder=None): self.platform = platform self.context = context self.output_folder = output_folder or "local" - self.pool = libmambapy.Pool() + self.pool = libmambapy.solver.libsolv.Database() self.repos = [] self.index = load_channels( self.pool, self.channels, self.repos, platform=platform diff --git a/asv/plugins/mamba.py b/asv/plugins/mamba.py index 62ad292e7..f14bb48ca 100644 --- a/asv/plugins/mamba.py +++ b/asv/plugins/mamba.py @@ -4,6 +4,9 @@ import re from pathlib import Path +import libmambapy.solver +import libmambapy.solver.libsolv +import libmambapy.specs import yaml from yaml import load @@ -12,7 +15,7 @@ except ImportError: from yaml import Loader -from ._mamba_helpers import libmambapy, MambaSolver +import libmambapy from .. import environment, util from ..console import log @@ -138,7 +141,10 @@ def _setup(self): env_data = load(Path(env_file_name).open(), Loader=Loader) mamba_pkgs = [x for x in env_data.get("dependencies", []) if isinstance(x, str)] self._mamba_channels += [x for x in env_data.get("channels", []) if isinstance(x, str)] - self._mamba_channels = list(dict.fromkeys(self._mamba_channels).keys()) + # self._mamba_channels = list(dict.fromkeys(self._mamba_channels).keys()) + self._mamba_channels = libmambapy.specs.ChannelResolveParams.name_map( + (x, libmambapy.specs.Channel(x)) for x in self._mamba_channels + ) # Handle possible pip keys pip_maybe = [x for x in env_data.get("dependencies", []) if isinstance(x, dict)] if len(pip_maybe) == 1: @@ -146,13 +152,23 @@ def _setup(self): pip_args += pip_maybe[0]["pip"] except KeyError: raise KeyError("Only pip is supported as a secondary key") - mamba_pkgs += mamba_args - solver = MambaSolver( - self._mamba_channels, None, self.context # or target_platform + Request = libmambapy.solver.Request + MatchSpec = libmambapy.specs.MatchSpec + request = Request( + jobs=[ + Request.Install(MatchSpec.parse(pkg)) for pkg in mamba_pkgs + ], + # flags=Request.Flags( + # arg for arg in mamba_args + # ), + ) + solver = libmambapy.solver.libsolv.Solver() + db = libmambapy.solver.libsolv.Database( + libmambapy.specs.ChannelResolveParams(channel_alias="https://conda.anaconda.org") ) with _mamba_lock(): - transaction = solver.solve(mamba_pkgs) - transaction.execute(libmambapy.PrefixData(self._path)) + transaction = solver.solve(db, request) + # transaction.execute(libmambapy.PrefixData(self._path)) if pip_args: for declaration in pip_args: parsed_declaration = util.ParsedPipDeclaration(declaration) diff --git a/test/test_environment_bench.py b/test/test_environment_bench.py index 7a957f8d8..4a90791eb 100644 --- a/test/test_environment_bench.py +++ b/test/test_environment_bench.py @@ -146,31 +146,35 @@ def test_asv_benchmark(asv_project_factory, env): ) @pytest.mark.skipif(not tools.HAS_MAMBA, reason="needs mamba") -def test_asv_mamba( +def test_asv_mamba_f( asv_project_factory, config_modifier, expected_success, expected_error ): """ Test running ASV benchmarks with various configurations, checking for specific errors when failures are expected. """ - project_dir = asv_project_factory(custom_config=config_modifier) - try: - subprocess.run( - ["asv", "run", "--quick", "--dry-run", "--environment", "mamba"], - cwd=project_dir, - check=True, - capture_output=True, - text=True, - ) - if not expected_success: - pytest.fail("Expected failure, but succeeded") - except subprocess.CalledProcessError as exc: - if expected_success: - pytest.fail(f"ASV benchmark unexpectedly failed: {exc.stderr}") - elif expected_error and expected_error not in exc.stderr: - pytest.fail( - f"Expected error '{expected_error}' not found in stderr: {exc.stderr}" - ) + import libmambapy + db = libmambapy.solver.libsolv.Database( + libmambapy.specs.ChannelResolveParams(channel_alias="https://conda.anaconda.org") + ) + # project_dir = asv_project_factory(custom_config=config_modifier) + # try: + # subprocess.run( + # ["asv", "run", "--quick", "--dry-run", "--environment", "mamba"], + # cwd=project_dir, + # check=True, + # capture_output=True, + # text=True, + # ) + # if not expected_success: + # pytest.fail("Expected failure, but succeeded") + # except subprocess.CalledProcessError as exc: + # if expected_success: + # pytest.fail(f"ASV benchmark unexpectedly failed: {exc.stderr}") + # elif expected_error and expected_error not in exc.stderr: + # pytest.fail( + # f"Expected error '{expected_error}' not found in stderr: {exc.stderr}" + # ) @pytest.mark.parametrize( From b260e698859d3a3889e61afb75dd4b52189c32c2 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Wed, 30 Oct 2024 12:15:32 +0000 Subject: [PATCH 2/4] wip --- asv/plugins/mamba.py | 29 +++++++++++++++--------- test/test_environment_bench.py | 40 +++++++++++++++------------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/asv/plugins/mamba.py b/asv/plugins/mamba.py index f14bb48ca..e6b4494c3 100644 --- a/asv/plugins/mamba.py +++ b/asv/plugins/mamba.py @@ -82,6 +82,7 @@ def __init__(self, conf, python, requirements, tagged_env_vars): with mambarc_path.open() as f: condarc_data = yaml.safe_load(f) self._apply_condarc_settings(condarc_data) + self.channel_context = libmambapy.ChannelContext.make_conda_compatible(self.context) def _apply_condarc_settings(self, condarc_data): # Apply channel settings @@ -141,10 +142,7 @@ def _setup(self): env_data = load(Path(env_file_name).open(), Loader=Loader) mamba_pkgs = [x for x in env_data.get("dependencies", []) if isinstance(x, str)] self._mamba_channels += [x for x in env_data.get("channels", []) if isinstance(x, str)] - # self._mamba_channels = list(dict.fromkeys(self._mamba_channels).keys()) - self._mamba_channels = libmambapy.specs.ChannelResolveParams.name_map( - (x, libmambapy.specs.Channel(x)) for x in self._mamba_channels - ) + self._mamba_channels = list(dict.fromkeys(self._mamba_channels).keys()) # Handle possible pip keys pip_maybe = [x for x in env_data.get("dependencies", []) if isinstance(x, dict)] if len(pip_maybe) == 1: @@ -152,23 +150,34 @@ def _setup(self): pip_args += pip_maybe[0]["pip"] except KeyError: raise KeyError("Only pip is supported as a secondary key") + mamba_pkgs += mamba_args Request = libmambapy.solver.Request MatchSpec = libmambapy.specs.MatchSpec request = Request( jobs=[ Request.Install(MatchSpec.parse(pkg)) for pkg in mamba_pkgs ], - # flags=Request.Flags( - # arg for arg in mamba_args - # ), ) solver = libmambapy.solver.libsolv.Solver() + ChannelResolveParams = libmambapy.specs.ChannelResolveParams + channels = [self.channel_context.make_channel(chan) for chan in self._mamba_channels] + channel_map = ChannelResolveParams.ChannelMap(dict((text_chan, chan[0]) for (text_chan, chan) in zip(self._mamba_channels, channels))) db = libmambapy.solver.libsolv.Database( - libmambapy.specs.ChannelResolveParams(channel_alias="https://conda.anaconda.org") + ChannelResolveParams(custom_channels=channel_map) ) + ### seemingly working up to here. But how to perform actions? with _mamba_lock(): - transaction = solver.solve(db, request) - # transaction.execute(libmambapy.PrefixData(self._path)) + outcome = solver.solve(db, request) + Solution = libmambapy.solver.Solution + # libmambapy.PrefixData(self._path) + if isinstance(outcome, Solution): + for action in outcome.actions: + if isinstance(action, Solution.Upgrade): + ... + if isinstance(action, Solution.Reinstall): + ... + ... + if pip_args: for declaration in pip_args: parsed_declaration = util.ParsedPipDeclaration(declaration) diff --git a/test/test_environment_bench.py b/test/test_environment_bench.py index 4a90791eb..ff4312d11 100644 --- a/test/test_environment_bench.py +++ b/test/test_environment_bench.py @@ -153,28 +153,24 @@ def test_asv_mamba_f( Test running ASV benchmarks with various configurations, checking for specific errors when failures are expected. """ - import libmambapy - db = libmambapy.solver.libsolv.Database( - libmambapy.specs.ChannelResolveParams(channel_alias="https://conda.anaconda.org") - ) - # project_dir = asv_project_factory(custom_config=config_modifier) - # try: - # subprocess.run( - # ["asv", "run", "--quick", "--dry-run", "--environment", "mamba"], - # cwd=project_dir, - # check=True, - # capture_output=True, - # text=True, - # ) - # if not expected_success: - # pytest.fail("Expected failure, but succeeded") - # except subprocess.CalledProcessError as exc: - # if expected_success: - # pytest.fail(f"ASV benchmark unexpectedly failed: {exc.stderr}") - # elif expected_error and expected_error not in exc.stderr: - # pytest.fail( - # f"Expected error '{expected_error}' not found in stderr: {exc.stderr}" - # ) + project_dir = asv_project_factory(custom_config=config_modifier) + try: + subprocess.run( + ["asv", "run", "--quick", "--dry-run", "--environment", "mamba"], + cwd=project_dir, + check=True, + capture_output=True, + text=True, + ) + if not expected_success: + pytest.fail("Expected failure, but succeeded") + except subprocess.CalledProcessError as exc: + if expected_success: + pytest.fail(f"ASV benchmark unexpectedly failed: {exc.stderr}") + elif expected_error and expected_error not in exc.stderr: + pytest.fail( + f"Expected error '{expected_error}' not found in stderr: {exc.stderr}" + ) @pytest.mark.parametrize( From ca90f87d1e8a58f249a7b74f01619e43d7627d98 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Wed, 30 Oct 2024 12:23:35 +0000 Subject: [PATCH 3/4] wip --- asv/plugins/_mamba_helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asv/plugins/_mamba_helpers.py b/asv/plugins/_mamba_helpers.py index 3a5b09b04..bf8518901 100644 --- a/asv/plugins/_mamba_helpers.py +++ b/asv/plugins/_mamba_helpers.py @@ -152,7 +152,7 @@ def load_channels( ) print("Cache path: ", subdir.cache_path()) - repo = subdir.create_repo() + repo = subdir.create_repo(pool) repo.set_priority(priority, subpriority) repos.append(repo) @@ -165,7 +165,7 @@ def __init__(self, channels, platform, context, output_folder=None): self.platform = platform self.context = context self.output_folder = output_folder or "local" - self.pool = libmambapy.solver.libsolv.Database() + self.pool = libmambapy.Pool() self.repos = [] self.index = load_channels( self.pool, self.channels, self.repos, platform=platform From ec5158697513c9dfa12ba48b99b5ca2ddf1933fe Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Wed, 30 Oct 2024 12:26:07 +0000 Subject: [PATCH 4/4] wip --- asv/plugins/mamba.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/asv/plugins/mamba.py b/asv/plugins/mamba.py index e6b4494c3..34937ddd5 100644 --- a/asv/plugins/mamba.py +++ b/asv/plugins/mamba.py @@ -4,9 +4,6 @@ import re from pathlib import Path -import libmambapy.solver -import libmambapy.solver.libsolv -import libmambapy.specs import yaml from yaml import load