Merge pull request #942 from yuvipanda/conda-channels

Add the ability to define conda channels in plugins via `tljh_extra_user_conda_channels`
This commit is contained in:
Erik Sundell
2024-09-04 16:14:10 +02:00
committed by GitHub
6 changed files with 48 additions and 6 deletions

View File

@@ -7,7 +7,14 @@ from tljh.hooks import hookimpl
@hookimpl
def tljh_extra_user_conda_packages():
return ["tqdm"]
# tqdm installs from the conda-forge channel (https://conda-forge.org/packages/)
# csvtk installs from the bioconda channel (https://bioconda.github.io/conda-package_index.html)
return ["tqdm", "csvtk"]
@hookimpl
def tljh_extra_user_conda_channels():
return ["conda-forge", "bioconda"]
@hookimpl

View File

@@ -27,6 +27,17 @@ def test_tljh_extra_hub_pip_packages():
subprocess.check_call([f"{HUB_ENV_PREFIX}/bin/python3", "-c", "import there"])
def test_conda_packages():
"""
Test extra user conda packages are installed from multiple channels.
- tqdm installs from the conda-forge channel (https://conda-forge.org/packages/)
- csvtk installs from the bioconda channel (https://bioconda.github.io/conda-package_index.html)
"""
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import tqdm"])
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/csvtk", "cat", "--help"])
def test_tljh_extra_apt_packages():
assert os.path.exists("/usr/games/sl")

View File

@@ -34,6 +34,15 @@ def test_ensure_packages(prefix):
subprocess.check_call([os.path.join(prefix, "bin", "python"), "-c", "import numpy"])
def test_ensure_channel_packages(prefix):
"""
Test installing packages in conda environment
"""
conda.ensure_conda_packages(prefix, ["csvtk"], channels=("conda-forge", "bioconda"))
# Throws an error if this fails
subprocess.check_call([os.path.join(prefix, "bin", "csvtk"), "cat", "--help"])
def test_ensure_pip_packages(prefix):
"""
Test installing pip packages in conda environment

View File

@@ -99,9 +99,11 @@ def install_miniconda(installer_path, prefix):
fix_permissions(prefix)
def ensure_conda_packages(prefix, packages, force_reinstall=False):
def ensure_conda_packages(
prefix, packages, channels=("conda-forge",), force_reinstall=False
):
"""
Ensure packages (from conda-forge) are installed in the conda prefix.
Ensure packages (from channels) are installed in the conda prefix.
Note that conda seem to update dependencies by default, so there is probably
no need to have a update parameter exposed for this function.
@@ -118,13 +120,14 @@ def ensure_conda_packages(prefix, packages, force_reinstall=False):
# avoids problems with RemoveError upgrading conda from old versions
cmd += ["--force-reinstall"]
for channel in channels:
cmd += ["-c", channel]
abspath = os.path.abspath(prefix)
utils.run_subprocess(
cmd
+ [
"-c",
"conda-forge", # Make customizable if we ever need to
"--prefix",
abspath,
]

View File

@@ -15,6 +15,13 @@ def tljh_extra_user_conda_packages():
"""
@hookspec
def tljh_extra_user_conda_channels():
"""
Return a list of conda channels to be used during user environment installation.
"""
@hookspec
def tljh_extra_user_pip_packages():
"""

View File

@@ -449,13 +449,18 @@ def run_plugin_actions(plugin_manager):
# Install conda packages
conda_packages = list(set(itertools.chain(*hook.tljh_extra_user_conda_packages())))
conda_channels = list(itertools.chain(*hook.tljh_extra_user_conda_channels()))
if len(conda_channels) == 0:
conda_channels = ("conda-forge",)
if conda_packages:
logger.info(
"Installing {} user conda packages collected from plugins: {}".format(
len(conda_packages), " ".join(conda_packages)
)
)
conda.ensure_conda_packages(USER_ENV_PREFIX, conda_packages)
conda.ensure_conda_packages(
USER_ENV_PREFIX, conda_packages, channels=conda_channels
)
# Install pip packages
user_pip_packages = list(set(itertools.chain(*hook.tljh_extra_user_pip_packages())))