Skip to content

Commit

Permalink
Set channel priority to strict, print config info
Browse files Browse the repository at this point in the history
Fixes #359, fixes #659.
  • Loading branch information
nkaretnikov committed Dec 11, 2023
1 parent 39b8bf3 commit a7b285e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
34 changes: 28 additions & 6 deletions conda-store-server/conda_store_server/action/generate_lockfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import pathlib
import subprocess
import typing

import yaml
Expand All @@ -13,19 +15,39 @@ 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"

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)
23 changes: 23 additions & 0 deletions conda-store-server/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down

0 comments on commit a7b285e

Please sign in to comment.