Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions scripts/dev_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -euo pipefail

VENV_DIR=".venv"

# Pick a Python command that exists
if command -v python3 >/dev/null 2>&1; then
PY=python3
elif command -v python >/dev/null 2>&1; then
PY=python
elif command -v py >/dev/null 2>&1; then
PY="py -3"
else
echo "❌ Python not found. Install Python 3 and try again."
exit 1
fi

# Create venv only if missing
if [ ! -d "$VENV_DIR" ]; then
eval $PY -m venv "$VENV_DIR"
fi
Comment on lines +7 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using eval can be a security risk if the variable contains untrusted input. While it's safe in this specific context as you control the inputs, it's a best practice to avoid it. You can use a bash array to handle commands with arguments, which is safer and more robust.

This also simplifies the command execution when creating the virtual environment.

Suggested change
if command -v python3 >/dev/null 2>&1; then
PY=python3
elif command -v python >/dev/null 2>&1; then
PY=python
elif command -v py >/dev/null 2>&1; then
PY="py -3"
else
echo "❌ Python not found. Install Python 3 and try again."
exit 1
fi
# Create venv only if missing
if [ ! -d "$VENV_DIR" ]; then
eval $PY -m venv "$VENV_DIR"
fi
if command -v python3 >/dev/null 2>&1; then
PY_CMD=(python3)
elif command -v python >/dev/null 2>&1; then
PY_CMD=(python)
elif command -v py >/dev/null 2>&1; then
PY_CMD=(py -3)
else
echo "❌ Python not found. Install Python 3 and try again."
exit 1
fi
# Create venv only if missing
if [ ! -d "$VENV_DIR" ]; then
"${PY_CMD[@]}" -m venv "$VENV_DIR"
fi


# Activate (Linux/Mac = bin; Windows = Scripts)
if [ -f "$VENV_DIR/bin/activate" ]; then
# shellcheck source=/dev/null
source "$VENV_DIR/bin/activate"
elif [ -f "$VENV_DIR/Scripts/activate" ]; then
# shellcheck source=/dev/null
source "$VENV_DIR/Scripts/activate"
else
echo "❌ Could not find activate script in $VENV_DIR"
exit 1
fi

# Upgrade pip
python -m pip install --upgrade pip

# Install deps if present
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
echo "ℹ️ No requirements.txt — skipping"
fi

echo "✅ Environment Ready"
Comment on lines +24 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The source command within the script activates the virtual environment only for the duration of the script's execution. When the script finishes, the user's parent shell will not have the virtual environment activated, which might be unexpected. To improve usability, it's helpful to inform the user how to activate it manually in their shell.

This can be done by storing the path to the activation script and printing a helpful message at the end.

# Activate (Linux/Mac = bin; Windows = Scripts)
ACTIVATE_SCRIPT=""
if [ -f "$VENV_DIR/bin/activate" ]; then
  ACTIVATE_SCRIPT="$VENV_DIR/bin/activate"
elif [ -f "$VENV_DIR/Scripts/activate" ]; then
  ACTIVATE_SCRIPT="$VENV_DIR/Scripts/activate"
fi

if [ -z "$ACTIVATE_SCRIPT" ]; then
  echo "❌ Could not find activate script in $VENV_DIR"
  exit 1
fi

# shellcheck source=/dev/null
source "$ACTIVATE_SCRIPT"

# Upgrade pip
python -m pip install --upgrade pip

# Install deps if present
if [ -f "requirements.txt" ]; then
  pip install -r requirements.txt
else
  echo "ℹ️ No requirements.txt — skipping"
fi

echo "✅ Environment Ready"
echo "To use it, run:"
echo "source $ACTIVATE_SCRIPT"

Loading