-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
executable file
·239 lines (213 loc) · 7.4 KB
/
updater.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Tkinter import *
from time import strftime,gmtime,sleep
from shutil import move as move_file
from subprocess import *
from threading import Thread
from ttk import Button
from ttk import Scrollbar
import platform, urllib, urllib2, os, zipfile
v = '4'
OS = platform.system()
def readf(filename):
file = filename
try:
f = open(file, 'rU')
except:
savef('',filename)
f = open(file, 'rU')
a = f.read()
a = str.splitlines(a)
f.close()
return a
def savef(text,file):
f = open(file, 'w')
f.write(text)
f.close()
def write_settings(text_find,new_value):
global sys_path
a = readf('load/settings.ini')
a = edit_settings(a,text_find,new_value)
text = a = '\n'.join(str(e) for e in a)
savef(text,'load/settings.ini')
def edit_settings(text,text_find,new_value):
count = 0
newlist = []
for lines in text:
b = lines.find(text_find)
if b is not -1:
c = text_find + '=' + str(new_value)
newlist.append(c)
else:
newlist.append(lines)
count+=1
count = 1
return newlist
def read_settings(*arg):
a = readf('load/settings.ini')
a = get_settings(a,arg[0],arg[1])
return a
def get_settings(text,text_find,default_value):
for lines in text:
b = lines.find(text_find)
if b is not -1:
c = lines[len(text_find):]
try:
if c == '':
c = default_value
write_settings(text_find[:-1],'\n'+c)
except:
c = default_value
fh = open('load/settings.ini', 'a')
fh.write('\n'+str(text_find)+str(c))
fh.close()
return c
def set_winicon(window,name):
global OS
if OS is 'Windows':
try:
window.iconbitmap("load\\"+name+".ico")
except:
print "Couldn't load windows icon"
else:
try:
img = PhotoImage(file='load/'+name+'.png')
window.tk.call('wm', 'iconphoto', window._w, img)
except:
print "Couldn't load Linux icon"
def restart_client():
global OS
INPUT = 'python client.py'
if OS == 'Windows':
if os.path.exists('client.exe') == True:
INPUT = 'client.exe'
Popen([INPUT], shell=True,
stdin=None, stdout=None, stderr=None, close_fds=True)
def download(filelink):
global T
filelink = filelink.rstrip()
try:
b = filelink.find('http://')
if b is -1:
filelink = 'http://'+filelink
name = str(filelink)
filehandle = urllib.urlopen(filelink)
while True:
nametemp = name.find('/')
if nametemp is -1:
break
if nametemp is not -1:
name = name[nametemp+1:]
filer = urllib2.urlopen(filelink)
name = name.rstrip()
output = open(name,'wb')
output.write(filer.read())
output.close()
except Exception as e:
T.config(yscrollcommand=S.set,state="normal")
T.insert(END, str(e)+'\n','redcol')
T.config(yscrollcommand=S.set,state="disabled")
## Tkinter below
root = Tk()
root.title("Updater ver: "+str(v))
root.minsize(280,300)
frame = Frame(root, height=210,width=600, relief=SUNKEN)
frame.pack_propagate(0)
frame.pack(anchor=NE,side=TOP,padx=20,pady=20)
S = Scrollbar(frame)
T = Text(frame, height=46, width=80,wrap=WORD)
S.pack(side=RIGHT, fill=Y)
T.pack(side=BOTTOM,fill=BOTH,expand=1)
S.config(command=T.yview)
T.tag_configure('redcol', foreground='red')
T.tag_configure('blackcol', foreground='black')
T.tag_configure('greencol', background='#c8d9ea',foreground='#009900')
T.tag_configure('failed', background='#c8d9ea',foreground='red')
button = Button(root, text='Reopen client', command=lambda: {restart_client(),root.destroy()})
button.place(x=180,y=250)
button2 = Button(root, text='Close', command=lambda: {root.destroy()})
button2.place(x=360,y=250)
## Create download link list
tlist = []
tfile = readf('load/upd_filelist')
def worker_thread():
global T, tlist
cnt = 0
try:
for x in tlist:
try:
T.config(yscrollcommand=S.set,state="normal")
if x[:8] == 'download':
T.insert(END, 'Downloading '+x[len('download,'):]+'\n','blackcol')
download(x[len('download,'):])
elif x[:8] == 'movefile':
b = x[9:].find(',')
source = x[9:b+9]
target = x[b+10:]
templist = [[source],[target]]
T.insert(END, 'Moving '+source+' to '+target+'\n','blackcol')
move_file(source,target)
elif x[:8] == 'mkfolder':
try:
os.stat(x[9:])
except:
os.mkdir(x[9:])
T.insert(END, 'Making folder - '+x[9:]+'\n','blackcol')
elif x[:7] == 'extract':
T.insert(END, 'Extracting '+x[8:]+'\n','blackcol')
zifi = zipfile.ZipFile(x[8:])
zifi.extractall()
except Exception as e:
e = str(e)
T.insert(END, e+'\n','redcol')
savef('','load/upd_filelist')
T.config(yscrollcommand=S.set,state="normal")
T.insert(END, 'Erased file list\n','blackcol')
T.insert(END, '#Done\n','greencol')
T.config(yscrollcommand=S.set,state="disabled")
except Exception as e:
e = str(e)
T.config(yscrollcommand=S.set,state="normal")
T.insert(END, 'Update failed, worker thread has crashed\n','redcol')
T.insert(END, e+'\n','redcol')
T.insert(END, '#Failed\n','failed')
T.config(yscrollcommand=S.set,state="disabled")
def kill_root():
root.destroy()
def task1():
sleep(1)
global T, tfile, tlist
T.config(yscrollcommand=S.set,state="normal")
try:
if len(tfile) > 0:
for x in tfile:
tlist.append(x)
T.insert(END, 'List loaded\n','blackcol')
root.after(100, task2)
else:
T.insert(END, 'No list, closing in 15 seconds\n','redcol')
root.after(15000, kill_root)
except Exception as e:
T.insert(END, str(e)+'\n','redcol')
T.insert(END, 'Updater has stopped\n','redcol')
T.insert(END, '#Failed\n','failed')
T.config(yscrollcommand=S.set,state="disabled")
def task2():
T.config(yscrollcommand=S.set,state="normal")
T.insert(END, 'Worker thread started\n','blackcol')
T.config(yscrollcommand=S.set,state="disabled")
Thread(target=worker_thread).start()
file_updater_ver = str(read_settings('updater_ver=','1'))
can_update = True
if v != file_updater_ver:
T.config(yscrollcommand=S.set,state="normal")
T.insert(END, 'Updater version does not match the stored value\nTry updating again\n','redcol')
T.insert(END, '#Failed\n','failed')
T.config(yscrollcommand=S.set,state="disabled")
write_settings('updater_ver',v)
can_update = False
set_winicon(root,'icon')
if can_update == True:
root.after(100, task1)
root.mainloop()