-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployer_app.py
More file actions
416 lines (341 loc) · 16.8 KB
/
deployer_app.py
File metadata and controls
416 lines (341 loc) · 16.8 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""
Auto Portable Python Deployer - Tkinter GUI Application
Generates self-contained portable Python deployment packages.
"""
import os
import sys
from pathlib import Path
# Bootstrap: ensure project root is on sys.path
_BASE_DIR = Path(__file__).parent.resolve()
if str(_BASE_DIR) not in sys.path:
sys.path.insert(0, str(_BASE_DIR))
import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox, filedialog
import threading
from config import BASE_DIR, WINDOW_TITLE, WINDOW_SIZE, APP_VERSION
from core.python_manager import PYTHON_VERSIONS, PYTHON_VERSION_LABELS
from core.package_generator import PackageConfig, PackageGenerator
class DeployerApp:
"""Main GUI application for the Portable Python Deployer."""
def __init__(self, root: tk.Tk):
self.root = root
self.root.title(f"{WINDOW_TITLE} v{APP_VERSION}")
self.root.geometry(WINDOW_SIZE)
self.root.minsize(800, 650)
# Theme
self.style = ttk.Style()
available = self.style.theme_names()
for theme in ("vista", "winnative", "clam", "default"):
if theme in available:
self.style.theme_use(theme)
break
self.style.configure("Title.TLabel", font=("Segoe UI", 14, "bold"))
self.style.configure("Section.TLabelframe.Label", font=("Segoe UI", 10, "bold"))
self.style.configure("Generate.TButton", font=("Segoe UI", 11, "bold"))
self.style.configure("Status.TLabel", font=("Segoe UI", 9))
# Variables
self.project_name_var = tk.StringVar(value="MyProject")
self.output_dir_var = tk.StringVar(value=str(BASE_DIR / "output"))
self.python_version_var = tk.StringVar(value="3.12")
self.entry_point_var = tk.StringVar(value="app.py")
self.launcher_name_var = tk.StringVar(value="launcher.bat")
self.include_git_var = tk.BooleanVar(value=False)
self.include_ffmpeg_var = tk.BooleanVar(value=False)
self.include_tkinter_var = tk.BooleanVar(value=True)
self.extra_pth_var = tk.StringVar(value="")
self.extra_pip_args_var = tk.StringVar(value="")
self._generating = False
self._build_ui()
def _build_ui(self):
"""Build the main UI layout."""
# Main container with padding
main = ttk.Frame(self.root, padding=10)
main.pack(fill=tk.BOTH, expand=True)
# Title
title = ttk.Label(main, text="Auto Portable Python Deployer",
style="Title.TLabel")
title.pack(pady=(0, 10))
# Create a paned window: top for config, bottom for log
paned = ttk.PanedWindow(main, orient=tk.VERTICAL)
paned.pack(fill=tk.BOTH, expand=True)
# Top section: config panels
config_frame = ttk.Frame(paned)
paned.add(config_frame, weight=3)
# Bottom section: log output
log_frame = ttk.Frame(paned)
paned.add(log_frame, weight=2)
# Build config panels in a 2-column layout
left_col = ttk.Frame(config_frame)
left_col.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 5))
right_col = ttk.Frame(config_frame)
right_col.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=(5, 0))
self._build_project_panel(left_col)
self._build_options_panel(left_col)
self._build_dependencies_panel(right_col)
self._build_generation_panel(right_col)
self._build_log_panel(log_frame)
def _build_project_panel(self, parent):
"""Project configuration panel."""
frame = ttk.LabelFrame(parent, text=" Project Configuration ",
style="Section.TLabelframe", padding=8)
frame.pack(fill=tk.X, pady=(0, 5))
# Project Name
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Project Name:", width=16, anchor="w").pack(side=tk.LEFT)
ttk.Entry(row, textvariable=self.project_name_var).pack(side=tk.LEFT, fill=tk.X, expand=True)
# Output Directory
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Output Directory:", width=16, anchor="w").pack(side=tk.LEFT)
ttk.Entry(row, textvariable=self.output_dir_var).pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
ttk.Button(row, text="Browse...", width=10,
command=self._browse_output_dir).pack(side=tk.RIGHT)
# Python Version
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Python Version:", width=16, anchor="w").pack(side=tk.LEFT)
version_labels = list(PYTHON_VERSION_LABELS.values())
version_keys = list(PYTHON_VERSION_LABELS.keys())
self._version_combo = ttk.Combobox(row, values=version_labels, state="readonly", width=50)
# Default to 3.12
default_idx = version_keys.index("3.12") if "3.12" in version_keys else 0
self._version_combo.current(default_idx)
self._version_combo.pack(side=tk.LEFT, fill=tk.X, expand=True)
self._version_combo.bind("<<ComboboxSelected>>", self._on_version_change)
self._version_keys = version_keys
# Entry Point
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Entry Point:", width=16, anchor="w").pack(side=tk.LEFT)
ttk.Entry(row, textvariable=self.entry_point_var).pack(side=tk.LEFT, fill=tk.X, expand=True)
# Launcher Name
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Launcher Name:", width=16, anchor="w").pack(side=tk.LEFT)
ttk.Entry(row, textvariable=self.launcher_name_var).pack(side=tk.LEFT, fill=tk.X, expand=True)
def _build_options_panel(self, parent):
"""Options panel with checkboxes."""
frame = ttk.LabelFrame(parent, text=" Portable Components ",
style="Section.TLabelframe", padding=8)
frame.pack(fill=tk.X, pady=(0, 5))
ttk.Checkbutton(frame, text="Include Tkinter (GUI framework)",
variable=self.include_tkinter_var).pack(anchor="w", pady=1)
ttk.Checkbutton(frame, text="Include Portable Git (for git clone operations)",
variable=self.include_git_var).pack(anchor="w", pady=1)
ttk.Checkbutton(frame, text="Include Portable FFmpeg (for audio/video processing)",
variable=self.include_ffmpeg_var).pack(anchor="w", pady=1)
# Extra ._pth paths
sep = ttk.Separator(frame, orient=tk.HORIZONTAL)
sep.pack(fill=tk.X, pady=5)
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Extra ._pth paths:", width=16, anchor="w").pack(side=tk.LEFT)
ttk.Entry(row, textvariable=self.extra_pth_var).pack(side=tk.LEFT, fill=tk.X, expand=True)
tip = ttk.Label(frame, text="Comma-separated relative paths added to Python path (e.g., ..\\mylib,src)",
font=("Segoe UI", 8), foreground="gray")
tip.pack(anchor="w")
# Extra pip args
row = ttk.Frame(frame)
row.pack(fill=tk.X, pady=2)
ttk.Label(row, text="Extra pip args:", width=16, anchor="w").pack(side=tk.LEFT)
ttk.Entry(row, textvariable=self.extra_pip_args_var).pack(side=tk.LEFT, fill=tk.X, expand=True)
tip2 = ttk.Label(frame, text="Extra pip install arguments (e.g., --index-url https://...)",
font=("Segoe UI", 8), foreground="gray")
tip2.pack(anchor="w")
def _build_dependencies_panel(self, parent):
"""Dependencies panel with text area."""
frame = ttk.LabelFrame(parent, text=" Requirements (requirements.txt) ",
style="Section.TLabelframe", padding=8)
frame.pack(fill=tk.BOTH, expand=True, pady=(0, 5))
# Toolbar
toolbar = ttk.Frame(frame)
toolbar.pack(fill=tk.X, pady=(0, 3))
ttk.Button(toolbar, text="Load File...", command=self._load_requirements).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(toolbar, text="Clear", command=self._clear_requirements).pack(side=tk.LEFT)
# Text area
self.req_text = scrolledtext.ScrolledText(frame, height=8, width=40,
font=("Consolas", 10),
wrap=tk.NONE)
self.req_text.pack(fill=tk.BOTH, expand=True)
self.req_text.insert("1.0", "# Add your pip dependencies here, one per line\n"
"# Example:\n"
"# requests>=2.31.0\n"
"# flask\n"
"# numpy\n")
def _build_generation_panel(self, parent):
"""Generation controls."""
frame = ttk.LabelFrame(parent, text=" Generate Package ",
style="Section.TLabelframe", padding=8)
frame.pack(fill=tk.X, pady=(0, 5))
# Progress bar
self.progress = ttk.Progressbar(frame, mode="determinate", maximum=100)
self.progress.pack(fill=tk.X, pady=(0, 5))
# Status label
self.status_label = ttk.Label(frame, text="Ready to generate.",
style="Status.TLabel")
self.status_label.pack(fill=tk.X, pady=(0, 5))
# Buttons
btn_frame = ttk.Frame(frame)
btn_frame.pack(fill=tk.X)
self.generate_btn = ttk.Button(btn_frame, text="Generate Deployment Package",
style="Generate.TButton",
command=self._on_generate)
self.generate_btn.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
self.open_btn = ttk.Button(btn_frame, text="Open Output", state="disabled",
command=self._open_output)
self.open_btn.pack(side=tk.RIGHT)
def _build_log_panel(self, parent):
"""Log output panel."""
frame = ttk.LabelFrame(parent, text=" Log Output ",
style="Section.TLabelframe", padding=5)
frame.pack(fill=tk.BOTH, expand=True, pady=(5, 0))
self.log_text = scrolledtext.ScrolledText(frame, height=6,
font=("Consolas", 9),
state="disabled",
wrap=tk.WORD)
self.log_text.pack(fill=tk.BOTH, expand=True)
# ---- Event Handlers ----
def _on_version_change(self, event=None):
"""Update internal version when combo changes."""
idx = self._version_combo.current()
if 0 <= idx < len(self._version_keys):
self.python_version_var.set(self._version_keys[idx])
def _browse_output_dir(self):
d = filedialog.askdirectory(initialdir=self.output_dir_var.get())
if d:
self.output_dir_var.set(d)
def _load_requirements(self):
f = filedialog.askopenfilename(
filetypes=[("Text files", "*.txt"), ("All files", "*.*")],
title="Load requirements.txt"
)
if f:
try:
content = Path(f).read_text(encoding="utf-8")
self.req_text.delete("1.0", tk.END)
self.req_text.insert("1.0", content)
self._log(f"Loaded requirements from: {f}")
except Exception as e:
messagebox.showerror("Error", f"Failed to load file:\n{e}")
def _clear_requirements(self):
self.req_text.delete("1.0", tk.END)
def _open_output(self):
output_dir = Path(self.output_dir_var.get()) / self.project_name_var.get().replace(" ", "_")
if output_dir.exists():
os.startfile(str(output_dir))
else:
messagebox.showinfo("Info", "Output directory doesn't exist yet.")
def _log(self, message: str):
"""Append message to the log panel (thread-safe)."""
def _do():
self.log_text.config(state="normal")
self.log_text.insert(tk.END, message + "\n")
self.log_text.see(tk.END)
self.log_text.config(state="disabled")
self.root.after(0, _do)
def _update_progress(self, current: int, total: int, message: str):
"""Update progress bar and status (thread-safe)."""
def _do():
pct = int(current / total * 100) if total > 0 else 0
self.progress["value"] = pct
self.status_label.config(text=message)
self.root.after(0, _do)
def _on_generate(self):
"""Start package generation in a background thread."""
if self._generating:
return
# Validate inputs
project_name = self.project_name_var.get().strip()
if not project_name:
messagebox.showwarning("Validation", "Please enter a project name.")
return
output_dir = self.output_dir_var.get().strip()
if not output_dir:
messagebox.showwarning("Validation", "Please select an output directory.")
return
entry_point = self.entry_point_var.get().strip()
if not entry_point:
messagebox.showwarning("Validation", "Please enter an entry point filename.")
return
# Get selected python version
idx = self._version_combo.current()
python_minor = self._version_keys[idx] if 0 <= idx < len(self._version_keys) else "3.12"
# Parse extra pth paths
extra_pth_raw = self.extra_pth_var.get().strip()
extra_pth = [p.strip() for p in extra_pth_raw.split(",") if p.strip()] if extra_pth_raw else []
# Get requirements
requirements = self.req_text.get("1.0", tk.END).strip()
# Filter out comment-only lines for display, but keep all for file
req_lines = [l for l in requirements.split("\n") if l.strip() and not l.strip().startswith("#")]
# Build config
config = PackageConfig(
project_name=project_name,
python_minor=python_minor,
output_dir=Path(output_dir),
entry_point=entry_point,
launcher_name=self.launcher_name_var.get().strip() or "launcher.bat",
requirements=requirements,
include_git=self.include_git_var.get(),
include_ffmpeg=self.include_ffmpeg_var.get(),
include_tkinter=self.include_tkinter_var.get(),
extra_pth_paths=extra_pth,
extra_pip_args=self.extra_pip_args_var.get().strip(),
)
# Log configuration summary
self._log("=" * 50)
self._log(f"Generating deployment package: {project_name}")
self._log(f" Python: {PYTHON_VERSIONS.get(python_minor, '?')} ({python_minor})")
self._log(f" Entry point: {entry_point}")
self._log(f" Output: {output_dir}/{project_name.replace(' ', '_')}")
self._log(f" Tkinter: {'Yes' if config.include_tkinter else 'No'}")
self._log(f" Git: {'Yes' if config.include_git else 'No'}")
self._log(f" FFmpeg: {'Yes' if config.include_ffmpeg else 'No'}")
if req_lines:
self._log(f" Requirements: {len(req_lines)} package(s)")
self._log("-" * 50)
# Disable button, start generation
self._generating = True
self.generate_btn.config(state="disabled")
self.open_btn.config(state="disabled")
def _generate_thread():
try:
generator = PackageGenerator(config)
def progress_cb(current, total, message):
self._update_progress(current, total, message)
self._log(f" {message}")
success = generator.generate(progress_callback=progress_cb)
if success:
self._log("")
self._log("Package generated successfully!")
self._log(f" Location: {generator.output_path}")
self._log("")
self._log("To deploy, run install.bat in the output folder.")
self.root.after(0, lambda: self.open_btn.config(state="normal"))
else:
self._log("")
self._log("ERROR: Package generation failed.")
except Exception as e:
self._log(f"ERROR: {e}")
import traceback
self._log(traceback.format_exc())
finally:
self._generating = False
self.root.after(0, lambda: self.generate_btn.config(state="normal"))
thread = threading.Thread(target=_generate_thread, daemon=True)
thread.start()
def main():
# Check for CLI mode
if len(sys.argv) > 1 and sys.argv[1] == "cli":
from core.cli import run_cli
sys.exit(run_cli(sys.argv[2:]))
# GUI mode
root = tk.Tk()
try:
root.iconbitmap(default="")
except Exception:
pass
app = DeployerApp(root)
root.mainloop()
if __name__ == "__main__":
main()