-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
256 lines (225 loc) · 10 KB
/
setup.py
File metadata and controls
256 lines (225 loc) · 10 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
import sys
import venv
import subprocess
import platform
import getpass
def print_colored(text, color):
colors = {
'GREEN': '\033[92m',
'YELLOW': '\033[93m',
'RED': '\033[91m',
'NC': '\033[0m'
}
print(f"{colors[color]}{text}{colors['NC']}")
def run_command(command, message):
print_colored(f"{message}...", "GREEN")
try:
subprocess.run(command, check=True, shell=True, capture_output=not DEBUG)
return True
except subprocess.CalledProcessError as e:
print_colored(f"Error: {e}", "RED")
return False
def detect_django_settings():
"""Detect the correct Django settings module path"""
possible_paths = [
'Stack.settings',
'IntelStack.settings',
'config.settings',
'core.settings'
]
for path in possible_paths:
module_parts = path.split('.')
if os.path.exists(os.path.join(*module_parts) + '.py'):
return path
return None
def create_superuser(venv_python):
# First detect the correct settings module
settings_module = detect_django_settings()
if not settings_module:
print_colored("Could not detect Django settings module. Trying with manage.py...", "YELLOW")
# Try using manage.py directly as fallback
return create_superuser_with_manage_py(venv_python)
while True:
print_colored("Creating superuser for IntelStack admin...", "GREEN")
username = input("Username: ")
email = input("Email: ")
password = getpass.getpass("Password: ")
password2 = getpass.getpass("Confirm password: ")
if password != password2:
print_colored("Passwords do not match!", "RED")
retry = input("Do you want to try again? (y/n): ").lower()
if retry != 'y':
print_colored("Superuser creation skipped.", "YELLOW")
return False
continue
# Create superuser using manage.py directly
cmd = f'"{venv_python}" manage.py createsuperuser --noinput --username "{username}" --email "{email}"'
env = os.environ.copy()
env['DJANGO_SUPERUSER_PASSWORD'] = password
try:
result = subprocess.run(
cmd,
shell=True,
capture_output=True,
text=True,
env=env
)
if result.returncode == 0:
print_colored("Superuser created successfully!", "GREEN")
return True
else:
print_colored(f"Error: {result.stderr.strip()}", "RED")
if DEBUG:
print_colored("Debug information:", "YELLOW")
print_colored(f"Return code: {result.returncode}", "YELLOW")
print_colored(f"Command output: {result.stdout}", "YELLOW")
retry = input("Do you want to try again? (y/n): ").lower()
if retry != 'y':
return False
except Exception as e:
print_colored(f"Error creating superuser: {str(e)}", "RED")
if DEBUG:
import traceback
print_colored(traceback.format_exc(), "RED")
retry = input("Do you want to try again? (y/n): ").lower()
if retry != 'y':
return False
def create_superuser_with_manage_py(venv_python):
"""Fallback method using manage.py createsuperuser command"""
while True:
print_colored("Creating superuser (using manage.py)...", "GREEN")
username = input("Username: ")
email = input("Email: ")
password = getpass.getpass("Password: ")
password2 = getpass.getpass("Confirm password: ")
if password != password2:
print_colored("Passwords do not match!", "RED")
retry = input("Do you want to try again? (y/n): ").lower()
if retry != 'y':
return False
continue
cmd = f'"{venv_python}" manage.py createsuperuser --noinput'
env = os.environ.copy()
env['DJANGO_SUPERUSER_USERNAME'] = username
env['DJANGO_SUPERUSER_EMAIL'] = email
env['DJANGO_SUPERUSER_PASSWORD'] = password
try:
result = subprocess.run(
cmd,
shell=True,
capture_output=True,
text=True,
env=env
)
if result.returncode == 0:
print_colored("Superuser created successfully!", "GREEN")
return True
else:
error_msg = result.stderr.strip() or result.stdout.strip()
print_colored(f"Error: {error_msg}", "RED")
retry = input("Do you want to try again? (y/n): ").lower()
if retry != 'y':
return False
except Exception as e:
print_colored(f"Error: {str(e)}", "RED")
retry = input("Do you want to try again? (y/n): ").lower()
if retry != 'y':
return False
def get_python_command():
"""Detect the correct Python command"""
if IS_WINDOWS:
# Try 'python' first, then 'py' on Windows
try:
subprocess.run(['python', '--version'], check=True, capture_output=True)
return 'python'
except (subprocess.CalledProcessError, FileNotFoundError):
try:
subprocess.run(['py', '--version'], check=True, capture_output=True)
return 'py'
except (subprocess.CalledProcessError, FileNotFoundError):
print_colored("Python not found! Please install Python 3.8 or higher.", "RED")
sys.exit(1)
else:
# Try 'python3' first, then 'python' on Unix
try:
subprocess.run(['python3', '--version'], check=True, capture_output=True)
return 'python3'
except (subprocess.CalledProcessError, FileNotFoundError):
try:
subprocess.run(['python', '--version'], check=True, capture_output=True)
return 'python'
except (subprocess.CalledProcessError, FileNotFoundError):
print_colored("Python not found! Please install Python 3.8 or higher.", "RED")
sys.exit(1)
if __name__ == "__main__":
try:
DEBUG = "--debug" in sys.argv
IS_WINDOWS = platform.system() == "Windows"
python_cmd = get_python_command()
print_colored("Starting IntelStack installation...", "GREEN")
print_colored(f"Using Python command: {python_cmd}", "GREEN")
# Create directories
os.makedirs("storage/screenshots", exist_ok=True)
# Generate encryption key if not exists
key_path = os.path.join(os.getcwd(), "encryption.key")
if not os.path.exists(key_path):
print_colored("Generating encryption key...", "GREEN")
try:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
with open(key_path, 'wb') as key_file:
key_file.write(key)
except Exception as e:
print_colored(f"Error generating encryption key: {e}", "RED")
sys.exit(1)
# Create and activate venv
try:
venv.create("venv", with_pip=True)
except Exception as e:
print_colored(f"Error creating virtual environment: {e}", "RED")
sys.exit(1)
# Set venv activation command based on platform
if IS_WINDOWS:
venv_python = os.path.join("venv", "Scripts", "python.exe")
pip_cmd = f'"{venv_python}" -m pip install --upgrade pip'
requirements_cmd = f'"{venv_python}" -m pip install -r requirements.txt'
else:
venv_python = os.path.join("venv", "bin", "python")
pip_cmd = f'"{venv_python}" -m pip install --upgrade pip'
requirements_cmd = f'"{venv_python}" -m pip install -r requirements.txt'
# Install dependencies
if not run_command(pip_cmd, "Upgrading pip"):
print_colored("Failed to upgrade pip. Continuing with installation...", "YELLOW")
if not run_command(requirements_cmd, "Installing requirements"):
print_colored("Installation failed. Please check your internet connection and try again.", "RED")
sys.exit(1)
# Setup Redis
if IS_WINDOWS:
if not os.path.exists("C:\\Program Files\\Redis\\redis-server.exe"):
print_colored("Please install Redis for Windows manually", "YELLOW")
else:
run_command("sudo apt-get update && sudo apt-get install -y redis-server", "Installing Redis")
run_command("sudo systemctl start redis-server", "Starting Redis service")
run_command("sudo systemctl enable redis-server", "Enabling Redis service")
# Run migrations
if not run_command(f'"{venv_python}" manage.py makemigrations', "Creating migrations"):
print_colored("Migration creation failed. Please check your Django configuration.", "RED")
sys.exit(1)
if not run_command(f'"{venv_python}" manage.py migrate', "Applying migrations"):
print_colored("Migration failed. Please check your database configuration.", "RED")
sys.exit(1)
# Create superuser with retry option
superuser_created = create_superuser(venv_python)
print_colored("Installation completed successfully!", "GREEN")
if not superuser_created:
print_colored("Note: Superuser was not created. You can create it later using 'python manage.py createsuperuser'", "YELLOW")
print_colored("Run 'python run.py' to start the application", "GREEN")
except KeyboardInterrupt:
print_colored("\nInstallation interrupted by user.", "YELLOW")
sys.exit(1)
except Exception as e:
print_colored(f"\nUnexpected error: {str(e)}", "RED")
if DEBUG:
raise
sys.exit(1)