Skip to content

Commit bc1ef6a

Browse files
d4l3kxhochy
andauthored
activate.fish: support activating conda in fish shells (#342)
Co-authored-by: Uwe L. Korn <[email protected]>
1 parent cfcccdf commit bc1ef6a

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ jobs:
8686
include:
8787
# include the appropriate dependencies for testing SquashFS on each OS
8888
- os: macos-12
89-
squashfs_deps: "conda-forge::squashfs-tools"
89+
conda_deps: "conda-forge::squashfs-tools conda-forge::fish"
9090
- os: ubuntu-latest
91-
squashfs_deps: "conda-forge::squashfs-tools conda-forge::squashfuse"
91+
conda_deps: "conda-forge::squashfs-tools conda-forge::squashfuse conda-forge::fish"
9292
- os: windows-latest
93-
squashfs_deps: ""
93+
conda_deps: ""
9494
steps:
9595
- name: Retrieve the source code
9696
uses: actions/checkout@v4
@@ -118,7 +118,7 @@ jobs:
118118
source $CONDA_ROOT/etc/profile.d/conda.sh
119119
conda info -a
120120
mv conda-bld $CONDA_ROOT/conda-bld
121-
conda create -n cptest local::conda-pack conda-forge::pytest conda-forge::pytest-cov defaults::python=${{ matrix.pyver }} ${{ matrix.squashfs_deps }}
121+
conda create -n cptest local::conda-pack conda-forge::pytest conda-forge::pytest-cov defaults::python=${{ matrix.pyver }} ${{ matrix.conda_deps }}
122122
conda activate cptest
123123
pytest -v -ss --cov=conda_pack --cov-branch --cov-report=xml conda_pack/tests
124124
- uses: codecov/codecov-action@v4

conda_pack/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class CondaPackException(Exception):
4242
_scripts = [(os.path.join(_current_dir, 'scripts', 'posix', 'activate'),
4343
os.path.join(BIN_DIR, 'activate')),
4444
(os.path.join(_current_dir, 'scripts', 'posix', 'deactivate'),
45-
os.path.join(BIN_DIR, 'deactivate'))]
45+
os.path.join(BIN_DIR, 'deactivate')),
46+
(os.path.join(_current_dir, 'scripts', 'posix', 'activate.fish'),
47+
os.path.join(BIN_DIR, 'activate.fish'))]
4648

4749

4850
class _Context:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function _activate
2+
set -l full_path_script_dir (cd (dirname (status -f)); and pwd)
3+
set -l full_path_env (dirname "$full_path_script_dir")
4+
set env_name (basename "$full_path_env")
5+
6+
# If there's already a source env
7+
if [ -n "$CONDA_PREFIX" ]
8+
# If the source env differs from this env
9+
if [ "$CONDA_PREFIX" != "$full_path_env" ]
10+
deactivate
11+
else
12+
return 0 # nothing to do
13+
end
14+
end
15+
16+
export CONDA_PREFIX="$full_path_env"
17+
set _OLD_PATH "$PATH"
18+
set PATH "$full_path_env/bin:$PATH"
19+
20+
functions -c fish_prompt _old_fish_prompt
21+
function fish_prompt
22+
# Run the user's prompt first; it might depend on (pipe)status.
23+
set -l prompt (_old_fish_prompt)
24+
25+
printf "($env_name) "
26+
27+
string join -- \n $prompt # handle multi-line prompts
28+
end
29+
30+
# Run the activate scripts
31+
set -l _script_dir "$full_path_env/etc/conda/activate.d"
32+
if [ -d "$_script_dir" ] && [ -n "(ls -A "$_script_dir")" ]
33+
set -l _path
34+
for _path in "$_script_dir"/*.sh
35+
# Normally these are sourced but given they're not-fish scripts it will
36+
# error out. Run them anyways for any side effects.
37+
sh "$_path"
38+
end
39+
for _path in "$_script_dir"/*.fish
40+
. "$_path"
41+
end
42+
end
43+
end
44+
45+
function deactivate -d 'Exit conda mode and return to the normal environment.'
46+
# reset old environment variables
47+
if test -n "$_OLD_PATH"
48+
set -gx PATH $_OLD_PATH
49+
set -e _OLD_PATH
50+
end
51+
52+
if functions -q _old_fish_prompt
53+
# Erase virtualenv's `fish_prompt` and restore the original.
54+
functions -e fish_prompt
55+
functions -c _old_fish_prompt fish_prompt
56+
functions -e _old_fish_prompt
57+
end
58+
59+
set -e CONDA_PREFIX
60+
end
61+
62+
_activate

conda_pack/tests/test_core.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def test_pack(tmpdir, py37_env):
509509
fnames = ('conda-unpack.exe', 'conda-unpack-script.py',
510510
'activate.bat', 'deactivate.bat')
511511
else:
512-
fnames = ('conda-unpack', 'activate', 'deactivate')
512+
fnames = ('conda-unpack', 'activate', 'deactivate', 'activate.fish')
513513
assert diff == {os.path.join(BIN_DIR_L, f) for f in fnames}
514514

515515

@@ -622,6 +622,7 @@ def test_activate(tmpdir):
622622
assert out == 'CONDAPACK_ACTIVATED=1\r\nCONDAPACK_ACTIVATED=\r\nDone\r\n'
623623

624624
else:
625+
# bash
625626
command = (". {path}/bin/activate && "
626627
"test $CONDAPACK_ACTIVATED -eq 1 && "
627628
". {path}/bin/deactivate && "
@@ -632,3 +633,14 @@ def test_activate(tmpdir):
632633
stderr=subprocess.STDOUT).decode()
633634

634635
assert out == 'Done\n'
636+
637+
# fish
638+
command = (". {path}/bin/activate.fish && "
639+
"python -c 'import sys; print(sys.executable)' && "
640+
"deactivate && "
641+
"echo 'Done'").format(path=extract_path)
642+
643+
out = subprocess.check_output(['/usr/bin/env', 'fish', '-c', command],
644+
stderr=subprocess.STDOUT).decode()
645+
assert "test_activate0" in out
646+
assert "Done\n" in out

0 commit comments

Comments
 (0)