Skip to content

Commit

Permalink
fix(venv): make relocatable activation scripts support ksh
Browse files Browse the repository at this point in the history
It transpires that detecting the directory a script was sourced from
is non-trivial across `bash`, `ksh` and `zsh`.

The previous version was a one-liner and supported `bash` and `zsh` but
not `ksh`.

It is possible to keep the one-liner and add `ksh` support, but that
is mutually-exclusive with `zsh`.

Therefore, the only way to square this circle is to add an `if` block.
A silver lining here is that although longer, the script is probably
easier to follow as there is less code-golfing going on.
  • Loading branch information
paveldikov committed Jul 30, 2024
1 parent 51b7e9b commit 50041c8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions crates/uv-virtualenv/src/activator/activate
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly


if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
exit 33
# Get script path (only used if environment is relocatable).
if [ -n "${BASH_VERSION:+x}" ] ; then
SCRIPT_PATH="${BASH_SOURCE[0]}"
if [ "$SCRIPT_PATH" = "$0" ]; then
# Only bash has a reasonably robust check for source'dness.
echo "You must source this script: \$ source $0" >&2
exit 33
fi
elif [ -n "${ZSH_VERSION:+x}" ] ; then
SCRIPT_PATH="${(%):-%x}"
elif [ -n "${KSH_VERSION:+x}" ] ; then
SCRIPT_PATH="${.sh.file}"
fi

deactivate () {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-virtualenv/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub(crate) fn create(
(true, "activate") => {
// Extremely verbose, but should cover all major POSIX shells,
// as well as platforms where `readlink` does not implement `-f`.
r#"'"$(dirname -- "$(CDPATH= cd -- "$(dirname -- ${BASH_SOURCE[0]:-${(%):-%x}})" && echo "$PWD")")"'"#
r#"'"$(dirname -- "$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" && echo "$PWD")")"'"#
}
(true, "activate.bat") => r"%~dp0..",
(true, "activate.fish") => {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ fn verify_pyvenv_cfg_relocatable() {

let activate_sh = scripts.child("activate");
activate_sh.assert(predicates::path::is_file());
activate_sh.assert(predicates::str::contains(r#"VIRTUAL_ENV=''"$(dirname -- "$(CDPATH= cd -- "$(dirname -- ${BASH_SOURCE[0]:-${(%):-%x}})" && echo "$PWD")")"''"#));
activate_sh.assert(predicates::str::contains(r#"VIRTUAL_ENV=''"$(dirname -- "$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" && echo "$PWD")")"''"#));

let activate_bat = scripts.child("activate.bat");
activate_bat.assert(predicates::path::is_file());
Expand Down

0 comments on commit 50041c8

Please sign in to comment.