From b5a58ca7157b6fea10bd9fcfa9b919fb7ef8c15d Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Thu, 8 Aug 2024 16:32:02 -0700 Subject: [PATCH] op.sh: fix venv on zsh + bash 3.2 (#33238) * i love zsh * check old-commit-hash: 80653bfb591a3897f4bb3e59ed82e046812751cf --- tools/op.sh | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tools/op.sh b/tools/op.sh index 7dc2ac1377..7d972b0c86 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -14,6 +14,7 @@ UNDERLINE='\033[4m' BOLD='\033[1m' NC='\033[0m' +SHELL_NAME="$(basename ${SHELL})" RC_FILE="${HOME}/.$(basename ${SHELL})rc" if [ "$(uname)" == "Darwin" ] && [ $SHELL == "/bin/bash" ]; then RC_FILE="$HOME/.bash_profile" @@ -145,7 +146,7 @@ function op_check_python() { function op_check_venv() { echo "Checking for venv..." - if source $OPENPILOT_ROOT/.venv/bin/activate; then + if [[ -f $OPENPILOT_ROOT/.venv/bin/activate ]]; then echo -e " ↳ [${GREEN}✔${NC}] venv detected." else echo -e " ↳ [${RED}✗${NC}] Can't activate venv in $OPENPILOT_ROOT. Assuming global env!" @@ -223,12 +224,28 @@ function op_setup() { } function op_activate_venv() { + # bash 3.2 can't handle this without the 'set +e' + set +e source $OPENPILOT_ROOT/.venv/bin/activate &> /dev/null || true + set -e } function op_venv() { op_before_cmd - bash --rcfile <(echo "source $RC_FILE; source $OPENPILOT_ROOT/.venv/bin/activate") + + if [[ ! -f $OPENPILOT_ROOT/.venv/bin/activate ]]; then + echo -e "No venv found in $OPENPILOT_ROOT" + return 1 + fi + + case $SHELL_NAME in + "zsh") + ZSHRC_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmp_zsh') + echo "source $RC_FILE; source $OPENPILOT_ROOT/.venv/bin/activate" >> $ZSHRC_DIR/.zshrc + ZDOTDIR=$ZSHRC_DIR zsh ;; + *) + bash --rcfile <(echo "source $RC_FILE; source $OPENPILOT_ROOT/.venv/bin/activate") ;; + esac } function op_check() {