|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Enhanced Python virtual environment setup script with global zap-cli and global package installation |
4 | | -set -e |
| 3 | +# Python virtual environment setup script with zap-cli global symlink |
5 | 4 |
|
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 |
| 5 | +set -euo pipefail |
11 | 6 |
|
12 | | -# Automatically detect the script's root directory dynamically |
13 | | -SCRIPT_DIR="$(dirname "$(realpath "$0")")" |
14 | | -ROOT_DIR="$HOME" # Set root directory to user's home directory |
15 | | - |
16 | | -# Define the virtual environment directory and requirements file path |
| 7 | +ROOT_DIR="$HOME" |
17 | 8 | VENV_DIR="${ROOT_DIR}/venv" |
18 | 9 | REQUIREMENTS_FILE="${ROOT_DIR}/requirements.txt" |
19 | 10 | ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py" |
20 | 11 |
|
21 | | -# Check if Python3 is installed |
22 | | -if ! command -v python3 &> /dev/null; then |
23 | | - echo -e "${RED}Error: Python3 is not installed. Please install Python3 and try again.${NC}" |
| 12 | +error_exit() { |
| 13 | + echo "Error: $1" |
24 | 14 | exit 1 |
25 | | -fi |
| 15 | +} |
| 16 | + |
| 17 | +info() { |
| 18 | + echo "[INFO] $1" |
| 19 | +} |
26 | 20 |
|
27 | | -# Check if the python3-venv package is installed, and install it if missing |
28 | | -if ! dpkg -s python3-venv &> /dev/null; then |
29 | | - echo -e "${YELLOW}Installing python3-venv...${NC}" |
| 21 | +command -v python3 >/dev/null || error_exit "Python3 is not installed." |
| 22 | + |
| 23 | +if command -v dpkg &> /dev/null && ! dpkg -s python3-venv &> /dev/null; then |
| 24 | + info "Installing python3-venv..." |
30 | 25 | sudo apt update && sudo apt install -y python3-venv |
31 | 26 | fi |
32 | 27 |
|
33 | | -# If the requirements file is not found, search for it in the parent directory |
34 | 28 | if [ ! -f "$REQUIREMENTS_FILE" ]; then |
35 | | - echo -e "${YELLOW}Requirements file not found in $ROOT_DIR. Searching in subdirectories...${NC}" |
| 29 | + info "requirements.txt not found. Searching..." |
36 | 30 | REQUIREMENTS_FILE=$(find "$ROOT_DIR" -name "requirements.txt" -print -quit) |
37 | | - |
38 | | - if [ -z "$REQUIREMENTS_FILE" ]; then |
39 | | - echo -e "${RED}Error: requirements.txt not found.${NC}" |
40 | | - exit 1 |
41 | | - else |
42 | | - echo -e "${YELLOW}Found requirements.txt at $REQUIREMENTS_FILE.${NC}" |
43 | | - fi |
| 31 | + [ -z "$REQUIREMENTS_FILE" ] && error_exit "requirements.txt not found." |
| 32 | + info "Found requirements.txt at $REQUIREMENTS_FILE." |
44 | 33 | fi |
45 | 34 |
|
46 | | -# Create or recreate the virtual environment |
47 | 35 | if [ ! -d "$VENV_DIR" ]; then |
48 | | - echo -e "${YELLOW}Creating virtual environment at $VENV_DIR...${NC}" |
| 36 | + info "Creating virtual environment at $VENV_DIR..." |
49 | 37 | python3 -m venv "$VENV_DIR" |
50 | 38 | else |
51 | | - echo -e "${GREEN}Virtual environment already exists at $VENV_DIR.${NC}" |
| 39 | + info "Virtual environment already exists at $VENV_DIR." |
52 | 40 | fi |
53 | 41 |
|
54 | | -# Activate the virtual environment |
55 | | -echo -e "${YELLOW}Activating the virtual environment...${NC}" |
56 | 42 | source "$VENV_DIR/bin/activate" |
57 | 43 |
|
58 | | -# Upgrade pip and install dependencies from requirements.txt |
59 | | -echo -e "${YELLOW}Upgrading pip and installing packages in the virtual environment...${NC}" |
| 44 | +info "Upgrading pip and installing packages..." |
60 | 45 | pip install --upgrade pip |
61 | 46 | pip install -r "$REQUIREMENTS_FILE" |
62 | 47 |
|
63 | | -# Create a symlink for the zap-cli command globally |
| 48 | +# Create symlink |
64 | 49 | if [ -f "$ZAP_CLI_PATH" ]; then |
65 | | - echo -e "${YELLOW}Creating global symlink for zap-cli...${NC}" |
66 | | - sudo ln -sf "$ZAP_CLI_PATH" /usr/local/bin/zap-cli |
| 50 | + if [ ! -L /usr/local/bin/zap-cli ]; then |
| 51 | + sudo ln -s "$ZAP_CLI_PATH" /usr/local/bin/zap-cli |
| 52 | + echo "[INFO] zap-cli command created." |
| 53 | + else |
| 54 | + echo "[INFO] zap-cli command already exists." |
| 55 | + fi |
67 | 56 | else |
68 | | - echo -e "${RED}Error: zap-cli.py not found at $ZAP_CLI_PATH.${NC}" |
69 | 57 | deactivate |
70 | | - exit 1 |
| 58 | + error_exit "zap_cli.py not found at $ZAP_CLI_PATH." |
71 | 59 | fi |
72 | 60 |
|
73 | | -# Install pip packages globally (if any additional packages need to be globally installed) |
74 | | -echo -e "${YELLOW}Installing global pip packages...${NC}" |
75 | | -sudo pip install -r "$REQUIREMENTS_FILE" # Install globally |
76 | | - |
77 | | -# Final messages |
78 | | -echo -e "${GREEN}Setup complete. Virtual environment is ready to use.${NC}" |
79 | | -echo -e "${GREEN}To activate the virtual environment manually, run:${NC}" |
80 | | -echo -e "${GREEN}source ${VENV_DIR}/bin/activate${NC}" |
81 | | - |
82 | | -echo -e "${GREEN}You can now run the CLI with:${NC}" |
83 | | -echo -e "${GREEN}zap-cli --help${NC}" |
| 61 | +info "Setup complete." |
| 62 | +info "To activate the virtual environment manually, run:" |
| 63 | +echo "source ${VENV_DIR}/bin/activate" |
| 64 | +info "You can now run:" |
| 65 | +echo "zap-cli --help" |
84 | 66 |
|
85 | | -# Deactivate the virtual environment |
86 | 67 | deactivate |
0 commit comments