Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

activate.fish: support activating conda in fish shells #342

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ jobs:
include:
# include the appropriate dependencies for testing SquashFS on each OS
- os: macos-12
squashfs_deps: "conda-forge::squashfs-tools"
conda_deps: "conda-forge::squashfs-tools conda-forge::fish"
- os: ubuntu-latest
squashfs_deps: "conda-forge::squashfs-tools conda-forge::squashfuse"
conda_deps: "conda-forge::squashfs-tools conda-forge::squashfuse conda-forge::fish"
- os: windows-latest
squashfs_deps: ""
conda_deps: ""
steps:
- name: Retrieve the source code
uses: actions/checkout@v4
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
source $CONDA_ROOT/etc/profile.d/conda.sh
conda info -a
mv conda-bld $CONDA_ROOT/conda-bld
conda create -n cptest local::conda-pack conda-forge::pytest conda-forge::pytest-cov defaults::python=${{ matrix.pyver }} ${{ matrix.squashfs_deps }}
conda create -n cptest local::conda-pack conda-forge::pytest conda-forge::pytest-cov defaults::python=${{ matrix.pyver }} ${{ matrix.conda_deps }}
conda activate cptest
pytest -v -ss --cov=conda_pack --cov-branch --cov-report=xml conda_pack/tests
- uses: codecov/codecov-action@v4
Expand Down
4 changes: 3 additions & 1 deletion conda_pack/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class CondaPackException(Exception):
_scripts = [(os.path.join(_current_dir, 'scripts', 'posix', 'activate'),
os.path.join(BIN_DIR, 'activate')),
(os.path.join(_current_dir, 'scripts', 'posix', 'deactivate'),
os.path.join(BIN_DIR, 'deactivate'))]
os.path.join(BIN_DIR, 'deactivate')),
(os.path.join(_current_dir, 'scripts', 'posix', 'activate.fish'),
os.path.join(BIN_DIR, 'activate.fish'))]


class _Context:
Expand Down
62 changes: 62 additions & 0 deletions conda_pack/scripts/posix/activate.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function _activate
set -l full_path_script_dir (cd (dirname (status -f)); and pwd)
set -l full_path_env (dirname "$full_path_script_dir")
set env_name (basename "$full_path_env")

# If there's already a source env
if [ -n "$CONDA_PREFIX" ]
# If the source env differs from this env
if [ "$CONDA_PREFIX" != "$full_path_env" ]
deactivate
else
return 0 # nothing to do
end
end

export CONDA_PREFIX="$full_path_env"
set _OLD_PATH "$PATH"
set PATH "$full_path_env/bin:$PATH"

functions -c fish_prompt _old_fish_prompt
function fish_prompt
# Run the user's prompt first; it might depend on (pipe)status.
set -l prompt (_old_fish_prompt)

printf "($env_name) "

string join -- \n $prompt # handle multi-line prompts
end

# Run the activate scripts
set -l _script_dir "$full_path_env/etc/conda/activate.d"
if [ -d "$_script_dir" ] && [ -n "(ls -A "$_script_dir")" ]
set -l _path
for _path in "$_script_dir"/*.sh
# Normally these are sourced but given they're not-fish scripts it will
# error out. Run them anyways for any side effects.
sh "$_path"
end
for _path in "$_script_dir"/*.fish
. "$_path"
end
end
end

function deactivate -d 'Exit conda mode and return to the normal environment.'
# reset old environment variables
if test -n "$_OLD_PATH"
set -gx PATH $_OLD_PATH
set -e _OLD_PATH
end

if functions -q _old_fish_prompt
# Erase virtualenv's `fish_prompt` and restore the original.
functions -e fish_prompt
functions -c _old_fish_prompt fish_prompt
functions -e _old_fish_prompt
end

set -e CONDA_PREFIX
end

_activate
14 changes: 13 additions & 1 deletion conda_pack/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def test_pack(tmpdir, py37_env):
fnames = ('conda-unpack.exe', 'conda-unpack-script.py',
'activate.bat', 'deactivate.bat')
else:
fnames = ('conda-unpack', 'activate', 'deactivate')
fnames = ('conda-unpack', 'activate', 'deactivate', 'activate.fish')
assert diff == {os.path.join(BIN_DIR_L, f) for f in fnames}


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

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

assert out == 'Done\n'

# fish
command = (". {path}/bin/activate.fish && "
"python -c 'import sys; print(sys.executable)' && "
"deactivate && "
"echo 'Done'").format(path=extract_path)

out = subprocess.check_output(['/usr/bin/env', 'fish', '-c', command],
stderr=subprocess.STDOUT).decode()
assert "test_activate0" in out
assert "Done\n" in out
Loading