Skip to content

Commit

Permalink
inve can create non-default-named virtual env from arg
Browse files Browse the repository at this point in the history
  • Loading branch information
awdeorio committed Sep 11, 2023
1 parent eb5161a commit 436c0f1
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions bin/inve
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,47 @@
# Stop on errors
set -Eeuo pipefail

# Search for virtual environments in directories with these names
VIRTUAL_ENV_BASENAMES=".venv venv env"
# Virtual environment search path, basenames only, space separated
VENVPATH=".venv venv env"

if [ -n "${1-}" ]; then
# Virtual environment root specified as argument
VIRTUAL_ENV="$1"
else
# Save PWD
INVE_OLDPWD="`pwd`"

# Search up for the root of a virtual environment, trying each of the
# possible viretual env base names.
# Search from PWD to root, for any of the virtual env directory names
INVE_OLDPWD="$PWD"
while [ "${PWD}" != "/" ]; do
for VIRTUAL_ENV_BASENAME in ${VIRTUAL_ENV_BASENAMES}; do
VIRTUAL_ENV="${PWD}/${VIRTUAL_ENV_BASENAME}"
for VENV_BASENAME in ${VENVPATH}; do
VIRTUAL_ENV="${PWD}/${VENV_BASENAME}"
if [ -e "${VIRTUAL_ENV}/bin/activate" ]; then
break 2 # break out of nested loop
fi
done
cd ..
done
if [ "$PWD" = "/" ]; then
VIRTUAL_ENV_BASE=.venv
VIRTUAL_ENV="${INVE_OLDPWD}/${VIRTUAL_ENV_BASE}"
read -p "No virtual environment found. Would you like to create ${VIRTUAL_ENV_BASE} ? [yn]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "python3 -m venv ${VIRTUAL_ENV}"
python3 -m venv "${VIRTUAL_ENV}"
echo "pip install --upgrade --quiet pip setuptools wheel"
"${VIRTUAL_ENV}/bin/pip" install --upgrade --quiet pip setuptools wheel
else
echo "exit"
exit 1
fi
VIRTUAL_ENV="${INVE_OLDPWD}/.venv" # Default to ./.venv
fi

# Restore PWD
cd "${INVE_OLDPWD}"
cd $INVE_OLDPWD
fi

# Sanity checks
# Virtual environment not found: offer to create
if ! test -d "${VIRTUAL_ENV}"; then
echo "Error: directory does not exist: ${VIRTUAL_ENV}"
exit 1
BASENAME=$(basename "$VIRTUAL_ENV")
read -p "Virtual environment not found. Create ${BASENAME} ? [yn]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "python3 -m venv ${VIRTUAL_ENV}"
python3 -m venv "${VIRTUAL_ENV}"
echo "pip install --upgrade --quiet pip setuptools wheel"
"${VIRTUAL_ENV}/bin/pip" install --upgrade --quiet pip setuptools wheel
else
echo "exit"
exit 1
fi
fi

# Verify virtual environment exists
if ! test -d "${VIRTUAL_ENV}/bin"; then
echo "Error: directory does not exist: ${VIRTUAL_ENV}/bin"
exit 1
Expand Down

0 comments on commit 436c0f1

Please sign in to comment.