From a7b285ee0ed2391ababc2d966aef729f118105f3 Mon Sep 17 00:00:00 2001 From: Nikita Karetnikov Date: Mon, 11 Dec 2023 05:13:23 +0100 Subject: [PATCH] Set channel priority to strict, print config info Fixes #359, fixes #659. --- .../action/generate_lockfile.py | 34 +++++++++++++++---- conda-store-server/tests/test_actions.py | 23 +++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/conda-store-server/conda_store_server/action/generate_lockfile.py b/conda-store-server/conda_store_server/action/generate_lockfile.py index 45a7e50df..d22e7a6f5 100644 --- a/conda-store-server/conda_store_server/action/generate_lockfile.py +++ b/conda-store-server/conda_store_server/action/generate_lockfile.py @@ -1,5 +1,7 @@ import json +import os import pathlib +import subprocess import typing import yaml @@ -13,6 +15,8 @@ def action_solve_lockfile( conda_command: str, specification: schema.CondaSpecification, platforms: typing.List[str] = [conda_utils.conda_platform()], + # https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-channels.html + conda_flags: str = "--strict-channel-priority", ): environment_filename = pathlib.Path.cwd() / "environment.yaml" lockfile_filename = pathlib.Path.cwd() / "conda-lock.yaml" @@ -20,12 +24,30 @@ def action_solve_lockfile( with environment_filename.open("w") as f: json.dump(specification.dict(), f) - run_lock( - environment_files=[environment_filename], - platforms=platforms, - lockfile_path=lockfile_filename, - conda_exe=conda_command, - ) + def print_cmd(cmd): + print(f"Running command: {' '.join(cmd)}") + print(subprocess.check_output(cmd, stderr=subprocess.STDOUT, encoding="utf-8")) + + # The info command can be used with either mamba or conda + print_cmd([conda_command, "info"]) + # The config command is not supported by mamba + print_cmd(["conda", "config", "--show"]) + print_cmd(["conda", "config", "--show-sources"]) + + # CONDA_FLAGS is used by conda-lock in conda_solver.solve_specs_for_arch + try: + conda_flags_name = "CONDA_FLAGS" + print(f"{conda_flags_name}={conda_flags}") + os.environ[conda_flags_name] = conda_flags + + run_lock( + environment_files=[environment_filename], + platforms=platforms, + lockfile_path=lockfile_filename, + conda_exe=conda_command, + ) + finally: + os.environ.pop(conda_flags_name, None) with lockfile_filename.open() as f: return yaml.safe_load(f) diff --git a/conda-store-server/tests/test_actions.py b/conda-store-server/tests/test_actions.py index e3a455489..feda88127 100644 --- a/conda-store-server/tests/test_actions.py +++ b/conda-store-server/tests/test_actions.py @@ -69,6 +69,29 @@ def test_solve_lockfile(conda_store, specification, request): assert len(context.result["package"]) != 0 +def test_solve_lockfile_valid_conda_flags(conda_store, simple_specification): + context = action.action_solve_lockfile( + conda_command=conda_store.conda_command, + specification=simple_specification, + platforms=[conda_utils.conda_platform()], + conda_flags="--strict-channel-priority", + ) + assert len(context.result["package"]) != 0 + + +# Checks that conda_flags is used by conda-lock +def test_solve_lockfile_invalid_conda_flags(conda_store, simple_specification): + with pytest.raises(Exception, match=( + r"Command.*--this-is-invalid.*returned non-zero exit status" + )): + action.action_solve_lockfile( + conda_command=conda_store.conda_command, + specification=simple_specification, + platforms=[conda_utils.conda_platform()], + conda_flags="--this-is-invalid", + ) + + @pytest.mark.parametrize( "specification", [