-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
564 lines (409 loc) · 16.9 KB
/
Copy pathmain.py
File metadata and controls
564 lines (409 loc) · 16.9 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# -*- coding: utf-8 -*-
try:
import tkinter as tk
from tkinter import messagebox, scrolledtext, filedialog, ttk
except Exception as e:
input(f"Installer is damaged.\nDetail: {str(e)}\nPress Enter for exit.")
quit()
try:
import os
import sys
from threading import Thread
import shutil
from ast import literal_eval
from win32com.client import Dispatch
import winreg
from pathlib import Path
import pythoncom
import pywintypes
import locale
import ctypes
import subprocess
except Exception as e:
messagebox.showerror("Error", "Installer is damaged, impossible to load module.", detail=f"Detail: {str(e)}")
quit()
def main() -> None:
"""
A Python installer for Windows.
"""
if hasattr(sys, "_MEIPASS"):
data_path = os.path.abspath(sys._MEIPASS)
else:
data_path = os.path.abspath("")
try:
f_info = open(os.path.join(data_path, "install_info.txt"), mode="r", encoding="UTF-8")
f_info_r = f_info.read()
f_info.close()
intall_info = literal_eval(f_info_r)
EXECUTABLE = intall_info["EXECUTABLE_NAME"] # the executable file (*.exe)
TEXT_INSTALL = { # set your text for installer
"info":intall_info["INFO"],
"license":intall_info["LICENCE"]
}
APP_NAME = intall_info["APP_NAME"]
APP_SIZE = intall_info["SIZE_MO"]
RUN_COMMAND = intall_info["COMMAND"]
del intall_info
except Exception as e:
messagebox.showerror("Error", "Error with file 'install_info.txt'", detail="Detail: " + str(e))
quit()
UPDATE_TIME = 100
try:
language = locale.getlocale()[0].split("_")[0]
except:
language = "en"
class Trad:
"""The translate"""
T000 = {
"en":"Error",
"fr":"Erreur"
}
T001 = {
"en":"The installer is damaged...",
"fr":"L'instalateur est endomagé..."
}
T002 = {
"en":"Installer",
"fr":"Instalateur"
}
T003 = {
"en":"Next ▶",
"fr":"Suivant ▶"
}
T004 = {
"en":"Licence:",
"fr":"Licence :"
}
T005 = {
"en":"Not agree",
"fr":"Refuser"
}
T006 = {
"en":"Agree",
"fr":"Acepter"
}
T007 = {
"fr":"Paramètres :",
"en":"Setting:"
}
T008 = {
"fr":"Ajouter au bureau",
"en":"Add to desktop"
}
T009 = {
"fr":"Ajouter au path",
"en":"Add to path"
}
T010 = {
"fr":"Dossier d'instalation :",
"en":"Installation folder:"
}
T011 = {
"fr":"📂 Ouvrir",
"en":"📂 Open"
}
T012 = {
"fr":"Erreur",
"en":"Error"
}
T013 = {
"fr":"Le chemain spécifier est vide.",
"en":"The specified path is empty."
}
T014 = {
"fr":"Instalation",
"en":"Install"
}
T015 = {
"fr":"Installation en cours,\nne fermer pas la fenêtre...",
"en":"Insalling,\ndo not close the window..."
}
T016 = {
"en":"Installation is complete.",
"fr":"L'instalation est terminer."
}
T017 = {
"en":"Error",
"fr":"Erreur"
}
T018 = {
"en":"Impossible to create shortcut on desktop.",
"fr":"Impossible de crée le racoursis au bureau."
}
T019 = {
"en":"Detail: {}",
"fr":"Détail : {}"
}
T020 = {
"en":"Error",
"fr":"Erreur"
}
T021 = {
"en":"An error occurred while adding to Path.",
"fr":"Une erreur est arrivé lors de l'ajout au Path."
}
T022 = {
"en":"An error occurred during installation.",
"fr":"Une erreur est survenu durant l'instalation."
}
T023 = {
"en":"Open {}",
"fr":"Ouvrir {}"
}
T024 = {
"en":"Close",
"fr":"Fermer"
}
T025 = {
"en":"Warning",
"fr":"Attention"
}
T026 = {
"en":"{} seems to be already installed.",
"fr":"{} semble déjà être installé."
}
T027 = {
"en":"By installing {}, you will update the application.\nWarning: some configuration files may be reset.\n\nDo you want to continue ?",
"fr":"En installant {}, vous allez mettre à jour l'application.\nAttention : des fichier de configuration peuvent être réinicialiser.\n\nVoulez vous continuer ?"
}
T028 = {
"en":"Impossible to access to application folder.\nMake sure the application is closed and try again...",
"fr":"Impossible d'accéder au dossier de l'application.\nVérifier que l'application est bien fermer et recommencer..."
}
T029 = {
"en":"Install",
"fr":"Installer"
}
bool_agree = None
button_next_1 = None
add_desktop = None
add_path = None
path_copy = os.environ["LOCALAPPDATA"]
def add_to_user_path(folder: str) -> None:
folder = os.path.abspath(folder)
access = winreg.KEY_QUERY_VALUE | winreg.KEY_SET_VALUE
with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, r"Environment", 0, access) as key:
try:
current_path, reg_type = winreg.QueryValueEx(key, "Path")
except FileNotFoundError:
current_path, reg_type = "", winreg.REG_EXPAND_SZ
parts = [p for p in str(current_path).split(";") if p]
normalized = {os.path.normcase(os.path.normpath(p)) for p in parts}
folder_norm = os.path.normcase(os.path.normpath(folder))
if folder_norm not in normalized:
new_path = ";".join(parts + [folder]) if parts else folder
winreg.SetValueEx(key, "Path", 0, reg_type, new_path)
# Notifie Windows qu'une variable d'environnement a changé
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 0x0002
result = ctypes.c_ulong()
ctypes.windll.user32.SendMessageTimeoutW(
HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment",
SMTO_ABORTIFHUNG, 5000, ctypes.byref(result)
)
def step_3() -> None:
"""Configure the frame for step_3"""
def install() -> None:
"""Copy all file."""
nonlocal end_copy, end_zip, install_error
path = os.path.join(path_copy, APP_NAME)
if os.path.isdir(path):
if messagebox.askokcancel(Trad.T025[language], Trad.T026[language].format(APP_NAME), detail=Trad.T027[language].format(APP_NAME)):
try:
shutil.rmtree(path)
except Exception as e:
messagebox.showerror(Trad.T020[language], Trad.T028[language])
install_error = True
quit()
else:
install_error = True
quit()
try:
shutil.unpack_archive(os.path.join(data_path, "data.zip"), path)
end_zip = True
progress_var.set(85)
if add_desktop.get():
registry_path = r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
pythoncom.CoInitialize()
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, registry_path) as key:
desktop_path = winreg.QueryValueEx(key, "Desktop")[0]
path_ink = Path(desktop_path)
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(os.path.join(path_ink, APP_NAME + ".lnk"))
shortcut.TargetPath = os.path.join(path, EXECUTABLE)
shortcut.Description = TEXT_INSTALL["info"]
shortcut.Save()
except pywintypes.com_error as e:
window_install.after(0, lambda: messagebox.showerror(Trad.T017[language], Trad.T018[language], detail=Trad.T019[language].format(str(e))))
except:
window_install.after(0, lambda: messagebox.showerror(Trad.T017[language]))
finally:
pythoncom.CoUninitialize()
progress_var.set(90)
if add_path.get():
try:
exe_dir = os.path.dirname(os.path.join(path, EXECUTABLE))
add_to_user_path(exe_dir)
except Exception as e:
window_install.after(0, lambda: messagebox.showerror(Trad.T020[language], Trad.T021[language], detail=Trad.T019[language].format(str(e))))
progress_var.set(95)
if RUN_COMMAND:
subprocess.run(RUN_COMMAND, shell=True, cwd=os.path.join(path_copy, APP_NAME), text=True, check=True)
progress_var.set(100)
end_copy = True
except Exception as e:
messagebox.showerror(Trad.T020[language], Trad.T022[language], detail=Trad.T019[language].format(str(e)))
install_error = True
quit()
end_copy = False
end_zip = False
install_error = False
thread_copy = Thread(target=install, daemon=True)
thread_copy.start()
window_install.protocol("WM_DELETE_WINDOW", lambda: None)
step[2].destroy()
def while_install() -> None:
"""while the installation, controle if the intall have finish."""
if install_error:
quit()
elif end_copy:
text_copy.destroy()
progress_bar.destroy()
text_end = tk.Label(step[3], text=Trad.T016[language])
text_end.pack()
try:
tk.Label(step[3], text="✔️", fg="#00FF2F", font=("Segoe UI Emoji", 50)).pack()
except:pass
open_app_bool = tk.BooleanVar(step[3])
open_app = tk.Checkbutton(step[3], text=Trad.T023[language].format(APP_NAME), variable=open_app_bool)
open_app.pack(side="left")
def close_install() -> None:
"""Close the install window."""
if open_app_bool.get():
app_dir = os.path.join(path_copy, APP_NAME)
path = os.path.join(app_dir, EXECUTABLE)
subprocess.Popen([path], cwd=app_dir)
window_install.destroy()
window_install.protocol("WM_DELETE_WINDOW", close_install)
close_button = tk.Button(step[3], text=Trad.T024[language], command=close_install)
close_button.pack(side="right")
if install_error:
window_install.destroy()
else:
if not end_zip:
size = sum(f.stat().st_size for f in Path(abs_path).rglob('*') if f.is_file()) / 1048576
if not end_zip: #double controle because end_zip is suppose to change
progress_var.set(size / size_for_progress_bar)
window_install.after(20, while_install)
size_for_progress_bar = APP_SIZE * 85
step[3].configure(text=Trad.T014[language])
step[3].pack(fill="both", expand=True)
abs_path = os.path.join(path_copy, APP_NAME)
text_copy = tk.Label(step[3], text=Trad.T015[language])
text_copy.pack()
progress_var = tk.IntVar(step[3])
progress_bar = ttk.Progressbar(step[3], maximum=100, variable=progress_var, length=430)
progress_bar.pack(pady=20)
while_install()
def step_2() -> None:
"""Configure the frame for step_2"""
def start_next() -> None:
"""Call step_3."""
path = entry_path.get()
if path:
nonlocal path_copy
path_copy = path
step_3()
else:
messagebox.showerror(Trad.T012[language], Trad.T013[language])
def select_folder() -> None:
"""Set the the Entry for path the celected folder by user (with filedialog)."""
path = filedialog.askdirectory(initialdir=path_copy)
if path:
entry_path.delete(0, tk.END)
entry_path.insert(0, path)
nonlocal add_desktop, add_path
step[1].destroy()
step[2].configure(text=Trad.T007[language])
step[2].pack(expand=True, fill="both")
frame_config = tk.Frame(step[2])
frame_config.grid(column=0, row=0)
add_desktop = tk.BooleanVar(step[2], value=True)
add_path = tk.BooleanVar(step[2], value=True)
radio_add_desktop = tk.Checkbutton(frame_config, text=Trad.T008[language], variable=add_desktop)
radio_add_desktop.pack(anchor="w")
radio_add_path = tk.Checkbutton(frame_config, text=Trad.T009[language], variable=add_path)
radio_add_path.pack(anchor="w")
frame_path = tk.LabelFrame(frame_config, text=Trad.T010[language])
frame_path.pack(anchor="w")
entry_path = tk.Entry(frame_path, width=50)
entry_path.insert(0, path_copy)
entry_path.grid(column=0, row=0)
button_open = tk.Button(frame_path, text=Trad.T011[language], command=select_folder)
button_open.grid(column=1, row=0)
button_next_2 = tk.Button(step[2], text=Trad.T029[language], command=start_next)
button_next_2.grid(column=1, row=1, pady=50)
def step_1() -> None:
"""Configure the frame for step_1"""
def start_next() -> None:
"""Call step_2 if the user have agree licence."""
if bool_agree.get():
step_2()
def set_button() -> None:
"""Set the state of button for agree licence."""
try:
button_next_1["state"] = "normal" if bool_agree.get() else "disabled"
window_install.after(UPDATE_TIME, set_button)
except:
pass
global bool_agree, button_next_1
step[0].destroy()
step[1].pack(expand=True, fill="both")
step[1].configure(text=Trad.T004[language])
license_text = scrolledtext.ScrolledText(step[1], height=7, width=50)
license_text.grid(column=0, row=0, columnspan=2)
license_text.insert(0.0, TEXT_INSTALL["license"])
license_text["state"] = "disabled"
frame_agree = tk.Frame(step[1])
frame_agree.grid(column=0, row=1, sticky="w", pady=10)
bool_agree = tk.BooleanVar(frame_agree, value=False)
not_agree = tk.Radiobutton(frame_agree, text=Trad.T005[language], variable=bool_agree, value=False)
not_agree.pack(anchor="w")
agree = tk.Radiobutton(frame_agree, text=Trad.T006[language], variable=bool_agree, value=True)
agree.pack(anchor="w")
button_next_1 = tk.Button(step[1], text=Trad.T003[language], command=start_next)
button_next_1.grid(column=1, row=1)
set_button()
if EXECUTABLE == "":
messagebox.showerror(Trad.T000[language], Trad.T001[language])
quit()
window_install = tk.Tk()
window_install.title(Trad.T002[language])
step = [tk.LabelFrame(window_install) for i in range(4)]
step[0].pack(expand=True, fill="both")
text_app_name = tk.Label(step[0], text=APP_NAME, font=("Arial", 50, "bold"))
text_app_name.grid(column=1, row=0, sticky="sewn")
try:
heit_image = 125
image = tk.PhotoImage(file=os.path.join(data_path, "icon.png"), height=heit_image, width=heit_image)
image_label = tk.Label(step[0], image=image, height=heit_image, width=heit_image)
image_label.grid(column=0, row=0, rowspan=3)
except:
pass
text_info = tk.Label(step[0], text=TEXT_INSTALL["info"])
text_info.grid(column=1, row=1, sticky="sewn")
button_next = tk.Button(step[0], text=Trad.T003[language], command=step_1)
button_next.grid(column=1, row=2, sticky="sewn")
window_install.geometry("430x200")
window_install.resizable(False, False)
try:
window_install.iconphoto(False, image)
except:
pass
window_install.mainloop()
try:
main()
except Exception as e:
messagebox.showerror("Error", "Fatal error during the instalation.", detail=f"Detail: {str(e)}")