-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_protection.py
More file actions
379 lines (325 loc) · 15.1 KB
/
py_protection.py
File metadata and controls
379 lines (325 loc) · 15.1 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import tkinter as tk
from tkinter import filedialog
import subprocess
import threading
import os
import sys
bg = "#0f0f0f"
panel = "#1a1a1a"
accent = "#00ff88"
dim = "#555555"
text = "#dddddd"
green = "#00ff88"
tick = "✓"
cross = "✕"
warn = "⚠"
arrow = "▶"
clock = "⏳"
nuitka_deps = ["nuitka", "zstandard"]
pyarmor_deps = ["pyarmor", "pyinstaller"]
class darkscrollbar(tk.Canvas):
def __init__(self, parent, command, bg=bg, fg=accent, **kwargs):
super().__init__(parent, width=6, bg=bg, highlightthickness=0, bd=0, **kwargs)
self.command = command
self.fg = fg
self._thumb = self.create_rectangle(0, 0, 6, 40, fill=fg, outline="", width=0)
self.bind("<ButtonPress-1>", self._on_click)
self.bind("<B1-Motion>", self._on_drag)
self._lo = 0.0
self._hi = 1.0
self._drag_start = None
def set(self, lo, hi):
self._lo = float(lo)
self._hi = float(hi)
self._draw()
def _draw(self):
h = self.winfo_height() or 1
y0 = int(self._lo * h)
y1 = int(self._hi * h)
if y1 - y0 < 8:
y1 = y0 + 8
self.coords(self._thumb, 1, y0, 5, y1)
def _on_click(self, e):
self._drag_start = e.y
h = self.winfo_height() or 1
frac = e.y / h
self.command("moveto", frac - (self._hi - self._lo) / 2)
def _on_drag(self, e):
if self._drag_start is None:
return
h = self.winfo_height() or 1
delta = (e.y - self._drag_start) / h
self._drag_start = e.y
self.command("moveto", self._lo + delta)
class app:
def __init__(self, root):
self.root = root
self.root.title("Py Protection (by SolaraUser123)")
self.root.geometry("500x700")
self.root.configure(bg=bg)
self.root.resizable(True, True)
self.root.minsize(420, 500)
self.filepath = ""
self.method = tk.StringVar(value="nuitka")
self.running = False
self.cmd_box = None
self.nuitka_flags = {
"--onefile": tk.BooleanVar(value=True),
"--standalone": tk.BooleanVar(value=True),
"--lto=yes": tk.BooleanVar(value=True),
"--remove-output": tk.BooleanVar(value=True),
"--python-flag=no_docstrings": tk.BooleanVar(value=True),
"--python-flag=no_asserts": tk.BooleanVar(value=True),
"--enable-plugin=tk-inter": tk.BooleanVar(value=False),
"--enable-plugin=numpy": tk.BooleanVar(value=False),
"--enable-plugin=qt-plugins": tk.BooleanVar(value=False),
"--disable-console": tk.BooleanVar(value=False),
"--mingw64": tk.BooleanVar(value=False),
"--no-debug-immortal-assumptions": tk.BooleanVar(value=False),
}
self.pyarmor_flags = {
"--enable-jit": tk.BooleanVar(value=True),
"--assert-call": tk.BooleanVar(value=True),
"--assert-import": tk.BooleanVar(value=True),
}
self.pyinstaller_flags = {
"--onefile": tk.BooleanVar(value=True),
"--noupx": tk.BooleanVar(value=True),
"--noconsole": tk.BooleanVar(value=False),
}
self.build()
def sep(self, parent):
tk.Frame(parent, bg="#2a2a2a", height=1).pack(fill="x", pady=8)
def lbl(self, parent, txt, color=text, size=9, bold=False):
tk.Label(parent, text=txt, bg=parent["bg"], fg=color,
font=("Consolas", size, "bold" if bold else "normal")).pack(anchor="w")
def mk_btn(self, parent, txt, cmd, small=False, dimmed=False):
color = dim if dimmed else accent
size = 8 if small else 9
return tk.Button(parent, text=txt, command=cmd,
bg=panel, fg=color,
font=("Consolas", size),
bd=0, padx=10, pady=4,
relief="flat", cursor="hand2",
activebackground="#2a2a2a", activeforeground=color,
highlightbackground=color, highlightthickness=1)
def build(self):
outer = tk.Frame(self.root, bg=bg)
outer.pack(fill="both", expand=True)
canvas = tk.Canvas(outer, bg=bg, highlightthickness=0)
main_sb = darkscrollbar(outer, command=canvas.yview, bg=bg, fg=accent)
canvas.configure(yscrollcommand=main_sb.set)
main_sb.pack(side="right", fill="y", padx=(0, 2))
canvas.pack(side="left", fill="both", expand=True)
self.wrap = tk.Frame(canvas, bg=bg, padx=18, pady=14)
win_id = canvas.create_window((0, 0), window=self.wrap, anchor="nw")
self.wrap.bind("<Configure>",
lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.bind("<Configure>",
lambda e: canvas.itemconfig(win_id, width=e.width))
canvas.bind_all("<MouseWheel>",
lambda e: canvas.yview_scroll(-1 * (e.delta // 120), "units"))
tk.Label(self.wrap, text="Py Protection", bg=bg, fg=accent,
font=("Consolas", 15, "bold")).pack(anchor="w")
tk.Label(self.wrap, text="by SolaraUser123", bg=bg, fg=dim,
font=("Consolas", 8)).pack(anchor="w", pady=(0, 8))
self.sep(self.wrap)
self.lbl(self.wrap, "FILE", accent, 8, True)
self.file_lbl = tk.Label(self.wrap, text="No file selected", bg=bg, fg=dim,
font=("Consolas", 9), anchor="w")
self.file_lbl.pack(fill="x", pady=(3, 4))
fr = tk.Frame(self.wrap, bg=bg)
fr.pack(anchor="w")
self.mk_btn(fr, "Browse", self.browse).pack(side="left", padx=(0, 6))
self.mk_btn(fr, "Clear", self.clear_file, dimmed=True).pack(side="left")
self.sep(self.wrap)
self.lbl(self.wrap, "METHOD", accent, 8, True)
mr = tk.Frame(self.wrap, bg=bg)
mr.pack(anchor="w", pady=(4, 0))
for val, txt in [("nuitka", "Nuitka"), ("pyarmor", "PyArmor + PyInstaller")]:
tk.Radiobutton(mr, text=txt, variable=self.method, value=val,
command=self.switch_method,
bg=bg, fg=text, selectcolor=bg,
activebackground=bg, activeforeground=accent,
font=("Consolas", 9)).pack(side="left", padx=(0, 14))
self.sep(self.wrap)
self.lbl(self.wrap, "OPTIONS", accent, 8, True)
self.opts_frame = tk.Frame(self.wrap, bg=bg)
self.opts_frame.pack(fill="x", pady=(4, 0))
self.render_opts()
self.sep(self.wrap)
cr = tk.Frame(self.wrap, bg=bg)
cr.pack(fill="x")
self.lbl(cr, "COMMAND", accent, 8, True)
self.mk_btn(cr, "Copy", self.copy_cmd, small=True).pack(side="right")
self.cmd_box = tk.Text(self.wrap, bg=panel, fg=accent, insertbackground=accent,
font=("Consolas", 8), height=3, bd=0,
padx=8, pady=6, wrap="word", state="disabled",
highlightbackground="#2a2a2a", highlightthickness=1)
self.cmd_box.pack(fill="x", pady=(4, 0))
self.sep(self.wrap)
self.run_btn = tk.Button(self.wrap, text=arrow + " Run Protection",
command=self.run, bg=panel, fg=accent,
font=("Consolas", 11, "bold"),
bd=0, pady=10, cursor="hand2", relief="flat",
activebackground="#2a2a2a", activeforeground=accent,
highlightbackground=accent, highlightthickness=1)
self.run_btn.pack(fill="x")
self.sep(self.wrap)
self.lbl(self.wrap, "OUTPUT", accent, 8, True)
log_frame = tk.Frame(self.wrap, bg=panel,
highlightbackground="#2a2a2a", highlightthickness=1)
log_frame.pack(fill="both", expand=True, pady=(4, 0))
log_sb = darkscrollbar(log_frame, command=None, bg=panel, fg=accent)
log_sb.pack(side="right", fill="y", padx=(0, 2), pady=2)
self.log = tk.Text(log_frame, bg=panel, fg=text,
font=("Consolas", 8), height=9, bd=0,
padx=8, pady=6, state="disabled",
wrap="word", yscrollcommand=log_sb.set)
self.log.pack(side="left", fill="both", expand=True)
log_sb.command = self.log.yview
self.mk_btn(self.wrap, "Clear Output", self.clear_log,
small=True, dimmed=True).pack(anchor="e", pady=(4, 0))
self.update_cmd()
def render_opts(self):
for w in self.opts_frame.winfo_children():
w.destroy()
groups = ([("Nuitka Flags", self.nuitka_flags)] if self.method.get() == "nuitka"
else [("PyArmor Flags", self.pyarmor_flags),
("PyInstaller Flags", self.pyinstaller_flags)])
for name, flags in groups:
tk.Label(self.opts_frame, text=name, bg=bg, fg=dim,
font=("Consolas", 8)).pack(anchor="w", pady=(6, 2))
for flag, var in flags.items():
tk.Checkbutton(self.opts_frame, text=flag, variable=var,
command=self.update_cmd,
bg=bg, fg=text, selectcolor=bg,
activebackground=bg, activeforeground=accent,
font=("Consolas", 9)).pack(anchor="w")
def switch_method(self):
self.render_opts()
self.update_cmd()
def update_cmd(self):
if not self.cmd_box:
return
fp = self.filepath if self.filepath else "C:\\path\\to\\script.py"
fname = os.path.basename(fp)
dist = os.path.join(os.path.dirname(fp), "dist", fname) if self.filepath else f"dist\\{fname}"
if self.method.get() == "nuitka":
flags = [f for f, v in self.nuitka_flags.items() if v.get()]
cmd = f'nuitka {" ".join(flags)} "{fp}"'
else:
pa = [f for f, v in self.pyarmor_flags.items() if v.get()]
pi = [f for f, v in self.pyinstaller_flags.items() if v.get()]
cmd = f'pyarmor gen {" ".join(pa)} "{fp}"\npyinstaller {" ".join(pi)} "{dist}"'
self.cmd_box.configure(state="normal")
self.cmd_box.delete("1.0", "end")
self.cmd_box.insert("1.0", cmd)
self.cmd_box.configure(state="disabled")
def copy_cmd(self):
self.root.clipboard_clear()
self.root.clipboard_append(self.cmd_box.get("1.0", "end").strip())
def browse(self):
path = filedialog.askopenfilename(filetypes=[("Python files", "*.py")])
if path:
self.filepath = path
self.file_lbl.configure(text=tick + " " + os.path.basename(path), fg=green)
self.update_cmd()
def clear_file(self):
self.filepath = ""
self.file_lbl.configure(text="No file selected", fg=dim)
self.update_cmd()
def log_write(self, msg):
self.log.configure(state="normal")
self.log.insert("end", msg + "\n")
self.log.see("end")
self.log.configure(state="disabled")
def clear_log(self):
self.log.configure(state="normal")
self.log.delete("1.0", "end")
self.log.configure(state="disabled")
def check_deps(self):
deps = nuitka_deps if self.method.get() == "nuitka" else pyarmor_deps
missing = []
for d in deps:
r = subprocess.run([sys.executable, "-m", "pip", "show", d], capture_output=True)
if r.returncode != 0:
missing.append(d)
if not missing:
self.log_write(tick + " All dependencies installed.\n")
return True
self.log_write("Missing: " + ", ".join(missing) + " — installing...")
for d in missing:
r = subprocess.run([sys.executable, "-m", "pip", "install", d],
capture_output=True, text=True)
if r.returncode == 0:
self.log_write(" " + tick + " " + d)
else:
self.log_write(" " + cross + " Failed: " + d + "\n" + r.stderr)
return False
self.log_write("Ready.\n")
return True
def run(self):
if self.running:
return
if not self.filepath:
self.log_write(warn + " No file selected.")
return
if not os.path.exists(self.filepath):
self.log_write(cross + " File not found.")
return
self.running = True
self.run_btn.configure(text=clock + " Running...", state="disabled")
threading.Thread(target=self._thread, daemon=True).start()
def _thread(self):
if not self.check_deps():
self._done()
return
fp = self.filepath
fname = os.path.basename(fp)
env = os.environ.copy()
scripts = os.path.join(os.path.dirname(sys.executable), "Scripts")
env["PATH"] = scripts + os.pathsep + env.get("PATH", "")
def run_cmd(cmd, label):
self.log_write(arrow + " " + label + "\n " + cmd + "\n")
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True,
env=env, cwd=os.path.dirname(fp))
for line in proc.stdout:
self.log_write(line.rstrip())
proc.wait()
return proc.returncode == 0
if self.method.get() == "nuitka":
flags = [f for f, v in self.nuitka_flags.items() if v.get()]
ok = run_cmd('nuitka ' + " ".join(flags) + ' "' + fp + '"', "Nuitka")
if ok:
self.log_write("\n" + tick + " Done: " + os.path.splitext(fp)[0] + ".exe")
else:
self.log_write("\n" + cross + " Nuitka failed.")
else:
pa = [f for f, v in self.pyarmor_flags.items() if v.get()]
ok1 = run_cmd('pyarmor gen ' + " ".join(pa) + ' "' + fp + '"', "PyArmor")
if not ok1:
self.log_write("\n" + cross + " PyArmor failed.")
self._done()
return
dist = os.path.join(os.path.dirname(fp), "dist", fname)
if not os.path.exists(dist):
self.log_write("\n" + cross + " Not found: " + dist)
self._done()
return
pi = [f for f, v in self.pyinstaller_flags.items() if v.get()]
ok2 = run_cmd('pyinstaller ' + " ".join(pi) + ' "' + dist + '"', "PyInstaller")
out = os.path.join(os.path.dirname(fp), "dist", os.path.splitext(fname)[0] + ".exe")
if ok2:
self.log_write("\n" + tick + " Done: " + out)
else:
self.log_write("\n" + cross + " PyInstaller failed.")
self._done()
def _done(self):
self.running = False
self.run_btn.configure(text=arrow + " Run Protection", state="normal")
if __name__ == "__main__":
root = tk.Tk()
app(root)
root.mainloop()