|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -set -euo pipefail |
| 3 | +# Enhanced Python virtual environment setup script |
| 4 | +set -e |
4 | 5 |
|
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 |
7 | 11 |
|
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 |
16 | 15 |
|
17 | | -# Define the paths for the environment setup |
| 16 | +# Define the virtual environment directory and requirements file path, based on the current directory |
18 | 17 | VENV_DIR="${ROOT_DIR}/venv" |
19 | 18 | REQUIREMENTS_FILE="${ROOT_DIR}/requirements.txt" |
20 | | -ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py" # Path to zap_cli.py in the root directory |
21 | 19 |
|
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}" |
26 | 23 | exit 1 |
27 | | -else |
28 | | - echo "zap_cli.py found at $ZAP_CLI_PATH" |
29 | 24 | fi |
30 | 25 |
|
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 |
32 | 47 | 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 |
37 | 57 | fi |
38 | 58 |
|
39 | | -# Create virtual environment if it doesn't exist |
| 59 | +# Create or recreate the virtual environment |
40 | 60 | 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}" |
42 | 62 | python3 -m venv "$VENV_DIR" |
43 | 63 | 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 |
45 | 77 | fi |
46 | 78 |
|
47 | 79 | # Activate the virtual environment |
| 80 | +echo -e "${YELLOW}Activating the virtual environment...${NC}" |
48 | 81 | source "$VENV_DIR/bin/activate" |
49 | 82 |
|
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}" |
52 | 85 | 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 |
58 | 86 |
|
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" |
61 | 90 |
|
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" |
73 | 93 |
|
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 |
76 | 98 | 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 |
78 | 102 | fi |
79 | 103 |
|
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}" |
85 | 114 |
|
| 115 | +# Deactivate the virtual environment |
86 | 116 | deactivate |
0 commit comments