updated script

This commit is contained in:
ramzcaca 2026-02-05 10:56:10 +01:00
parent accbc6cbd8
commit 47be65f721
2 changed files with 85 additions and 46 deletions

View File

@ -2,3 +2,6 @@
simple script to avoid redundant python venv setup
the project is to continue to learn deeper bash scripting and try to make a script usable for everyone via package managers like homebrew, etc...
to run the script, use this command:
'curl -fsSL https://raw.githubusercontent.com/TON_USER/TON_REPO/main/venvsetup.sh | bash -s'

128
venvsetup.sh Normal file → Executable file
View File

@ -1,73 +1,109 @@
#!/bin/bash
existing_venv=$(find . -maxdepth 2 -name "pyvenv.cfg")
existing_req=$(find . -name "requirements.txt")
set -e
# IF EXISTING VENV HAVE SOMETHING, USE THIS PATH FOR WORK
venv_path=${existing_venv::-11}
echo "Python venv tool"
echo "Make sure you are inside your workspace"
echo ""
# --- Detect existing venv (first match only)
existing_venv=$(find . -maxdepth 2 -name pyvenv.cfg 2>/dev/null | head -n 1)
existing_req=$(find . -name requirements.txt 2>/dev/null | head -n 1)
if [ -n "$existing_venv" ]; then
venv_path="${existing_venv%/pyvenv.cfg}"
else
venv_path=""
fi
# --- Create venv if none found
create_venv() {
if [ -z "$existing_venv" ]; then
echo "No venv found, creating one..."
if [ -z "$venv_path" ]; then
echo "No virtual environment found."
read -p "Type the name of your venv: " name
echo "Creating $name virtual environement..."
python3 -m venv $name
echo "Creating virtual environment: $name"
python3 -m venv "$name"
venv_path="./$name"
echo ""
fi
}
start_venv(){
if [ -z "$VIRTUAL_ENV" ]; then
echo "Enabling the venv..."
source $venv_path/bin/activate
echo ""
fi
}
# --- Add venv to .gitignore
gitignore() {
echo "Do you want the to add the venv in a .gitignore? (recomended)"
if [ -f .gitignore ]; then
if grep -qx "$venv_path" .gitignore; then
return
fi
fi
echo "Do you want to add the venv to .gitignore? (recommended)"
read -p " y / n " choice
if [ "$choice" = "y" ]; then
echo "Adding the venv in a gitignore..."
echo "$venv_path >> .gitignore"
echo "Done!"
elif [ "$choice" = "n" ];then
echo "Got it!"
else
echo "No choices detected!"
if [ "$choice" = "y" ]; then
echo "$venv_path" >> .gitignore
echo "Added $venv_path to .gitignore"
fi
}
requirements(){
# --- Activate venv
activate_venv() {
if [ -f "$venv_path/bin/activate" ]; then
echo "Enabling the venv..."
# shellcheck disable=SC1090
source "$venv_path/bin/activate"
echo ""
else
echo "ERROR: activate script not found in $venv_path"
exit 1
fi
}
# --- Install requirements
requirements() {
if [ -n "$existing_req" ]; then
echo "Packages required detected (requirements.txt), do you want to install them? "
echo "requirements.txt detected. Install dependencies?"
read -p " y / n " choice
if [ "$choice" = "y" ]; then
pip install -r requirements.txt
echo "Done!"
elif [ "$choice" = "n" ];then
echo "Got it!"
else
echo "No choices detected!"
if [ "$choice" = "y" ]; then
pip install -r "$existing_req"
echo "Dependencies installed"
fi
fi
}
echo "Python venv tool"
echo "Make sure you are inside your workspace, this script will work if it finds the right folder"
echo ""
# --- Install packages interactively
installer() {
read -p "Type the package you need to install: " package
pip install "$package"
}
alfred() {
read -p "Do you want to install a package? (y / n) " choice
if [ "$choice" != "y" ]; then
return
fi
installer
while true; do
read -p "Do you want to install another package? (y / n) " choice
if [ "$choice" = "y" ]; then
installer
else
break
fi
done
}
# ------------------ MAIN ------------------
create_venv
start_venv
gitignore
pip install --upgrade pip
activate_venv
requirements
alfred
echo "Your venv is setup and launched!"
echo ""
echo "Your venv is ready!"
echo "To activate it later, run:"
echo "source \"$venv_path/bin/activate\""