From fb28e0024e30782a412702e9cb982799e413930b Mon Sep 17 00:00:00 2001 From: logicmoo Date: Wed, 28 Aug 2024 04:51:05 -0700 Subject: [PATCH] only gives output when needing to create venv --- scripts/ensure_venv | 87 +++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 27 deletions(-) diff --git a/scripts/ensure_venv b/scripts/ensure_venv index abc5773d1ab..d33fd279305 100755 --- a/scripts/ensure_venv +++ b/scripts/ensure_venv @@ -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 @@ -32,11 +60,11 @@ 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 @@ -44,29 +72,34 @@ create_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..."