Skip to content

Commit aca9c53

Browse files
adding changes
1 parent e57c944 commit aca9c53

File tree

1 file changed

+85
-55
lines changed

1 file changed

+85
-55
lines changed

scripting_tools/setup.sh

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,116 @@
11
#!/bin/bash
22

3-
set -euo pipefail
3+
# Enhanced Python virtual environment setup script
4+
set -e
45

5-
# Ensure python3-venv is installed
6-
command -v python3 >/dev/null || { echo "Python3 is not installed."; exit 1; }
6+
# Colors for output
7+
GREEN='\033[0;32m'
8+
RED='\033[0;31m'
9+
YELLOW='\033[1;33m'
10+
NC='\033[0m' # No Color
711

8-
if ! dpkg -s python3-venv &>/dev/null; then
9-
echo "Installing python3-venv..."
10-
sudo apt update && sudo apt install -y python3-venv
11-
fi
12-
13-
# Get the script's directory dynamically, so we can resolve paths relative to the script location
14-
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
15-
ROOT_DIR="$SCRIPT_DIR/.." # The root directory is the parent of the 'scripting_tools' folder
12+
# Automatically detect the script's root directory dynamically
13+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
14+
ROOT_DIR="$(realpath "$SCRIPT_DIR/..")" # Parent directory of the script
1615

17-
# Define the paths for the environment setup
16+
# Define the virtual environment directory and requirements file path, based on the current directory
1817
VENV_DIR="${ROOT_DIR}/venv"
1918
REQUIREMENTS_FILE="${ROOT_DIR}/requirements.txt"
20-
ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py" # Path to zap_cli.py in the root directory
2119

22-
# Debugging: Check if zap_cli.py exists in the expected location
23-
echo "DEBUG: Checking if $ZAP_CLI_PATH exists..."
24-
if [ ! -f "$ZAP_CLI_PATH" ]; then
25-
echo "ERROR: zap_cli.py not found at $ZAP_CLI_PATH"
20+
# Check if Python3 is installed
21+
if ! command -v python3 &> /dev/null; then
22+
echo -e "${RED}Error: Python3 is not installed. Please install Python3 and try again.${NC}"
2623
exit 1
27-
else
28-
echo "zap_cli.py found at $ZAP_CLI_PATH"
2924
fi
3025

