|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Enhanced Python virtual environment setup script |
| 3 | +# Python virtual environment setup script for zap-cli |
4 | 4 | set -e |
5 | 5 |
|
6 | | -# Colors for output |
| 6 | +# Colors |
7 | 7 | GREEN='\033[0;32m' |
8 | | -RED='\033[0;31m' |
9 | 8 | YELLOW='\033[1;33m' |
10 | | -NC='\033[0m' # No Color |
| 9 | +RED='\033[0;31m' |
| 10 | +NC='\033[0m' |
11 | 11 |
|
12 | | -# Automatically detect the script's root directory dynamically |
| 12 | +# Directories |
13 | 13 | SCRIPT_DIR="$(dirname "$(realpath "$0")")" |
14 | | -ROOT_DIR="$(realpath "$SCRIPT_DIR/..")" # Parent directory of the script |
15 | | - |
16 | | -# Define the virtual environment directory and requirements file path, based on the current directory |
| 14 | +ROOT_DIR="$(realpath "$SCRIPT_DIR/..")" |
17 | 15 | VENV_DIR="${ROOT_DIR}/venv" |
18 | | -REQUIREMENTS_FILE="${ROOT_DIR}/requirements.txt" |
19 | 16 |
|
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}" |
23 | | - exit 1 |
24 | | -fi |
25 | | - |
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 |
47 | | -if [ ! -f "$REQUIREMENTS_FILE" ]; then |
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 |
57 | | -fi |
58 | | - |
59 | | -# Create or recreate the virtual environment |
60 | | -if [ ! -d "$VENV_DIR" ]; then |
61 | | - echo -e "${YELLOW}Virtual environment not found. Creating it at $VENV_DIR...${NC}" |
62 | | - python3 -m venv "$VENV_DIR" |
63 | | -else |
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 |
| 17 | +echo -e "${YELLOW}Creating virtual environment at ${VENV_DIR}...${NC}" |
| 18 | +python3 -m venv "$VENV_DIR" |
72 | 19 |
|
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 |
77 | | -fi |
78 | | - |
79 | | -# Activate the virtual environment |
80 | | -echo -e "${YELLOW}Activating the virtual environment...${NC}" |
| 20 | +# Activate venv |
81 | 21 | source "$VENV_DIR/bin/activate" |
82 | 22 |
|
83 | | -# Upgrade pip to the latest version |
84 | | -echo -e "${YELLOW}Upgrading pip to the latest version...${NC}" |
| 23 | +# Upgrade pip |
| 24 | +echo -e "${YELLOW}Upgrading pip...${NC}" |
85 | 25 | pip install --upgrade pip |
86 | 26 |
|
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" |
90 | | - |
91 | | -# Check if setup.py exists and install the package |
92 | | -SETUP_PY_PATH="${ROOT_DIR}/setup.py" |
93 | | -if [ -f "$SETUP_PY_PATH" ]; then |
94 | | - echo -e "${YELLOW}Found setup.py. Installing the package...${NC}" |
| 27 | +# Install dependencies |
| 28 | +if [ -f "${ROOT_DIR}/setup.py" ]; then |
| 29 | + echo -e "${YELLOW}Installing zap-cli in editable mode...${NC}" |
95 | 30 | pip install -e "$ROOT_DIR" |
| 31 | +elif [ -f "${ROOT_DIR}/requirements.txt" ]; then |
| 32 | + echo -e "${YELLOW}Installing dependencies from requirements.txt...${NC}" |
| 33 | + pip install -r "${ROOT_DIR}/requirements.txt" |
96 | 34 | else |
97 | | - echo -e "${YELLOW}No setup.py found. Skipping installation from setup.py.${NC}" |
98 | | -fi |
99 | | - |
100 | | -# Create a symlink for the zap-cli command globally |
101 | | -ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py" |
102 | | - |
103 | | -# Check if zap-cli.py exists before creating the symlink |
104 | | -if [ -f "$ZAP_CLI_PATH" ]; then |
105 | | - echo -e "${YELLOW}Creating global symlink for zap-cli...${NC}" |
106 | | - sudo ln -sf "$ZAP_CLI_PATH" /usr/local/bin/zap-cli |
107 | | -else |
108 | | - echo -e "${RED}Error: zap-cli.py not found in $ROOT_DIR/${NC}" |
| 35 | + echo -e "${RED}No setup.py or requirements.txt found. Exiting...${NC}" |
109 | 36 | deactivate |
110 | 37 | exit 1 |
111 | 38 | fi |
112 | 39 |
|
113 | | -# Notify the user that the setup process is complete |
114 | | -echo -e "${GREEN}Setup complete. Virtual environment is ready to use.${NC}" |
115 | | - |
116 | | -# Dynamically display the path to activate the virtual environment |
117 | | -echo -e "${GREEN}To activate the virtual environment manually, run:${NC}" |
118 | | -echo -e "${GREEN}source ${VENV_DIR}/bin/activate${NC}" |
| 40 | +# Deactivate |
| 41 | +deactivate |
119 | 42 |
|
120 | | -# Instruction to run the main zap-cli.py script |
121 | | -echo -e "${GREEN}You can now run the CLI with:${NC}" |
| 43 | +echo -e "${GREEN}✅ Setup complete.${NC}" |
| 44 | +echo -e "${GREEN}To use zap-cli globally, make sure ~/.local/bin is in your PATH.${NC}" |
| 45 | +echo -e "${GREEN}You can now run:${NC}" |
122 | 46 | echo -e "${GREEN}zap-cli --help${NC}" |
123 | | - |
124 | | -# Deactivate the virtual environment |
125 | | -deactivate |
0 commit comments