- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
Latest commit
309 lines (308 loc) · 12 KB
/
Copy pathsetup.py
File metadata and controls
309 lines (308 loc) · 12 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
fromtimeimportsleep
fromzipfileimportZipFile
fromshutilimportcopy, rmtree
frompythoncomimportCoInitialize
fromwin32com.clientimportDispatch
fromwin32pdhimportEnumObjects, EnumObjectItems
fromosimportgetcwd, environ, remove, rename
fromos.pathimportjoin, isfile, isdir, basename, dirname
fromthreadingimportThread, active_count
fromtkinter.filedialogimportaskdirectory
fromtkinter.messageboximportshowerror, showinfo, askyesno
fromtkinterimportttk, Tk, Frame, Label, Text, Toplevel, INSERT, HORIZONTAL
fromwinregimportOpenKey, CreateKey, KEY_ALL_ACCESS, QueryValueEx, SetValueEx, REG_SZ, HKEY_CURRENT_USER, DeleteKey, DeleteValue
fileName='TPbuilder.exe'
Desktop=join('C:'+environ["HOMEPATH"], 'Desktop')
defCheckSetup(Path=''):
ifnotisfile(join(Desktop, 'TPbuilder.lnk')):
return(False)
ifnotisfile(join(Path, 'models.json')):
return(False)
foreach_filein ['Fortilogo.ico', 'Fortilogo.gif']:
ifnotisfile(join(join(Path, 'image'), each_file)):
return(False)
return(True)
defgetProcesses(ProcessList=[], all=1):
templist= []
foreach_processinProcessList:
EnumObjects(None, None, 0, 1)
(items, instance) =EnumObjectItems(None, None, "Process", -1)
ifeach_processininstance:
templist.append(each_process)
ifall==0:
return(instance)
else:
return(templist)
defRealeasepkg(pkg='', dest=''):
ifisfile(pkg):
package=pkg+'.zip'
copy(pkg, package)
try:
withZipFile(package) aszipf:
zipf.extractall(dest, pwd=b'P)h>@6aWAK2mm&gW=RK-r8gW8002}h000jF003}la4%n9X>MtBUtcb8d8JxybK5o&{;pqv$n}tHC^l~')
remove(package)
exceptIOErrorasioerr:
showerror('ERROR', str(ioerr))
defRegistrar(Action='get', RegPath='', RegName='', RegValue=''):
try:
ClassDict= {'HKEY_CURRENT_USER':HKEY_CURRENT_USER}
mainkey=RegPath.split('\\')[0]
RegRelativePath=RegPath.replace(mainkey+'\\', '')
withOpenKey(ClassDict[mainkey], RegRelativePath, 0, KEY_ALL_ACCESS) askey:
ifAction=='get':
value=QueryValueEx(key, RegName)[0]
return(value)
elifAction=='set':
SetValueEx(key, RegName, 0, REG_SZ, RegValue)
ifQueryValueEx(key, RegName)[0]:
return(True)
else:
return(False)
elifAction=='setkey':
CreateKey(ClassDict[mainkey], join(RegRelativePath, RegName))
elifAction=='rmvalue':
DeleteValue(key, RegName)
elifAction=='rmkey':
DeleteKey(ClassDict[mainkey], join(RegRelativePath, RegName))
exceptFileNotFoundErrorasregerr:
pass
defcreateShortcut(fileName):
try:
CoInitialize()
winscript=Dispatch("wscript.shell")
link=join(Desktop, fileName.replace('.exe', '.lnk'))
ifisfile(link):
remove(link)
shortcut=winscript.CreateShortcut(link)
shortcut.TargetPath='\"{0}\"'.format(join(installPath, fileName))
shortcut.Save()
exceptExceptionaserr:
showerror('ERROR', str(err))
defisAlive(logic=None, threadNum=0):
while(True):
iflogic=='lt':
ifactive_count() <threadNum:
break
eliflogic=='gt':
ifactive_count() >threadNum:
break
sleep(0.5)
classMultiProcess(Thread):
def__init__(self, action, event, delay):
Thread.__init__(self)
self.action=action
self.event=event
self.delay=delay
defrun(self):
ProcessAction(self.action, self.event, self.delay)
defProcessAction(action=None, event=None, delay=0):
globalfileName
globalinstallPath
ifaction=='create':
ifevent=='Install':
Realeasepkg(join(getcwd(), 'package'), installPath)
createShortcut(fileName)
Registrar('setkey', r'HKEY_CURRENT_USER\Software', 'TPbuilder')
Registrar('set', r'HKEY_CURRENT_USER\Software\TPbuilder', 'TBPath', join(installPath, 'image'))
elifevent=='Uninstall':
ifisdir(installPath):
try:
rmtree(installPath)
except:
pass
ifisfile(join(Desktop, 'TPbuilder.lnk')):
remove(join(Desktop, 'TPbuilder.lnk'))
Registrar('rmvalue', r'HKEY_CURRENT_USER\Software\TPbuilder', 'TBPath')
Registrar('rmkey', r'HKEY_CURRENT_USER\Software', 'TPbuilder')
else:
ProgressBar(delay, event)
root.withdraw()
Finish(event)
classProgressBar(Toplevel):
def__init__(self, delay, event):
Toplevel.__init__(self)
self.event=event
self.delay=delay
self.geometry('400x48+470+420')
self.resizable(0, 0)
self.focus()
self.grab_set()
self.createWidgets()
defcreateWidgets(self):
self.progress=ttk.Progressbar(self, orient=HORIZONTAL, length=400, mode='determinate')
self.progress.place(x=0, y=0)
self.BT=ttk.Button(self, style="TButton", text='Run')
self.BT.place(x=168, y=22)
self.title('{0}ing TPbuilder'.format(self.event))
defclose_E():
self.title('Do not close window while {0}ing'.format(self.event.lower()))
self.protocol('WM_DELETE_WINDOW', close_E)
self.config(cursor="wait")
self.BT.config(state="normal", text='Run')
ttk.Style().configure("TButton", foreground="blue")
forcountinrange(101):
ifcount>=1:
self.progress['value'] =count
self.BT['text'] =str(count) +'%'
self.update_idletasks()
sleep(self.delay)
ifself.progress['value'] ==99:
isAlive('lt', 3)
self.config(cursor="")
ttk.Style().configure("TButton", foreground="black")
self.BT.config(state="normal", text='Complete')
sleep(1)
showinfo('Information', 'TPbuilder has been {0}ed'.format(self.event.lower()))
self.destroy()
classFinish(Toplevel):
def__init__(self, event):
Toplevel.__init__(self)
self.event=event
self.title('TPbuilder Setup')
self.geometry('450x250+450+300')
self.resizable(0, 0)
ifisfile(join(getcwd(), 'install.ico')):
self.iconbitmap(default=join(getcwd(), 'install.ico'))
self.createWidgets()
self.protocol('WM_DELETE_WINDOW', root.quit)
defcreateWidgets(self):
self.TA=Text(self, height=5, width=65, bg='#FFFFFF', cursor='', selectbackground='#FFFFFF')
self.TA.config(state='disabled')
self.TA.place(x=-5, y=0)
self.LB=Label(self, text="Finish {0}".format(self.event), font='Calibri 11 bold', bg='#FFFFFF')
self.LB.place(x=10, y=10)
self.LB2=Label(self, text="At last {0} complete!".format(self.event))
ifself.event=='Uninstall':
globalinsDir
LBText=" TPbuilder has been removed, click Finish to exit."
self.LB1=Label(self, text="Removed item listed:")
self.LB1.place(x=15, y=71)
self.TA1=Text(self, height=6, width=47, bg='#FFFFFF', cursor='')
self.TA1.insert(INSERT, join(Desktop, 'TPbuilder.lnk') +'\n')
self.TA1.insert(INSERT, join(insDir, 'TPbuilder.exe') +'\n')
self.TA1.insert(INSERT, join(insDir, 'models.json') +'\n')
self.TA1.insert(INSERT, join(insDir, 'image') +'\n')
self.TA1.insert(INSERT, join(insDir, 'uninstall.exe') +'\n')
self.TA1.insert(INSERT, insDir)
self.TA1.config(state='disabled')
self.TA1.place(x=15, y=93)
self.LB2.place(x=15, y=176)
else:
LBText=" TPbuilder has been installed, click Finish to exit."
self.LB2.place(x=15, y=77)
self.LB=Label(self, text=LBText, font='Calibri 10', bg='#FFFFFF')
self.LB.place(x=10, y=37)
self.Spline1=ttk.Separator(self, orient=HORIZONTAL)
self.Spline1.place(x=0, y=67, width=450)
self.Spline2=ttk.Separator(self, orient=HORIZONTAL)
self.Spline2.place(x=0, y=200, width=450)
self.BT2=ttk.Button(self, text='Finish', command=root.quit)
defKeyPress_F(event):
root.quit()
self.BT2.bind("<KeyPress-F>", KeyPress_F)
self.BT2.bind("<KeyPress-f>", KeyPress_F)
self.BT2.place(x=340, y=213)
classWindows(Frame):
def__init__(self, master=None):
ttk.Frame.__init__(self, master)
self.master.title('TPbuilder Setup')
self.master.geometry('450x250+450+300')
self.master.resizable(0, 0)
ifisfile(join(getcwd(), 'install.ico')):
self.master.iconbitmap(default=join(getcwd(), 'install.ico'))
self.createWidgets()
self.master.protocol('WM_DELETE_WINDOW', self.cancel)
defcreateWidgets(self):
globalinsDir
try:
insDir=dirname(Registrar('get', r'HKEY_CURRENT_USER\Software\TPbuilder', 'TBPath'))
except:
insDir=''
ifCheckSetup(insDir):
LBText1="Remove TPbuilder"
LBText2=" Click Next to uninstall TPbuilder or click Cancel to exit."
self.master.LB=Label(self.master, text="TPbuilder installed path: ")
self.master.LB.place(x=15, y=77)
self.master.TF1=ttk.Entry(self.master, width=57)
self.master.TF1.insert(0, insDir)
self.master.TF1.config(state='readonly')
self.master.TF1.place(x=19, y=102)
self.master.BT1=ttk.Button(self.master, text='Next', command=self.exeUninstall)
defKeyPress_N(event):
self.exeUninstall()
self.master.BT1.place(x=240, y=213)
else:
LBText1="Destination Folder"
LBText2=" Click Next to install to the default folder or click Change to choose another."
self.master.LB=Label(self.master, text="Install TPbuilder to: ")
self.master.LB.place(x=15, y=77)
self.master.TF1=ttk.Entry(self.master, width=57)
self.master.TF1.insert(0, 'C:\\Program Files\\TPbuilder')
self.master.TF1.place(x=19, y=102)
self.master.BT0=ttk.Button(self.master, text='Change...', command=self.selectPath)
self.master.BT0.place(x=19, y=130)
self.master.BT1=ttk.Button(self.master, text='Next', command=self.exeInstall)
defKeyPress_N(event):
self.exeInstall()
self.master.BT1.place(x=240, y=213)
self.master.BT1.bind("<KeyPress-N>", KeyPress_N)
self.master.BT1.bind("<KeyPress-n>", KeyPress_N)
self.master.TA=Text(self.master, height=5, width=65, bg='#FFFFFF', cursor='', selectbackground='#FFFFFF')
self.master.TA.config(state='disabled')
self.master.TA.place(x=-5, y=0)
self.master.LB=Label(self.master, text=LBText1, font='Calibri 11 bold', bg='#FFFFFF')
self.master.LB.place(x=10, y=10)
self.master.LB=Label(self.master, text=LBText2, font='Calibri 10', bg='#FFFFFF')
self.master.LB.place(x=10, y=37)
self.master.Spline1=ttk.Separator(self.master, orient=HORIZONTAL)
self.master.Spline1.place(x=0, y=67, width=450)
self.master.Spline2=ttk.Separator(self.master, orient=HORIZONTAL)
self.master.Spline2.place(x=0, y=200, width=450)
self.master.BT2=ttk.Button(self.master, text='Cancel', command=self.cancel)
defKeyPress_C(event):
self.cancel()
self.master.BT2.bind("<KeyPress-C>", KeyPress_C)
self.master.BT2.bind("<KeyPress-c>", KeyPress_C)
self.master.BT2.place(x=340, y=213)
defselectPath(self):
Path=askdirectory(parent=self.master, initialdir='C:\\Program Files\\', title='Select Install Directory').replace('/', '\\')
ifPath:
self.master.TF1.delete(0, 'end')
self.master.TF1.insert(0, join(Path, 'TPbuilder'))
defexeInstall(self):
ifnotisdir(dirname(self.master.TF1.get())):
showerror('ERROR', 'Invalid installation path')
return(False)
ifnotisfile(join(getcwd(), 'package')):
showerror('ERROR', 'Install package lost')
return(False)
globalinstallPath
installPath=self.master.TF1.get()
ifaskyesno('TPbuilder Setup', 'Next will be install TPbuilder?', icon='warning'):
thread_1=MultiProcess('create', 'Install', 0)
thread_2=MultiProcess('progress', 'Install', 0.08)
thread_1.start()
thread_2.start()
defexeUninstall(self):
globalinstallPath
installPath=self.master.TF1.get()
ifaskyesno('TPbuilder Setup', 'Are you sure you want to uninstall it?', icon='warning'):
ifgetProcesses(['TPbuilder'], 0).count('TPbuilder') ==0:
thread_1=MultiProcess('create', 'Uninstall', 0)
thread_2=MultiProcess('progress', 'Uninstall', 0.08)
thread_1.start()
thread_2.start()
else:
showerror('ERROR', 'Please close TPbuilder.exe then try again')
defcancel(self):
ifaskyesno('TPbuilder Setup', 'Are you sure you want to cancel TPbuilder installation?'):
self.master.quit()
if__name__=='__main__':
root=Tk()
app=Windows(master=root)
root.withdraw()
ifgetProcesses(['Setup'], 0).count('Setup') in [0, 2]:
root.deiconify()
app.mainloop()
else:
showerror('ERROR', 'Setup.exe is already executing')