31-
# Find requirements.txt
26+
# Check if the python3-venv package is installed, and install it if missing
27+
if ! dpkg -s python3-venv &> /dev/null; then
28+
echo -e "${YELLOW}Installing python3-venv...${NC}"
29+
sudo apt update
30+
sudo apt install -y python3-venv
31+
fi
32+
33+
# Function to find the requirements file dynamically if it's not in the root folder
34+
find_requirements_file() {
35+
if [ -f "$1/requirements.txt" ]; then
36+
echo "$1/requirements.txt"
37+
elif [ -d "$1" ]; then
38+
for dir in "$1"/*; do
39+
if [ -d "$dir" ]; then
40+
find_requirements_file "$dir" # Recurse into subdirectories
41+
fi
42+
done
43+
fi
44+
}
45+
46+
# If the requirements file is not found in the current directory, try searching for it in the parent directory
3247
if [ ! -f "$REQUIREMENTS_FILE" ]; then
33-
echo "requirements.txt not found. Searching..."
34-
REQUIREMENTS_FILE=$(find "$ROOT_DIR" -name "requirements.txt" -print -quit)
35-
[ -z "$REQUIREMENTS_FILE" ] && { echo "requirements.txt not found."; exit 1; }
36-
echo "Found requirements.txt at $REQUIREMENTS_FILE."
48+
echo -e "${YELLOW}Requirements file not found in $ROOT_DIR. Searching in subdirectories...${NC}"
49+
REQUIREMENTS_FILE=$(find_requirements_file "$ROOT_DIR")
50+
51+
if [ -z "$REQUIREMENTS_FILE" ]; then
52+
echo -e "${RED}Error: requirements.txt not found in the directory structure.${NC}"
53+
exit 1
54+
else
55+
echo -e "${YELLOW}Found requirements.txt at $REQUIREMENTS_FILE.${NC}"
56+
fi
3757
fi
3858

39-
# Create virtual environment if it doesn't exist
59+
# Create or recreate the virtual environment
4060
if [ ! -d "$VENV_DIR" ]; then
41-
echo "Creating virtual environment at $VENV_DIR..."
61+
echo -e "${YELLOW}Virtual environment not found. Creating it at $VENV_DIR...${NC}"
4262
python3 -m venv "$VENV_DIR"
4363
else
44-
echo "Virtual environment already exists at $VENV_DIR."
64+
if [ ! -f "$VENV_DIR/bin/activate" ]; then
65+
echo -e "${RED}Error: Virtual environment exists but is corrupted. Recreating it...${NC}"
66+
rm -rf "$VENV_DIR"
67+
python3 -m venv "$VENV_DIR"
68+
else
69+
echo -e "${GREEN}Virtual environment already exists at $VENV_DIR.${NC}"
70+
fi
71+
fi
72+
73+
# Check if Python executable exists in the virtual environment
74+
if [ ! -f "$VENV_DIR/bin/python" ]; then
75+
echo -e "${RED}Error: Python executable missing in the virtual environment. Exiting...${NC}"
76+
exit 1
4577
fi
4678

4779
# Activate the virtual environment
80+
echo -e "${YELLOW}Activating the virtual environment...${NC}"
4881
source "$VENV_DIR/bin/activate"
4982

50-
# Install dependencies inside the virtual environment
51-
echo "Upgrading pip and installing requirements inside the virtual environment..."
83+
# Upgrade pip to the latest version
84+
echo -e "${YELLOW}Upgrading pip to the latest version...${NC}"
5285
pip install --upgrade pip
53-
pip install -r "$REQUIREMENTS_FILE"
54-
55-
# Ensure missing dependencies (like psutil) are installed
56-
echo "Installing missing dependencies like psutil..."
57-
pip install psutil
5886

59-
# Create a wrapper script for zap-cli command to always run inside the virtual environment
60-
WRAPPER_SCRIPT="/usr/local/bin/zap-cli"
87+
# Install the packages from requirements.txt if it exists
88+
echo -e "${YELLOW}Installing packages from $REQUIREMENTS_FILE...${NC}"
89+
pip install -r "$REQUIREMENTS_FILE"
6190

62-
if [ ! -f "$WRAPPER_SCRIPT" ]; then
63-
echo "Creating zap-cli wrapper script at $WRAPPER_SCRIPT..."
64-
cat << EOF | sudo tee "$WRAPPER_SCRIPT" > /dev/null
65-
#!/bin/bash
66-
# Ensure virtual environment is activated
67-
source $VENV_DIR/bin/activate
68-
# Run zap-cli within the virtual environment
69-
$VENV_DIR/bin/python $ZAP_CLI_PATH "\$@"
70-
# Deactivate the environment once done
71-
deactivate
72-
EOF
91+
# Create a symlink for the zap-cli command globally
92+
ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py"
7393

74-
sudo chmod +x "$WRAPPER_SCRIPT"
75-
echo "zap-cli wrapper script created successfully."
94+
# Check if zap-cli.py exists before creating the symlink
95+
if [ -f "$ZAP_CLI_PATH" ]; then
96+
echo -e "${YELLOW}Creating global symlink for zap-cli...${NC}"
97+
sudo ln -sf "$VENV_DIR/bin/python $ZAP_CLI_PATH" /usr/local/bin/zap-cli
7698
else
77-
echo "zap-cli wrapper script already exists."
99+
echo -e "${RED}Error: zap-cli.py not found in $ROOT_DIR/${NC}"
100+
deactivate
101+
exit 1
78102
fi
79103

80-
# Final message
81-
echo "Setup complete."
82-
echo "To activate the virtual environment manually, run:"
83-
echo "source ${VENV_DIR}/bin/activate"
84-
echo "You can now run zap-cli --help"
104+
# Notify the user that the setup process is complete
105+
echo -e "${GREEN}Setup complete. Virtual environment is ready to use.${NC}"
106+
107+
# Dynamically display the path to activate the virtual environment
108+
echo -e "${GREEN}To activate the virtual environment manually, run:${NC}"
109+
echo -e "${GREEN}source ${VENV_DIR}/bin/activate${NC}"
110+
111+
# Instruction to run the main zap-cli.py script
112+
echo -e "${GREEN}You can now run the CLI with:${NC}"
113+
echo -e "${GREEN}zap-cli --help${NC}"
85114

115+
# Deactivate the virtual environment
86116
deactivate

0 commit comments

Comments
 (0)