Skip to content

Commit 5834f14

Browse files
authored
Merge pull request #942 from yuvipanda/conda-channels
Add the ability to define conda channels in plugins via `tljh_extra_user_conda_channels`
2 parents 50c1b5f + 9111b73 commit 5834f14

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

integration-tests/plugins/simplest/tljh_simplest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

88
@hookimpl
99
def tljh_extra_user_conda_packages():
10-
return ["tqdm"]
10+
# tqdm installs from the conda-forge channel (https://conda-forge.org/packages/)
11+
# csvtk installs from the bioconda channel (https://bioconda.github.io/conda-package_index.html)
12+
return ["tqdm", "csvtk"]
13+
14+
15+
@hookimpl
16+
def tljh_extra_user_conda_channels():
17+
return ["conda-forge", "bioconda"]
1118

1219

1320
@hookimpl

integration-tests/test_simplest_plugin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def test_tljh_extra_hub_pip_packages():
2727
subprocess.check_call([f"{HUB_ENV_PREFIX}/bin/python3", "-c", "import there"])
2828

2929

30+
def test_conda_packages():
31+
"""
32+
Test extra user conda packages are installed from multiple channels.
33+
34+
- tqdm installs from the conda-forge channel (https://conda-forge.org/packages/)
35+
- csvtk installs from the bioconda channel (https://bioconda.github.io/conda-package_index.html)
36+
"""
37+
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/python3", "-c", "import tqdm"])
38+
subprocess.check_call([f"{USER_ENV_PREFIX}/bin/csvtk", "cat", "--help"])
39+
40+
3041
def test_tljh_extra_apt_packages():
3142
assert os.path.exists("/usr/games/sl")
3243

tests/test_conda.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def test_ensure_packages(prefix):
3434
subprocess.check_call([os.path.join(prefix, "bin", "python"), "-c", "import numpy"])
3535

3636

37+
def test_ensure_channel_packages(prefix):
38+
"""
39+
Test installing packages in conda environment
40+
"""
41+
conda.ensure_conda_packages(prefix, ["csvtk"], channels=("conda-forge", "bioconda"))
42+
# Throws an error if this fails
43+
subprocess.check_call([os.path.join(prefix, "bin", "csvtk"), "cat", "--help"])
44+
45+
3746
def test_ensure_pip_packages(prefix):
3847
"""
3948
Test installing pip packages in conda environment

tljh/conda.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ def install_miniconda(installer_path, prefix):
9999
fix_permissions(prefix)
100100

101101

102-
def ensure_conda_packages(prefix, packages, force_reinstall=False):
102+
def ensure_conda_packages(
103+
prefix, packages, channels=("conda-forge",), force_reinstall=False
104+
):
103105
"""
104-
Ensure packages (from conda-forge) are installed in the conda prefix.
106+
Ensure packages (from channels) are installed in the conda prefix.
105107
106108
Note that conda seem to update dependencies by default, so there is probably
107109
no need to have a update parameter exposed for this function.
@@ -118,13 +120,14 @@ def ensure_conda_packages(prefix, packages, force_reinstall=False):
118120
# avoids problems with RemoveError upgrading conda from old versions
119121
cmd += ["--force-reinstall"]
120122

123+
for channel in channels:
124+
cmd += ["-c", channel]
125+
121126
abspath = os.path.abspath(prefix)
122127

123128
utils.run_subprocess(
124129
cmd
125130
+ [
126-
"-c",
127-
"conda-forge", # Make customizable if we ever need to
128131
"--prefix",
129132
abspath,
130133
]

tljh/hooks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def tljh_extra_user_conda_packages():
1515
"""
1616

1717

18+
@hookspec
19+
def tljh_extra_user_conda_channels():
20+
"""
21+
Return a list of conda channels to be used during user environment installation.
22+
"""
23+
24+
1825
@hookspec
1926
def tljh_extra_user_pip_packages():
2027
"""

tljh/installer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,18 @@ def run_plugin_actions(plugin_manager):
449449

450450
# Install conda packages
451451
conda_packages = list(set(itertools.chain(*hook.tljh_extra_user_conda_packages())))
452+
conda_channels = list(itertools.chain(*hook.tljh_extra_user_conda_channels()))
453+
if len(conda_channels) == 0:
454+
conda_channels = ("conda-forge",)
452455
if conda_packages:
453456
logger.info(
454457
"Installing {} user conda packages collected from plugins: {}".format(
455458
len(conda_packages), " ".join(conda_packages)
456459
)
457460
)
458-
conda.ensure_conda_packages(USER_ENV_PREFIX, conda_packages)
461+
conda.ensure_conda_packages(
462+
USER_ENV_PREFIX, conda_packages, channels=conda_channels
463+
)
459464

460465
# Install pip packages
461466
user_pip_packages = list(set(itertools.chain(*hook.tljh_extra_user_pip_packages())))

0 commit comments

Comments
 (0)