Skip to content

Commit c88c2dd

Browse files
Update setup.sh (#16)
2 parents 7900349 + ec5aa17 commit c88c2dd

1 file changed

Lines changed: 41 additions & 48 deletions

File tree

scripting_tools/setup.sh

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# Enhanced Python virtual environment setup script
3+
# Enhanced Python virtual environment setup script with Zsh installation and setup
44
set -e
55

66
# Colors for output
@@ -13,9 +13,10 @@ NC='\033[0m' # No Color
1313
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
1414
ROOT_DIR="$(realpath "$SCRIPT_DIR/..")" # Parent directory of the script
1515

16-
# Define the virtual environment directory and requirements file path, based on the current directory
16+
# Define the virtual environment directory and requirements file path
1717
VENV_DIR="${ROOT_DIR}/venv"
1818
REQUIREMENTS_FILE="${ROOT_DIR}/requirements.txt"
19+
ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py"
1920

2021
# Check if Python3 is installed
2122
if ! command -v python3 &> /dev/null; then
@@ -26,30 +27,16 @@ fi
2627
# Check if the python3-venv package is installed, and install it if missing
2728
if ! dpkg -s python3-venv &> /dev/null; then
2829
echo -e "${YELLOW}Installing python3-venv...${NC}"
29-
sudo apt update
30-
sudo apt install -y python3-venv
30+
sudo apt update && sudo apt install -y python3-venv
3131
fi
3232

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
33+
# If the requirements file is not found, search for it in the parent directory
4734
if [ ! -f "$REQUIREMENTS_FILE" ]; then
4835
echo -e "${YELLOW}Requirements file not found in $ROOT_DIR. Searching in subdirectories...${NC}"
49-
REQUIREMENTS_FILE=$(find_requirements_file "$ROOT_DIR")
50-
36+
REQUIREMENTS_FILE=$(find "$ROOT_DIR" -name "requirements.txt" -print -quit)
37+
5138
if [ -z "$REQUIREMENTS_FILE" ]; then
52-
echo -e "${RED}Error: requirements.txt not found in the directory structure.${NC}"
39+
echo -e "${RED}Error: requirements.txt not found.${NC}"
5340
exit 1
5441
else
5542
echo -e "${YELLOW}Found requirements.txt at $REQUIREMENTS_FILE.${NC}"
@@ -58,59 +45,65 @@ fi
5845

5946
# Create or recreate the virtual environment
6047
if [ ! -d "$VENV_DIR" ]; then
61-
echo -e "${YELLOW}Virtual environment not found. Creating it at $VENV_DIR...${NC}"
48+
echo -e "${YELLOW}Creating virtual environment at $VENV_DIR...${NC}"
6249
python3 -m venv "$VENV_DIR"
6350
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
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
51+
echo -e "${GREEN}Virtual environment already exists at $VENV_DIR.${NC}"
7752
fi
7853

7954
# Activate the virtual environment
8055
echo -e "${YELLOW}Activating the virtual environment...${NC}"
8156
source "$VENV_DIR/bin/activate"
8257

83-
# Upgrade pip to the latest version
84-
echo -e "${YELLOW}Upgrading pip to the latest version...${NC}"
58+
# Upgrade pip and install dependencies from requirements.txt
59+
echo -e "${YELLOW}Upgrading pip and installing packages...${NC}"
8560
pip install --upgrade pip
86-
87-
# Install the packages from requirements.txt if it exists
88-
echo -e "${YELLOW}Installing packages from $REQUIREMENTS_FILE...${NC}"
8961
pip install -r "$REQUIREMENTS_FILE"
9062

9163
# Create a symlink for the zap-cli command globally
92-
ZAP_CLI_PATH="${ROOT_DIR}/zap_cli.py"
93-
94-
# Check if zap-cli.py exists before creating the symlink
9564
if [ -f "$ZAP_CLI_PATH" ]; then
9665
echo -e "${YELLOW}Creating global symlink for zap-cli...${NC}"
9766
sudo ln -sf "$ZAP_CLI_PATH" /usr/local/bin/zap-cli
9867
else
99-
echo -e "${RED}Error: zap-cli.py not found in $ROOT_DIR/${NC}"
68+
echo -e "${RED}Error: zap-cli.py not found at $ZAP_CLI_PATH.${NC}"
10069
deactivate
10170
exit 1
10271
fi
10372

104-
# Notify the user that the setup process is complete
105-
echo -e "${GREEN}Setup complete. Virtual environment is ready to use.${NC}"
73+
# Install Zsh if it's not installed
74+
if ! command -v zsh &> /dev/null; then
75+
echo -e "${YELLOW}Installing Zsh...${NC}"
76+
sudo apt install -y zsh
77+
fi
78+
79+
# Change the default shell to Zsh
80+
echo -e "${YELLOW}Setting Zsh as the default shell...${NC}"
81+
chsh -s "$(which zsh)"
82+
83+
# Ensure .zshrc exists and add zap-cli alias if not already present
84+
if [ ! -f "~/.zshrc" ]; then
85+
echo -e "${YELLOW}Creating default .zshrc configuration file...${NC}"
86+
touch ~/.zshrc
87+
fi
10688

107-
# Dynamically display the path to activate the virtual environment
89+
if ! grep -q "alias zap-cli" ~/.zshrc; then
90+
echo -e "${YELLOW}Adding alias for zap-cli in .zshrc...${NC}"
91+
echo "alias zap-cli='python $ZAP_CLI_PATH'" >> ~/.zshrc
92+
else
93+
echo -e "${GREEN}Alias for zap-cli already exists in .zshrc.${NC}"
94+
fi
95+
96+
# Final messages
97+
echo -e "${GREEN}Setup complete. Virtual environment is ready to use.${NC}"
10898
echo -e "${GREEN}To activate the virtual environment manually, run:${NC}"
10999
echo -e "${GREEN}source ${VENV_DIR}/bin/activate${NC}"
110100

111-
# Instruction to run the main zap-cli.py script
112101
echo -e "${GREEN}You can now run the CLI with:${NC}"
113102
echo -e "${GREEN}zap-cli --help${NC}"
114103

115104
# Deactivate the virtual environment
116105
deactivate
106+
107+
# Instructions for applying changes
108+
echo -e "${YELLOW}To apply changes, restart your terminal or run:${NC}"
109+
echo -e "${GREEN}source ~/.zshrc${NC}"

0 commit comments

Comments
 (0)