Skip to content

Commit

Permalink
only gives output when needing to create venv
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Aug 28, 2024
1 parent 1a2e0b9 commit fb28e00
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions scripts/ensure_venv
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
#!/bin/bash

# Name of the virtual environment directory
# Check if VENV_DIR is not set or is empty
if [ -z "$VENV_DIR" ]; then
# Then check if VIRTUAL_ENV is set and points to a valid directory
if [ -n "$VIRTUAL_ENV" ] && [ -d "$VIRTUAL_ENV" ]; then
# If VIRTUAL_ENV is valid, use it for VENV_DIR
VENV_DIR="$VIRTUAL_ENV"
else
# Otherwise, default to 'venv'
VENV_DIR="venv"
fi
# Ensure the script is being sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Warning: This script should be sourced, not executed directly." >&2
exit 1 # Exit the script if it's not being sourced
fi

# Get the directory of the script
SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
# Set VENV_DIR to one directory above the script's directory
PARENT_DIR=$(dirname "$SCRIPT_DIR")
VENV_DIR="$PARENT_DIR/venv"

# Default verbosity level (0: quiet, 1: normal, 2: verbose)
VERBOSITY=1

# Parse command-line options
for arg in "$@"; do
case $arg in
-v|--verbose)
VERBOSITY=2
;;
-q|--quiet)
VERBOSITY=0
;;
*)
#echo "Usage: source $0 [-v|--verbose] [-q|--quiet]" >&2
#exit 1
;;
esac
done

# echo "VERBOSITY=$VERBOSITY"

# Function to print messages based on verbosity level
log() {
local level="$1"
shift
if [ "$level" -le "$VERBOSITY" ]; then
echo "$@" >&2
fi
}

# Function to activate the virtual environment
activate_venv() {
echo "Activating the virtual environment: $(realpath $VENV_DIR)"
log 2 "Activating the virtual environment: $(realpath $VENV_DIR)"
source "$VENV_DIR/bin/activate"
}

# Function to check if we are inside a virtual environment
is_inside_venv() {
if [[ "$VIRTUAL_ENV" != "" ]]
then
if [[ "$VIRTUAL_ENV" != "" ]]; then
return 0 # True, script is running inside a virtual environment
else
return 1 # False, script is not running inside a virtual environment
Expand All @@ -32,41 +60,46 @@ is_inside_venv() {
# Function to create a virtual environment
create_venv() {
if [ ! -d "$VENV_DIR" ]; then
echo "Creating a virtual environment: $VENV_DIR"
log 1 "Creating a virtual environment: $VENV_DIR"
python3 -m venv "$VENV_DIR"
# Assuming the script is run from a virtual environment with useful packages
if [ -n "$VIRTUAL_ENV" ] && [ -d "$VIRTUAL_ENV" ]; then
echo "Inheriting packages from existing environment: $VIRTUAL_ENV"
log 2 "Inheriting packages from existing environment: $VIRTUAL_ENV"
source "$VIRTUAL_ENV/bin/activate"
pip freeze > /tmp/requirements.txt
deactivate
activate_venv
pip install -r /tmp/requirements.txt
rm /tmp/requirements.txt
fi
if [ -f "requirements.txt" ]; then
:
#echo "Found local requirements.txt, installing packages..."
#pip install -r requirements.txt
fi
if [ -f "requirements.txt" ]; then
:
#log 2 "Found local requirements.txt, installing packages..."
#pip install -r requirements.txt
fi
else
: # echo "Virtual environment already exists: $VENV_DIR"
log 2 "Virtual environment already exists: $VENV_DIR"
fi
}

# Main logic of the script
if is_inside_venv; then
: #echo "Script is running inside a virtual environment: $VIRTUAL_ENV"
if [ "$VIRTUAL_ENV" != "$(realpath $VENV_DIR)" ]; then
log 1 "Reusing virtual environment: $VIRTUAL_ENV"
#log 1 "Expected virtual environment: $(realpath $VENV_DIR)"
else
log 2 "Script is running inside the expected virtual environment: $VENV_DIR"
fi
else
# echo "Script is not running inside a virtual environment."
log 2 "Script is not running inside a virtual environment."
create_venv
activate_venv

# Relaunch the script inside the virtual environment
# echo "Relaunching the script inside the virtual environment..."
# exec "$0" "$@"
#log 2 "Relaunching the script inside the virtual environment..."
#exec "$0" "$@"
fi

# Place your script's main execution logic here
# echo "Executing the main script logic..."
#log 2 "Executing the main script logic..."

0 comments on commit fb28e00

Please sign in to comment.