-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_and_run.py
297 lines (263 loc) · 10.9 KB
/
setup_and_run.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import subprocess
import sys
import platform
import shutil
import logging
# -----------------------------
# Configuration Variables
# -----------------------------
REPO_URL = "https://github.com/Ate329/IDS.git"
PROJECT_NAME = "IDS"
CURRENT_DIR = os.getcwd()
PROJECT_DIR = os.path.join(CURRENT_DIR, PROJECT_NAME)
VENV_DIR = ".venv"
REQUIREMENTS_FILE = "requirements.txt"
CSV_FILE_NAME = "traffic_data.csv"
LOG_FILE = 'setup.log'
# -----------------------------
# Logging Configuration
# -----------------------------
logging.basicConfig(
level=logging.DEBUG, # Set to DEBUG for detailed logs
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout), # Console output
logging.FileHandler(LOG_FILE, mode='w') # File output
]
)
# -----------------------------
# Helper Functions
# -----------------------------
def git_pull():
"""Clone the repository or pull the latest changes if it already exists."""
if not os.path.exists(PROJECT_DIR):
logging.info(f"Cloning repository from {REPO_URL} into {PROJECT_DIR}...")
try:
subprocess.run(["git", "clone", REPO_URL, PROJECT_DIR], check=True)
logging.info("Repository cloned successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to clone repository: {e}")
sys.exit(1)
else:
logging.info("Repository already exists. Pulling latest changes...")
try:
subprocess.run(["git", "-C", PROJECT_DIR, "pull"], check=True)
logging.info("Repository updated successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to pull latest changes: {e}")
sys.exit(1)
def virtualenv_exists(venv_path):
"""Check if the virtual environment already exists."""
python_executable = os.path.join(venv_path, "Scripts", "python.exe") if platform.system() == "Windows" else os.path.join(venv_path, "bin", "python")
return os.path.exists(python_executable)
def get_virtualenv_paths(venv_path):
"""Get the paths to the Python and pip executables inside the virtual environment."""
if platform.system() == "Windows":
python_path = os.path.abspath(os.path.join(venv_path, "Scripts", "python.exe"))
pip_path = os.path.abspath(os.path.join(venv_path, "Scripts", "pip.exe"))
else:
python_path = os.path.abspath(os.path.join(venv_path, "bin", "python"))
pip_path = os.path.abspath(os.path.join(venv_path, "bin", "pip"))
return python_path, pip_path
def setup_virtualenv():
"""Create and activate a virtual environment, and install dependencies."""
venv_path = os.path.join(PROJECT_DIR, VENV_DIR)
if virtualenv_exists(venv_path):
logging.info("Virtual environment already exists. Skipping creation.")
else:
# Find the Python interpreter
python_executable = shutil.which("python") or shutil.which("python3")
if not python_executable:
logging.error("Python interpreter not found in PATH.")
sys.exit(1)
logging.info(f"Using Python interpreter at {python_executable}")
logging.info("Creating virtual environment...")
try:
subprocess.run([python_executable, "-m", "venv", venv_path], check=True)
logging.info("Virtual environment created successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to create virtual environment: {e}")
sys.exit(1)
# Get paths to pip and python inside the virtual environment
python_path, pip_path = get_virtualenv_paths(venv_path)
if not os.path.exists(pip_path):
logging.error("pip not found in the virtual environment.")
sys.exit(1)
logging.info(f"Python path: {python_path}")
logging.info(f"pip path: {pip_path}")
logging.info("Installing dependencies...")
try:
# Upgrade pip (optional)
try:
subprocess.run([pip_path, "install", "--upgrade", "pip"], check=True)
logging.info("Pip upgraded successfully.")
except subprocess.CalledProcessError as e:
logging.warning(f"Failed to upgrade pip: {e}. Proceeding with existing pip version.")
subprocess.run([pip_path, "install", "-r", os.path.join(PROJECT_DIR, REQUIREMENTS_FILE)], check=True)
logging.info("Dependencies installed successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to install dependencies: {e}")
sys.exit(1)
def create_csv_file():
"""Create an empty traffic_data.csv file in the project directory."""
csv_file_path = os.path.join(PROJECT_DIR, CSV_FILE_NAME)
if not os.path.exists(csv_file_path):
logging.info(f"Creating empty {CSV_FILE_NAME} at {csv_file_path}...")
try:
with open(csv_file_path, 'w') as csv_file:
pass # Creates an empty file without writing any data
logging.info(f"{CSV_FILE_NAME} created successfully.")
except Exception as e:
logging.error(f"Failed to create {CSV_FILE_NAME}: {e}")
sys.exit(1)
else:
logging.info(f"{CSV_FILE_NAME} already exists. Skipping creation.")
def run_migrations():
"""Run makemigrations and migrate commands."""
manage_py_dir = os.path.join(PROJECT_DIR, "ids_project")
manage_py_path = os.path.join(manage_py_dir, "manage.py")
venv_path = os.path.join(PROJECT_DIR, VENV_DIR)
python_path, _ = get_virtualenv_paths(venv_path)
logging.info(f"manage.py path: {manage_py_path}")
logging.info(f"Python executable path: {python_path}")
if not os.path.exists(manage_py_path):
logging.error(f"manage.py not found at {manage_py_path}")
sys.exit(1)
if not os.path.exists(python_path):
logging.error(f"Python executable not found at {python_path}")
sys.exit(1)
# Change working directory to manage_py_dir
os.chdir(manage_py_dir)
logging.info(f"Changed working directory to {os.getcwd()}")
# Prepare environment variables
env = os.environ.copy()
env["PATH"] = os.pathsep.join([os.path.dirname(python_path), env.get("PATH", "")])
logging.debug(f"Environment PATH: {env['PATH']}")
# Run makemigrations for main app
logging.info("Running makemigrations for main app...")
command = [python_path, manage_py_path, "makemigrations"]
logging.info(f"Executing command: {' '.join(command)}")
try:
result = subprocess.run(
command,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env
)
logging.info(f"Makemigrations output:\n{result.stdout}")
if result.stderr:
logging.warning(f"Makemigrations warnings/errors:\n{result.stderr}")
except Exception as e:
logging.error(f"An error occurred during makemigrations: {e}")
logging.error("Traceback:", exc_info=True)
sys.exit(1)
# Run makemigrations for ids_app
logging.info("Running makemigrations for ids_app...")
command = [python_path, manage_py_path, "makemigrations", "ids_app"]
logging.info(f"Executing command: {' '.join(command)}")
try:
result = subprocess.run(
command,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env
)
logging.info(f"Makemigrations output:\n{result.stdout}")
if result.stderr:
logging.warning(f"Makemigrations warnings/errors:\n{result.stderr}")
except Exception as e:
logging.error(f"An error occurred during makemigrations: {e}")
logging.error("Traceback:", exc_info=True)
sys.exit(1)
# Run migrate for main app
logging.info("Applying database migrations for main app...")
command = [python_path, manage_py_path, "migrate"]
logging.info(f"Executing command: {' '.join(command)}")
try:
result = subprocess.run(
command,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env
)
logging.info(f"Migrate output:\n{result.stdout}")
if result.stderr:
logging.warning(f"Migrate warnings/errors:\n{result.stderr}")
except Exception as e:
logging.error(f"An error occurred during migrate: {e}")
logging.error("Traceback:", exc_info=True)
sys.exit(1)
# Run migrate for ids_app
logging.info("Applying database migrations for ids_app...")
command = [python_path, manage_py_path, "migrate", "ids_app"]
logging.info(f"Executing command: {' '.join(command)}")
try:
result = subprocess.run(
command,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env
)
logging.info(f"Migrate output:\n{result.stdout}")
if result.stderr:
logging.warning(f"Migrate warnings/errors:\n{result.stderr}")
except Exception as e:
logging.error(f"An error occurred during migrate: {e}")
logging.error("Traceback:", exc_info=True)
sys.exit(1)
def run_django():
"""Run the Django development server."""
manage_py_dir = os.path.join(PROJECT_DIR, "ids_project")
manage_py_path = os.path.join(manage_py_dir, "manage.py")
venv_path = os.path.join(PROJECT_DIR, VENV_DIR)
python_path, _ = get_virtualenv_paths(venv_path)
if not os.path.exists(manage_py_path):
logging.error(f"manage.py not found at {manage_py_path}")
sys.exit(1)
if not os.path.exists(python_path):
logging.error(f"Python executable not found at {python_path}")
sys.exit(1)
# Change working directory to manage_py_dir
os.chdir(manage_py_dir)
logging.info(f"Changed working directory to {os.getcwd()}")
# Prepare environment variables
env = os.environ.copy()
env["PATH"] = os.pathsep.join([os.path.dirname(python_path), env.get("PATH", "")])
logging.debug(f"Environment PATH: {env['PATH']}")
logging.info("Starting Django development server...")
command = [python_path, "manage.py", "runserver"]
logging.info(f"Executing command: {' '.join(command)}")
try:
subprocess.run(
command,
check=True,
env=env
)
except Exception as e:
logging.error(f"An error occurred while running the server: {e}")
logging.error("Traceback:", exc_info=True)
sys.exit(1)
# -----------------------------
# Main Execution Block
# -----------------------------
if __name__ == "__main__":
try:
logging.info("===== Starting the Setup Process =====")
git_pull()
setup_virtualenv()
create_csv_file()
run_migrations()
run_django()
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
logging.error("Traceback:", exc_info=True)
sys.exit(1)