-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibupdate.py
475 lines (453 loc) · 16.4 KB
/
libupdate.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
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
import os,sys
sys.path.append("core/include")
sys.path.append("include")
if sys.platform=="win32":
os.environ['SDL_VIDEODRIVER']='windib'
import zlib
from core import pygame
import threading,time,urllib,urllib2
from zipfile import ZipFile,ZIP_DEFLATED
from pwvlib import *
ERROR_STR= """Error removing %(path)s, %(error)s """
ROOT_URL = "http://74.207.230.140/"
ROOT_URL = "http://pywright.dawnsoft.org/"
def rmgeneric(path, __func__):
try:
__func__(path)
print 'Removed ', path
except OSError, (errno, strerror):
print ERROR_STR % {'path' : path, 'error': strerror }
def removeall(path):
if not os.path.isdir(path):
return
files=os.listdir(path)
for x in files:
fullpath=os.path.join(path, x)
if os.path.isfile(fullpath):
f=os.remove
rmgeneric(fullpath, f)
elif os.path.isdir(fullpath):
removeall(fullpath)
f=os.rmdir
rmgeneric(fullpath, f)
import md5
from gui import *
def createfiles(dir="port"):
for f in os.listdir("data/art/"+dir):
if f == ".svn": continue
if not os.path.isdir(f): continue
myzip = ZipFile("zip_"+dir+"/"+f+".zip","w",ZIP_DEFLATED)
for sub in os.listdir("data/art/"+dir+"/"+f):
if sub == ".svn": continue
myzip.write("data/art/"+dir+"/"+f+"/"+sub,sub)
myzip.close()
print "wrote","zips/"+f+".zip"
def create_path(dir):
pathfull = ""
for pathpart in dir.split("/"):
pathfull+=pathpart+"/"
if not os.path.exists(pathfull):
os.mkdir(pathfull)
for required_path in ["data/art/3d","data/art/bg","data/art/ev","data/art/fg","data/art/general","data/art/port","data/music","games","fonts","data/sfx"]:
create_path(required_path)
def mynames(dir="data/art/port"):
files = {}
for file in [x for x in os.listdir(dir) if x != ".svn"]:
files[file] = get_data_from_folder(dir+"/"+file)
return files
import cStringIO
iconcache = {}
def load_image(path):
if path not in iconcache:
f = urllib2.urlopen(path.replace(" ","%20"))
txt = f.read()
f.close()
f = cStringIO.StringIO(txt)
icon = pygame.image.load(f,path)
iconcache[path] = icon
return iconcache[path]
class my_o_dict(dict):
pass
def names(url):
url = url.replace("(","%28").replace(")","%29").replace(" ","%20")
if 1:#try:
f = urllib2.urlopen(ROOT_URL+url)
else:#except:
print "fail"
return {}
txt = f.read()
lines = eval(txt)
f.close()
files = my_o_dict()
files.okeys = []
for x in lines:
if x["zipname"] in files:
if compare_versions(x["version"],files[x["zipname"]]["version"])<0:
continue
else:
files.okeys.append(x["zipname"])
files[x["zipname"]]=x
return files
def build_list(e,dir="data/art/port",url="zip_port_info",check_folder=None):
list = e.list
list.pane.children = [list.status_box]
fnd = 0
list.status_box.text="Scanning local files..."
mn = mynames(dir)
list.status_box.text="Fetching data from server..."
an = names(url)
if check_folder:
d = get_data_from_folder(check_folder)
mn = {}
for n in an:
mn[n] = d
cases = {"NEW":[],"UPDATED":[],"INSTALLED":[]}
for n in an.okeys:
if n not in mn:
status = "NEW"
elif compare_versions(an[n]["version"],mn[n]["version"])>0:
status = "UPDATED"
else:
status = "INSTALLED"
fnd = 1
cb = checkbox(an[n].get("title",an[n]["zipname"]))
cb.name = n
cb.file = an[n]["zipfile"]
cb.filename = an[n]["zipname"]
p = pane([0,0])
p.width,p.height = [380,95]
p.align = "horiz"
image_b = button(None,"")
image_b.background = False
image_b.border = False
image_b.click_down_over = cb.click_down_over
def load_icon_this(url=an[n]["iconurl"],but=image_b):
try:
image = load_image(url)
except:
image = None
but.graphic = image
threading.Thread(target=load_icon_this).start()
p.add_child(image_b)
stats = pane([0,0])
stats.width,stats.height = [340,93]
stats.align = "vert"
stats.background = False
stats.border = False
stats.add_child(cb)
sline = status
if an[n].get("author",""):
sline += " "+"by "+an[n]["author"]
stats.add_child(label(sline))
stats.date = 0
if an[n].get("version_date",""):
stats.date = an[n]["version"]
stats.add_child(label("ver %s updated on %s"%(cver_s(an[n]["version"]),an[n]["version_date"])))
if an[n].get("website",""):
url = an[n]["website"]
urlb = button(None,url)
urlb.textcolor = [0,0,255]
try:
import webbrowser
setattr(urlb,url,lambda *args: webbrowser.open(url))
except ImportError:
pass
stats.add_child(urlb)
p.add_child(stats)
p.bgcolor = {"NEW":[255,200,200],"UPDATED":[200,255,200],"INSTALLED":[255,255,255]}[status]
p.date = stats.date
cases[status].append(p)
for s in ["UPDATED","NEW","INSTALLED"]:
for n in reversed(sorted(cases[s],key=lambda el:el.date)):
list.add_child(n)
if dir == ".":
dir = "updates"
if not fnd:
list.status_box.text = "No "+dir+" are available to download"
else:
list.status_box.text = "Download "+dir+"! Click check boxes to select."
import libengine
get_url = "updates3/games.cgi?content_type=%s&ver_type=tuple&fullurl=true&version="+str(libengine.__version__)
print get_url
class Engine(pane):
mode = "port"
quit_threads = 0
num_threads = 0
def __init__(self,screen):
super(Engine,self).__init__()
self.align = None
self.root = self
self.z = 2000000
self.pri = -2000000
self.root.width,self.root.height = [SCREEN_WIDTH,SCREEN_HEIGHT]
self.list = scrollpane([0,0])
self.list.rpos[1]=15
self.list.width,self.list.height = [SCREEN_WIDTH,140]
self.list.status_box = editbox(None,"")
self.list.status_box.draw_back = False
self.list.status_box.draw(screen)
self.root.add_child(self.list)
def Download_X(self,mode,path,url,check_folder=None):
def t():
self.mode = mode
self.path = path
self.url = url
print "try download",self.url
build_list(self,path,url,check_folder)
rpos = self.root.children[self.root.start_index].rpos
self.root.children[self.root.start_index] = button(self,"download")
self.root.children[self.root.start_index].rpos = rpos
threading.Thread(target=t).start()
def Characters(self):
self.Download_X("port","data/art/port",get_url%("port",))
def Backgrounds(self):
self.Download_X("bg","data/art/bg",get_url%("bg",))
def Foreground(self):
self.Download_X("fg","data/art/fg",get_url%("fg",))
def Games(self):
self.Download_X("games","games",get_url%("games",))
def Music(self):
self.Download_X("music","data/music",get_url%("music",))
def Update_PyWright(self,thread=True):
self.path = "."
self.Download_X("engine",".",get_url%("engine",),check_folder=".")
def get_download_in_progress(self,check):
path,filename,url,seek = self.path,check.filename,check.file,False
if os.path.exists("downloads/"+check.filename+"_url") and os.path.exists("downloads/"+check.filename):
try:
path,filename,url = open("downloads/"+check.filename+"_url","r").read().split("\t")
seek = True
except:
pass
return path,filename,url,seek
def do_downloads(self,checkfolder=True,output=None):
print "doing downloads"
for x in self.list.pane.children[1:]:
check = x.children[1].children[0]
if check.checked:
path,filename,url,seek = self.get_download_in_progress(check)
self.download_file(path,filename,url,output,seek)
def make_download_folders(self):
if not os.path.exists("downloads"):
os.mkdir("downloads")
def download_file(self,path,filename,url,output=None,seek=False):
self.num_threads += 1
self.make_download_folders()
if not hasattr(self,"progress"):
self.progress = progress()
self.root.add_child(self.progress)
self.progress.height = 20
self.progress.width = 400
self.progress.rpos[1] = self.list.rpos[1]+self.list.height+20
self.progress.progress = 0
headers = {"User-Agent":"pywright downloader"}
size = None
print "download with seek",seek,url
if seek:
try:
f = open("downloads/"+filename,"rb")
old = f.read()
f.close()
cli = open("downloads/"+filename,"w")
cli.write(old)
seek = len(old)
print "seeked",seek,"bytes"
serv = urllib2.urlopen(url.replace(" ","%20"))
size = int(serv.info()["Content-Length"])
if seek>size:
print "resetting download"
seek = 0
os.remove("downloads/"+filename+"_url")
cli = open("downloads/"+filename,"w")
headers["Range"] = "bytes=%d-%d"%(seek,size)
serv.close()
print "headers:",headers
except:
import traceback
traceback.print_exc()
seek = 0
if not seek:
seek = 0
cli = open("downloads/"+filename,"wb")
print "opened new file"
req = urllib2.Request(url.replace(" ","%20"),None,headers)
try:
serv = urllib2.urlopen(req)
print "opened resume"
except:
seek = 0
serv = urllib2.urlopen(url)
print "opened new"
if not size:
size = int(serv.info()["Content-Length"])
print "size of document:",size
read = seek
bytes = seek
prog = open("downloads/"+filename+"_url","w")
prog.write(path+"\t"+filename+"\t"+url)
prog.close()
f = open("downloads/last","w")
f.write(path+" "+filename)
f.close()
s = time.time()
bps = 0
while not Engine.quit_threads:
r = serv.read(4096)
if not r: break
cli.write(r)
cli.flush()
read += len(r)
bytes += len(r)
self.progress.progress = read/float(size)
print self.progress.progress
if time.time()-s>1:
bps = bytes/(time.time()-s)
s = time.time()
bytes = 0
self.progress.text = "%sKB/%sKB - %s KB/s"%(read/1000.0,size/1000.0,bps/1000.0)
if output:
self.progress.rpos = [0,0]
self.progress.width = SCREEN_WIDTH
self.progress.draw(output[0])
output[1]()
for evt in pygame.event.get():
if evt.type == pygame.QUIT: raise SystemExit
print "closing server"
serv.close()
print "closing download file"
cli.close()
print "extract zip?"
if read==size:
self.progress.text = self.extract_zip(path,filename)
print "delete progress bar"
del self.progress
print "switch modes"
if self.mode == "games":
self.Games()
self.num_threads -= 1
def extract_zip(self,todir,filename):
try:
z = ZipFile("downloads/"+filename,"r")
except:
import traceback
traceback.print_exc()
return "Corrupt"
if self.mode == "engine":
root = "./"
block = None
#Extract folder from zip to todir
elif filename+"/" in z.namelist():
root = todir+"/"
block = filename+"/"
#Create folder from filename, extract contents of zip to there
else:
root = todir+"/"+filename+"/"
try:
os.makedirs(root)
except:
pass
block = None
for name in z.namelist():
if hasattr(self,"progress"):
self.progress.text = "extracting:"+name
print "extract:",name
try:
txt = z.read(name)
except:
return "Corrupt download"
if block:
if not name.startswith(block):
continue
if "/" in name and not os.path.exists(root+name.rsplit("/",1)[0]):
os.makedirs(root+name.rsplit("/",1)[0])
if not name.endswith("/"):
f = open(root+name,"wb")
f.write(txt)
f.close()
z.close()
os.remove("downloads/"+filename)
try:
os.remove("downloads/last")
except:
pass
if self.mode == "engine":
self.root.children[:] = [editbox(None,"In order to complete upgrade you must restart.")]
self.need_restart = True
return "FINISHED"
def download(self):
t = threading.Thread(target=self.do_downloads)
t.start()
def Cancel(self,*args):
Engine.quit_threads = True
self.running = False
self.kill = 1
def run(screen):
e = Engine(screen)
e.running = True
start = button(e,"download")
start.rpos[1] = e.list.rpos[1]+e.list.height
end = button(e,"Cancel")
end.draw(screen)
end.rpos = [SCREEN_WIDTH-end.width,0]
e.root.add_child(start)
e.root.add_child(end)
e.root.start_index = e.root.children.index(start)
#~ char_b = button(e,"Characters")
#~ #char_b.rpos[0]=label.rpos[0]+label.width
#~ char_b.rpos[1]=20
#~ char_b.draw(screen)
#~ e.root.add_child(char_b)
#~ bg_b = button(e,"Backgrounds")
#~ bg_b.rpos[0]=char_b.rpos[0]+char_b.width+5
#~ bg_b.rpos[1]=20
#~ bg_b.draw(screen)
#~ e.root.add_child(bg_b)
#~ fg_b = button(e,"Foreground")
#~ fg_b.rpos[0]=bg_b.rpos[0]+bg_b.width+5
#~ fg_b.rpos[1]=20
#~ fg_b.draw(screen)
#~ e.root.add_child(fg_b)
game_b = button(e,"Games")
game_b.rpos[0]=0
game_b.rpos[1]=0
game_b.draw(screen)
e.root.add_child(game_b)
music_b = button(e,"Music")
#~ music_b.rpos[0]=game_b.rpos[0]+game_b.width+5
#~ music_b.rpos[1]=40
#~ music_b.draw(screen)
#~ e.root.add_child(music_b)
#~ info_label = editbox(None, "Hold shift to select multiple items in a category")
#~ e.root.add_child(info_label)
#~ info_label.draw_back = False
#~ info_label.rpos[0]=0
#~ info_label.rpos[1]=60
pwup_b = button(e,"Update PyWright")
pwup_b.rpos[0]=game_b.rpos[1]+game_b.width+1
pwup_b.rpos[1]=0
pwup_b.draw(screen)
e.root.add_child(pwup_b)
if os.path.exists("downloads/last"):
try:
last_path,last_dl = open("downloads/last","r").read().split(" ")
e.extract_zip(last_path,last_dl)
except:
os.remove("downloads/last")
return e
#~ clock = pygame.time.Clock()
#~ while e.running:
#~ mp = pygame.mouse.get_pos()
#~ clock.tick(60)
#~ screen.fill([225,225,225])
#~ e.root.draw(screen)
#~ pygame.display.flip()
#~ pygame.event.pump()
#~ quit = e.root.handle_events(pygame.event.get())
#~ if quit:
#~ Engine.quit_threads = True
#~ e.running = False
#~ if getattr(e,"need_restart",False):
#~ sys.exit()
#~ while Engine.num_threads:
#~ print Engine.num_threads
#~ pass