/
run.py
executable file
·290 lines (241 loc) · 7.6 KB
/
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
#!/usr/bin/env python3
"""
Run the frontend and backend servers in parallel.
"""
import io
import multiprocessing
import os
import re
import socket
import sys
import unicodedata
from datetime import datetime
from functools import wraps
from http.server import HTTPServer, SimpleHTTPRequestHandler
from time import time
import toml
from fullstack_api.app import app
FRONTEND_FOLDER = os.path.join("frontend", "fullstack_ui")
LOGS_FOLDER = "logs"
FRONTEND_PORT = 8000
BACKEND_PORT = 5000
def create_logs_folder():
"""
Create the logs folder if it does not exist.
"""
if not os.path.exists(LOGS_FOLDER):
os.mkdir(LOGS_FOLDER)
def print_info(func_name):
"""
Print information about the current process.
"""
print(
text_marker("#", 50)
+ f"time: {datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')}\n"
f"func name: {func_name}\n"
f"parent process: {os.getppid()}\n"
f"process id: {os.getpid()}\n" + text_marker("#", 50)
)
class CustomStdout(io.TextIOWrapper):
"""
Allows writing to a log file, while echoing to stdout.
"""
def __init__(
self, stdout: io.TextIOWrapper, output_list: list, label: str
):
super().__init__(
stdout.buffer,
stdout.encoding,
stdout.errors,
stdout.newlines,
stdout.line_buffering,
)
self.output_list = output_list
self.stdout = stdout
self.label = label
def write(self, text: str):
if isinstance(text, bytes):
text = text.decode("utf-8")
text = text.strip()
text = trim_long_lines(text, 90)
if text:
self.output_list.append(f"{text}\n")
self.stdout.write(f"\n>>> {self.label} >>>\n{text}\n")
self.flush()
def strip_ansi_codes(str_input: str):
"""
Strip ANSI codes from a string.
cf. https://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences
"""
return re.sub(
r"\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?",
"",
str_input,
)
def text_marker(symbol: str, length: int):
"""
Create a text marker.
"""
return symbol * length + "\n"
def trim_long_lines(text, max_length):
"""
Trim long lines in the text without splitting words.
Args:
text (str): The input text.
max_length (int): The maximum length for a line.
Returns:
str: The text with trimmed lines.
"""
lines = text.split("\n")
trimmed_lines = []
for line in lines:
words = line.split()
current_line = ""
for word in words:
if len(current_line) + len(word) + 1 <= max_length:
current_line += word + " "
else:
trimmed_lines.append(current_line.strip())
current_line = word + " "
if current_line:
trimmed_lines.append(current_line.strip())
return "\n".join(trimmed_lines)
def log_stdout(func: callable):
"""
Wrapper for sys.stdout.write to write to a log file.
"""
@wraps(func)
def wrapper(*args, **kwargs):
funcname = func.__name__
create_logs_folder()
logs = ["\n", text_marker("~", 50)]
original_stdout = sys.stdout
original_stderr = sys.stderr
sys.stdout = CustomStdout(original_stdout, logs, funcname)
sys.stderr = CustomStdout(original_stderr, logs, funcname)
print_info(funcname)
start_time = time()
func(*args, **kwargs)
end_time = time()
run_time = end_time - start_time
print(
text_marker("#", 50)
+ f"{func.__name__} runtime: {run_time:.2f}seconds\n"
+ text_marker("#", 50)
)
logs.append(text_marker("~", 50))
with open(
os.path.join(LOGS_FOLDER, "logs.txt"),
"a",
encoding="ascii",
) as logfile:
for item in logs:
if isinstance(item, bytes):
item = item.decode("utf-8")
unicode_normalized_item = unicodedata.normalize(
"NFKD", item
).encode("ascii", "ignore")
try:
logfile.write(strip_ansi_codes(item))
except TypeError:
logfile.write(
unicode_normalized_item.decode("ascii")
)
return wrapper
def is_port_busy(port: int):
"""
Check if a port is busy.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(("localhost", port))
return False # Port is available
except socket.error:
return True # Port is already in use
finally:
sock.close()
def serve_on_available_port(func: callable):
"""
Decorator to try to start a server on a port, and if it fails, try the next
port until it succeeds or the max_tries limit is reached.
"""
@wraps(func)
def wrapper(*args, **kwargs):
port = kwargs.get("port", 8000) # Default port value
max_tries = kwargs.get(
"max_tries", 10
) # Default max_tries value
i = 0
while i < max_tries:
if not is_port_busy(port):
kwargs["port"] = port
return func(*args, **kwargs)
else:
print(f"\nPort {port} is busy...")
port += 1
i += 1
if i == max_tries:
print(
f"Reached attempt limit of {max_tries}, "
"try starting with a different port"
)
return None
return wrapper
def create_server(site_folder: str, address: str, port: int):
"""
Create a server to serve the files in the site_folder directory.
"""
class Handler(SimpleHTTPRequestHandler):
"""
Handler class to serve the files in the site_folder directory.
"""
def __init__(self, *args, **kwargs):
super().__init__(directory=site_folder, *args, **kwargs)
return HTTPServer((address, port), Handler)
@log_stdout
@serve_on_available_port
def frontend(
site_folder=FRONTEND_FOLDER, port=8000, address="localhost"
):
"""
Serve the frontend on the given port.
"""
server = create_server(site_folder, address, port)
print(
f"\nFrontend is live at https://{address}:{port}\n"
f"Serving files from {site_folder}\n\n"
)
try:
server.serve_forever()
except KeyboardInterrupt:
print("Keyboard interrupt received, exiting.")
server.server_close()
@log_stdout
@serve_on_available_port
def backend(port=5000, address="localhost"):
"""
Serve the backend on the given port.
"""
print(f"Backend is live at https://{address}:{port}")
app.run(host=address, port=port)
if __name__ == "__main__":
print("Launching fullstack app server...")
with open("fullstack.toml", "r", encoding="utf-8") as f:
config = toml.load(f).get("local", {})
frontend_port = config.get("frontend_port") or FRONTEND_PORT
frontend_folder = (
os.path.join(*config.get("frontend_folder"))
or FRONTEND_FOLDER
)
backend_port = config.get("backend_port") or BACKEND_PORT
frontend_process = multiprocessing.Process(
target=frontend,
kwargs={"port": frontend_port, "site_folder": frontend_folder},
)
backend_process = multiprocessing.Process(
target=backend, kwargs={"port": backend_port}
)
frontend_process.start()
backend_process.start()
frontend_process.join()
backend_process.join()