-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacinstall.command
211 lines (169 loc) · 8.01 KB
/
macinstall.command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# Function to handle errors gracefully and exit the script
function handle_error {
echo "ERROR: $1"
exit 1
}
# 1. Install Homebrew if not already installed
if ! command -v brew &> /dev/null; then
echo "Homebrew not found. Installing Homebrew..."
echo "You may be prompted for your password during the Homebrew installation."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || handle_error "Failed to install Homebrew."
# 2. Add Homebrew to the PATH
echo "Adding Homebrew to your PATH..."
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> "$HOME/.zprofile"
eval "$(/opt/homebrew/bin/brew shellenv)" || handle_error "Failed to add Homebrew to the PATH."
# 3. Reopen the terminal to refresh the environment
echo "Closing the current terminal and reopening a new one..."
osascript <<EOD
tell application "Terminal"
do script "if [ ! -f ~/.bash_profile ]; then
touch ~/.bash_profile
fi
if [ ! -f ~/.zshrc ]; then
touch ~/.zshrc
fi
source ~/.bash_profile
source ~/.zshrc && cd ~ && ~/Downloads/macinstall.command"
delay 1
close (every window whose name contains "bash")
end tell
EOD
exit 0
else
echo "Homebrew is already installed."
fi
# The script resumes from here when reopened
# 4. Install Python 3.11.9 using Homebrew
echo "Installing Python 3.11.9 via Homebrew..."
echo "You may be prompted for your password during the Python installation."
brew install [email protected] || handle_error "Failed to install Python 3.11."
# 5. Ensure Python 3.11 is the default version
echo "Setting Python 3.11 as the default..."
brew link --overwrite [email protected] || handle_error "Failed to link Python 3.11."
# 6. Update the PATH to ensure Python 3.11 is used globally and add aliases for both python3 and pip3
echo "Updating PATH to prioritize Python 3.11..."
# Function to update or create profile with aliases and Python path
write_to_profiles() {
local profile="$1"
if [ -f "$HOME/$profile" ]; then
echo "Updating $profile with Python path and aliases..."
else
echo "Creating $profile and adding Python path and aliases..."
touch "$HOME/$profile"
fi
# Add Python 3.11 path and aliases for python3 and pip3
echo "export PATH=\"/opt/homebrew/opt/[email protected]/bin:\$PATH\"" >> "$HOME/$profile"
echo "alias python3='/opt/homebrew/opt/[email protected]/bin/python3.11'" >> "$HOME/$profile"
echo "alias pip3='/opt/homebrew/opt/[email protected]/bin/pip3.11'" >> "$HOME/$profile"
}
# Add Python aliases and path to .bash_profile and .zshrc
write_to_profiles ".bash_profile"
write_to_profiles ".zshrc"
# Check if .zshrc exists, and create it if it doesn't
if [ ! -f "$HOME/.zshrc" ]; then
echo "Creating .zshrc..."
touch "$HOME/.zshrc"
fi
# Source both profiles to apply the changes
echo "Sourcing profiles to apply changes..."
if [ -f "$HOME/.bash_profile" ]; then
source "$HOME/.bash_profile"
fi
source "$HOME/.zshrc" || handle_error "Failed to source .zshrc"
# 7. Ask the user for the API key and set it as a global environment variable
read -p "Enter your OPENAI_API_KEY: " API_KEY
if [ -z "$API_KEY" ]; then
handle_error "No API key provided."
fi
# Write the API key to both .bash_profile and .zshrc
add_api_key_to_profiles() {
local profile="$1"
echo "export OPENAI_API_KEY=\"$API_KEY\"" >> "$HOME/$profile"
}
add_api_key_to_profiles ".bash_profile"
add_api_key_to_profiles ".zshrc"
# 8. Test Python 3.11 installation before proceeding
echo "Testing Python 3.11 installation..."
python3 -c "print('Python 3.11 installation successful!')" || handle_error "Failed to test Python 3.11 installation."
# 9. Download the GitHub repository to the target directory
echo "Downloading the GitHub repository..."
GITHUB_REPO_URL="https://github.com/cburst/efficientstudentmanagement/archive/refs/heads/main.zip"
TARGET_DIR="$HOME/efficientstudentmanagement-main"
ZIP_FILE="$HOME/efficientstudentmanagement.zip"
# Remove any existing zip file and force a fresh download without caching
rm -f "$ZIP_FILE" # Ensure old ZIP is removed
curl -H "Cache-Control: no-cache" -L "$GITHUB_REPO_URL" -o "$ZIP_FILE" || handle_error "Failed to download the GitHub repository."
# 10. Unzip the downloaded file and move it to the target directory
echo "Extracting the repository..."
unzip -o "$ZIP_FILE" -d "$HOME" || handle_error "Failed to unzip the repository."
rm "$ZIP_FILE"
# Verify the extraction and check for the requirements.txt file
if [ ! -f "$TARGET_DIR/folders/gpt-cli/requirements.txt" ]; then
handle_error "Failed to find the requirements.txt file after extraction. Please check if the repository was downloaded and unzipped correctly."
fi
# 11. Upgrade pip and install Python dependencies from requirements.txt after sourcing the profiles
echo "Upgrading pip and installing Python dependencies..."
pip3 install --upgrade pip || handle_error "Failed to upgrade pip."
REQUIREMENTS_FILE="$TARGET_DIR/folders/gpt-cli/requirements.txt"
pip3 install --no-deps -r "$REQUIREMENTS_FILE" || handle_error "Failed to install Python dependencies."
# 12. Ensure correct attrs version (23.2.0) is installed
echo "Installing the correct attrs version (23.2.0)..."
pip3 uninstall attrs -y || handle_error "Failed to uninstall conflicting attrs version."
pip3 install attrs==23.2.0 --no-deps || handle_error "Failed to install attrs==23.2.0."
# 13. Reinstall pydantic and pydantic_core with correct versions
echo "Reinstalling pydantic and pydantic_core with correct versions..."
pip3 install --force-reinstall pydantic==2.0.3 pydantic-core==2.3.0 || handle_error "Failed to reinstall pydantic or pydantic_core."
# 14. Upgrade OpenSSL and link it to Python
echo "Upgrading OpenSSL to resolve SSL issues..."
brew install openssl || handle_error "Failed to install OpenSSL."
echo "Linking OpenSSL to Python..."
export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
brew link openssl --force || handle_error "Failed to link OpenSSL."
# 15. Reinstall urllib3 and requests to use the correct OpenSSL version
echo "Reinstalling urllib3 and requests with proper OpenSSL support..."
pip3 install --upgrade urllib3 requests || handle_error "Failed to upgrade urllib3 and requests."
# 16. Create a terminal shortcut on the desktop to open in the target directory with environment variables loaded
echo "Creating a Terminal shortcut on the Desktop..."
SHORTCUT_FILE="$HOME/Desktop/Open_EfficientStudentManagement.command"
DEFAULT_SHELL=$(basename $SHELL)
cat <<EOL > "$SHORTCUT_FILE"
#!/bin/$DEFAULT_SHELL
# Source your shell configuration to load all environment variables
if [ -f "\$HOME/.bash_profile" ]; then
source "\$HOME/.bash_profile"
fi
if [ -f "\$HOME/.zshrc" ]; then
source "\$HOME/.zshrc"
fi
# Change directory to the project directory
cd "$TARGET_DIR"
# Keep the terminal open after execution
exec /bin/$DEFAULT_SHELL
EOL
# 17. Apply chmod +x to make the .command file executable
echo "Making the .command file executable..."
chmod +x "$SHORTCUT_FILE" || handle_error "Failed to make .command file executable."
# 18. Launch a new terminal to run secondary requirements and then test GPT-CLI in discrete steps
echo "Testing GPT-CLI in a new terminal session, running each command as a separate step..."
osascript <<EOD
tell application "Terminal"
do script "
source ~/.zshrc" -- First, source the profiles
delay 2
do script "
cd $TARGET_DIR/folders/gpt-cli" in front window -- Then, change to the GPT-CLI directory
delay 2
do script "
pip3 install --no-deps -r secondary_requirements.txt" in front window -- Install secondary requirements
delay 2
do script "
cp gpt.yml ~/.config/gpt-cli/gpt.yml" in front window -- Copy the gpt.yml to the appropriate location
delay 2
do script "
python3 gpt.py" in front window -- Finally, run the GPT-CLI test
end tell
EOD
echo "Setup complete! Please check the new terminal for GPT-CLI test results."