forked from TheR1D/shell_gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·228 lines (187 loc) · 8.08 KB
/
install.py
File metadata and controls
executable file
·228 lines (187 loc) · 8.08 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
import os
import platform
import subprocess
import sys
import shutil
import argparse
from pathlib import Path
def parse_arguments():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description="ShellGPT Installation Script",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--skip-checks",
action="store_true",
help="Skip prerequisite checks and proceed directly to installation"
)
parser.add_argument(
"--verbose",
action="store_true",
help="Show detailed output during installation"
)
parser.add_argument(
"--no-venv-in-project",
action="store_true",
help="Don't configure Poetry to create virtualenvs in the project directory"
)
parser.add_argument(
"--global",
action="store_true",
dest="global_install",
help="Install ShellGPT globally (makes 'sgpt' available as a direct command)"
)
return parser.parse_args()
def check_python_version():
"""Verify Python version meets requirements (>=3.10)."""
min_version = (3, 10)
current_version = sys.version_info[:2]
if current_version < min_version:
print(f"Error: Python {min_version[0]}.{min_version[1]} or higher is required.")
print(f"Current Python version is {current_version[0]}.{current_version[1]}")
sys.exit(1)
print(f"✓ Python version {current_version[0]}.{current_version[1]} meets requirements.")
def check_poetry_installation():
"""Check if Poetry is installed, install if not."""
if shutil.which("poetry") is None:
print("Poetry not found. Installing Poetry...")
# Poetry installation differs by platform
if platform.system() == "Windows":
# Windows installation
try:
subprocess.run(
["powershell", "-Command",
"(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -"],
check=True
)
# Add to PATH for this session
poetry_path = str(Path.home() / "AppData" / "Roaming" / "Python" / "Scripts")
os.environ["PATH"] += os.pathsep + poetry_path
except subprocess.CalledProcessError:
print("Error: Failed to install Poetry on Windows.")
sys.exit(1)
else:
# Unix-based installation (macOS, Linux)
try:
# Using subprocess.run with shell=True for pipe operation
subprocess.run(
"curl -sSL https://install.python-poetry.org | python3 -",
check=True, shell=True
)
# Add to PATH for this session
poetry_path = str(Path.home() / ".local" / "bin")
os.environ["PATH"] += os.pathsep + poetry_path
except subprocess.CalledProcessError:
print("Error: Failed to install Poetry.")
sys.exit(1)
print("✓ Poetry has been installed.")
else:
print("✓ Poetry is already installed.")
def check_system_dependencies():
"""Check for necessary system dependencies based on platform."""
system = platform.system()
if system == "Linux":
# Check for common build dependencies on Linux
dependencies = ["gcc", "python3-dev"]
missing_deps = []
for dep in dependencies:
print(f"Checking for {dep}...")
if subprocess.run(["which", dep], stdout=subprocess.PIPE, stderr=subprocess.PIPE).returncode != 0:
missing_deps.append(dep)
if missing_deps:
print("Warning: The following dependencies were not found:")
for dep in missing_deps:
print(f" - {dep}")
print("These might be needed for some dependencies.")
print("Consider installing them with your package manager.")
elif system == "Darwin": # macOS
# Check for Xcode command line tools
if subprocess.run(["xcode-select", "-p"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).returncode != 0:
print("Warning: Xcode Command Line Tools not found.")
print("Consider installing them with: xcode-select --install")
# Add Windows-specific checks if needed
elif system == "Windows":
# Check for Visual C++ Build Tools
print("Note: On Windows, you might need Microsoft Visual C++ Build Tools")
print(" if you encounter issues with native extensions.")
print(" You can download them from: https://visualstudio.microsoft.com/visual-cpp-build-tools/")
def install_shellgpt(verbose=False):
"""Install ShellGPT using Poetry."""
print("\nInstalling ShellGPT with Poetry...")
# Run poetry install in the current directory
cmd = ["poetry", "install"]
if verbose:
# Show verbose output
result = subprocess.run(cmd)
else:
# Capture output to keep it clean
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
print("Error: Poetry installation failed.")
if not verbose:
print("Try running with --verbose for more information.")
sys.exit(1)
# If we get here, installation succeeded
print("✓ ShellGPT has been successfully installed!")
def setup_virtual_environment():
"""Configure the virtual environment if needed."""
# Ensure poetry is creating virtualenvs in the project directory
subprocess.run(["poetry", "config", "virtualenvs.in-project", "true"], check=True)
print("✓ Poetry configured to create virtual environments in the project directory.")
def display_usage_instructions():
"""Show instructions on how to use the installed package."""
print("\n=== How to Use ShellGPT ===")
print("Option 1: Run directly with Poetry:")
print(" poetry run sgpt \"Your prompt here\"")
print("\nOption 2: Activate the virtual environment and run:")
print(" poetry shell")
print(" sgpt \"Your prompt here\"")
print("\nOption 3: Install ShellGPT globally:")
print(" poetry build")
print(" pip install dist/*.whl")
def install_globally(verbose=False):
"""Install ShellGPT globally from the local source using pip install ."""
print("\nInstalling ShellGPT globally from local source with pip install . ...")
install_cmd = [
sys.executable, "-m", "pip", "install", ".", "--force-reinstall"
]
if verbose:
result = subprocess.run(install_cmd)
else:
result = subprocess.run(install_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
print("Error: Failed to install package globally from local source.")
if not verbose:
print("Try running with --verbose for more information.")
sys.exit(1)
print("✓ ShellGPT has been successfully installed globally from your local source!")
print(" You can now run 'sgpt' directly from anywhere.")
def main():
"""Main installation process."""
args = parse_arguments()
print("=== ShellGPT Installation Script ===\n")
if not args.skip_checks:
# Step 1: Check Python version
check_python_version()
# Step 2: Check and install Poetry if needed
check_poetry_installation()
# Step 3: Check system dependencies
check_system_dependencies()
else:
print("Skipping prerequisite checks as requested.")
# Step 4: Configure Poetry
if not args.no_venv_in_project:
setup_virtual_environment()
# Step 5: Install ShellGPT
install_shellgpt(verbose=args.verbose)
# Step 6: Install globally if requested
if args.global_install:
install_globally(verbose=args.verbose)
# Step 7: Show usage instructions
if not args.global_install:
display_usage_instructions()
print("\nInstallation complete!")
if __name__ == "__main__":
main()