/
WurmCommon.py
1328 lines (1019 loc) · 45 KB
/
WurmCommon.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# $Id: WurmCommon.py 663 2009-06-01 18:43:01Z jonhogg $
# Copyright (c) 2006-2009 The WUU Development Team
#
# This software is provided 'as-is', without any express or implied warranty. In no event will the
# authors be held liable for any damages arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose, including commercial applications,
# and to alter it and redistribute it freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not claim that you
# wrote the original software. If you use this software in a product, an acknowledgment
# in the product documentation would be appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source distribution.
# Purpose of this module:
# - Gather functions used by WUU, Wurm and WurmUnpack, to prevent duplicating/unsyncing of data
# - Gather all global variables used by the Wurm* modules
import logging
STATUS = logging.DEBUG - 1 # toggle these two lines to see the status messages
# STATUS = logging.INFO - 1 #
DEBUG3 = logging.DEBUG - 3
DEBUG5 = logging.DEBUG - 5
DEBUG7 = logging.DEBUG - 7
logging.addLevelName(STATUS, "STATUS")
logging.addLevelName(DEBUG3, "DEBUG_3")
logging.addLevelName(DEBUG5, "DEBUG_5")
logging.addLevelName(DEBUG7, "DEBUG_7")
logger = logging.getLogger(None)
tracer = logging.getLogger("WUUtrace")
import os, sys, shutil, re, gzip, platform
import urllib2, urlparse, socket
from datetime import datetime, timedelta
import xml.dom.minidom
import threading
import codecs
try:
from cStringIO import StringIO
except ImportError:
import StringIO
import WurmLanguage
# Treat UnicodeWarning's as an error if running Python 2.5 or higher
if map(int, platform.python_version().split(".")[:2]) >= [2, 5]:
import warnings
# Warnings Filter
warnings.simplefilter("error", UnicodeWarning) # turn UnicodeWarning into an error
else:
UnicodeWarning = UnicodeDecodeError
# remap dummy translation function to real trans func
_ = WurmLanguage.s
# define the Application, Support & Preferences directories
origappldir = appldir = supportdir = prefsdir = os.path.split(os.path.realpath(sys.argv[0]))[0]
# If on Windows then use the preferred directories for Preference files etc
if sys.platform[:3] == "win":
try:
userdir = os.getenv("USERPROFILE")
supportdir = prefsdir = os.path.join(userdir, "WUU")
# create these directories if they don't exist
if not os.path.exists(supportdir):
os.makedirs(supportdir)
if not os.path.exists(prefsdir):
os.makedirs(prefsdir)
except Exception, details:
print "Unable to create Windows directories: %s" % str(details)
raise
# If on a Mac then use the preferred directories for Preference files etc
elif sys.platform == "darwin":
# if running from an .app then relocate the appldir variable to the directory the .app is in
if "/WUU.app" in appldir:
appldir = appldir[:appldir.find("/WUU.app")]
try:
userdir = os.getenv("HOME")
supportdir = userdir + "/Library/Application Support/WUU"
prefsdir = userdir + "/Library/Preferences/WUU"
# create these directories if they don't exist
if not os.path.exists(supportdir):
os.makedirs(supportdir)
if not os.path.exists(prefsdir):
os.makedirs(prefsdir)
except Exception, details:
print "Unable to create Mac directories: %s" % str(details)
raise
# print "Application directory: " + appldir
# print "Support directory: " + supportdir
# print "Preferences directory: " + prefsdir
# Locks for Addons sites
wurmlock = {}
# Gather all directory names in a single dictionary instead of separate variables
directories = {"wow":"", "addons":"", "backup":"", "temp":"", "unpack":""}
globalsettings = {} # Global settings for Wurm (so options may be set from the UI)
siteregexps = {} # Regexps used by the addon site classes
listchanged = {} # Entries for each list are set to True if the addon list was changed indirectly (that is, when the UI should give a "Do you want to save?"-popup when quitting)
# define working dictionaries
addons = {}
addonlist = None
addontypes = {}
toclist = {}
depdict = {}
deplist = []
needdepslist = [] # List of missing dependencies; filled by different routines, checked by UI now and then (TODO)
needdepsmap = {} # Missing dependency -> list of addons using it
unusedliblist = [] # List of addons marked as libraries that no addon depends on; might not be safe to remove
# Message output and callbacks
cbOutMessage = None
cbOutDebug = None
cbOutWarning = None
cbOutError = None
cbOutStatus = None
cbProgressPercent = None
cbResetProgress = None
cbProgressPercent2 = None
cbResetProgress2 = None
Wurm = None
# Filenames etc
langdir = os.path.join(origappldir, "lang") # where the lang-XX.xml files are stored
refile = "site_re.txt"
refilename = os.path.join(supportdir, refile) # regexps for the different sites
clreportfile = os.path.join(supportdir, "WUU-UpdateReport.html") # Changelog report file
depreportfile = os.path.join(supportdir, "WUU-DependencyReport.html") # Dependency report file
templatefile = "templates.xml"
templatefilename = os.path.join(supportdir, templatefile) # Templates
wuusite = "http://wuu.vagabonds.info/"
wuudownload = wuusite + "downloads.php"
wuuki = wuusite + "wuuki/"
wuusf = "http://sourceforge.net/projects/wuu"
reurl = wuusite + refile
templateurl = wuusite + templatefile
cosmosaddonslist = "http://www.cosmosui.org/addons.php"
newaddon = u">>NewAddon<<"
useragent = "WUU" # Useragent to use for web requests (set more specifically by WUU)
# Class Specific website availability
isAvailable = {}
# these are the keys used for the Templates
templateKeys = [
"AddonPage",
"VerRegExp",
"VerIsDate",
"DateFormat",
"VerIsLast",
"ExtnRegExp",
"ExtnIsName",
"DLNameExp"
]
# OtherSites templates
ostemplates = {}
ostKeys = []
# Child templates
childtemplates = {}
childtKeys = []
pagecache = {}
class AddonList(dict):
"""This is used to manage a list of addons"""
def __init__(self):
""""""
tracer.debug("AddonList - __init__")
# store object's id
self._id = id(self)
dict.__init__(self)
self._fname = 0
self._lname = 1
self._siteid = 2
self._atype = 3
self._flags = 4
def add(self, aname, fname=None, lname=None, siteid=None, atype=None, flags=None):
"""add a new addon entry into the dictionary"""
tracer.log(DEBUG5, "AddonList - add")
global listchanged
if fname is None:
fname = aname
if lname is None:
lname = aname
if siteid is None:
siteid = aname
if atype is None:
atype = "[Unknown]"
if flags is None:
flags = {}
self[aname] = (fname, lname, siteid, atype, flags)
# Set list changed indicator
listchanged[self._id] = True
def delete(self, aname):
"""delete an addon entry from the dictionary"""
tracer.log(DEBUG5, "AddonList - delete")
global listchanged
del self[aname]
# Set list changed indicator
listchanged[self._id] = True
def getSome(self, aname):
"""get some of the addon info from the dictionary"""
tracer.log(DEBUG5, "AddonList - getSome: %s" % (aname))
return (self.getFname(aname), self.getLname(aname), self.getSiteid(aname), self.getAtype(aname))
def getAll(self, aname):
"""get all the addon info from the dictionary"""
tracer.log(DEBUG5, "AddonList - getAll: %s" % (aname))
return (self.getFname(aname), self.getLname(aname), self.getSiteid(aname), self.getAtype(aname), self.getAllFlags(aname))
def getFname(self, aname):
"""get the addon's friendlyname from the dictionary"""
tracer.log(DEBUG5, "AddonList - getFname")
try:
return self[aname][self._fname]
except KeyError:
return None
def getLname(self, aname):
"""get the addon's localname from the dictionary"""
tracer.log(DEBUG5, "AddonList - getLname")
try:
return self[aname][self._lname]
except KeyError:
return None
def getSiteid(self, aname):
"""get the addon's siteid from the dictionary"""
tracer.log(DEBUG5, "AddonList - getSiteid")
try:
return self[aname][self._siteid]
except KeyError:
return None
def getAtype(self, aname):
"""get the addon's addontype from the dictionary"""
tracer.log(DEBUG5, "AddonList - getAtype")
try:
return self[aname][self._atype]
except KeyError:
return None
def getAllFlags(self, aname):
"""get the addon's flags from the dictionary"""
tracer.log(DEBUG5, "AddonList - getAllFlags: %s" % (aname))
try:
return self[aname][self._flags]
except KeyError:
return None
def getFlag(self, aname, flag):
"""get the addon's flags from the dictionary"""
tracer.log(DEBUG5, "AddonList - getFlag: %s, %s" % (aname, flag))
try:
return self[aname][self._flags][flag]
except KeyError:
return None
def addFlag(self, aname, flag, value):
"""add a flag to the addon's flags in the dictionary"""
tracer.log(DEBUG5, "AddonList - addFlag: %s, %s, %s" % (aname, flag, value))
self[aname][self._flags][flag] = value
def delFlag(self, aname, flag):
"""delete a flag from the addon's flags in the dictionary"""
tracer.log(DEBUG5, "AddonList - delFlag: %s, %s" % (aname, flag))
if flag in self[aname][self._flags]:
del self[aname][self._flags][flag]
class PageCacheEntry:
""" Simple time-based in-memory caching of web pages """
def __init__(self, url, content, timeout=600): # default timeout 10 minutes
self.url = url
self.content = content
self.expires = datetime.now() + timedelta(seconds=timeout)
def isValid(self):
return (self.expires >= datetime.now())
def _deleteAddon(addon):
""""""
tracer.debug("_deleteAddon: %s" % (addon))
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon1"].acquire()
outDebug("Ok, deleting %s" % (addon))
# Moving in for the kill
try:
try:
_deletePathRecursive(os.path.join(directories["addons"], addon))
except Exception, details:
outDebug("Couldn't remove %s: %s" % (addon, unicode(details)))
raise
finally:
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon1"].release()
return True
def _deleteBackup(addon):
""""""
tracer.debug("_deleteBackup: %s" % (addon))
global directories
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon1"].acquire()
# Moving in for the kill
try:
try:
if len(directories["backup"]) > 5 and os.path.exists(directories["backup"]): # Minor sanity check
_deletePathRecursive(os.path.join(directories["backup"], addon))
except Exception, details:
msg = "Error removing backup of %s: %s" % (addon, str(details))
logger.exception(msg)
outError(msg)
finally:
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon1"].release()
def _deletePathRecursive(path, deleteSelf=True):
""" Deletes all contained files and subfolders of "path", and, if deleteSelf is set, "path" itself too - returns the count of files and folders deleted, as a tuple """
tracer.debug("_deletePathRecursive: %s" % (path))
filecount = dircount = 0
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
filecount += 1
for name in dirs:
os.rmdir(os.path.join(root, name))
dircount += 1
if deleteSelf:
os.rmdir(path)
dircount += 1
return (filecount, dircount)
def addCachedPage(url, content, timeout=600):
""" Adds a page to the cache """
global pagecache
pagecache[url] = PageCacheEntry(url, content, timeout)
def cacheSize():
""" Returns a tuple of (count, total size) of the pages currently in the cache, in bytes """
total = 0
for url in pagecache:
total += len(pagecache[url].content)
return (len(pagecache), total)
def checkDeps(depReqd):
""" check to see if the specified dependecy exists """
tracer.debug("checkDeps: %s" % (depReqd))
global addonlist, deplist
if deplist == []:
deplist = depdict.keys()
# already in the addonlist
if depReqd in addonlist:
# not flagged as Unknown
if not addonlist.getAtype(depReqd) == "[Unknown]":
return True
# already in the dependency list
elif depReqd in deplist or depReqd.lower() in deplist:
return True
# As most dependencies don't match actual Addon lua filenames
# try to map them to actual Addons that we know about
# Ace2
elif depReqd == "Ace2":
for libReqd in ("AceLibrary", "AceAddon", "AceEvent", "AceDB", "AceConsole", "AceHook"):
depFound = False
for depname in deplist:
if libReqd == depname[:len(libReqd)]:
depFound = True
continue
if not depFound:
return False
return True
# Ace3 Libraries
elif (depReqd.startswith("Ace") and depReqd.endswith("-3.0")) or depReqd == "CallbackHandler-1.0":
if "Ace3" in deplist:
return True
return False
# handle mis-spelt Fubar
elif depReqd == "Fubar":
if "FuBar" in deplist:
return True
return False
# DewdropLib
elif depReqd == "Dewdrop-2.0":
if "DewdropLib" in deplist:
return True
return False
# TabletLib
elif depReqd == "Tablet-2.0":
if "TabletLib" in deplist:
return True
return False
# Does it start with SpecialEvents?
elif depReqd.startswith("SpecialEvents"):
libReqd = depReqd[:13]
for depname in deplist:
if libReqd == depname[:len(libReqd)]:
return True
return False
# Does it end in Lib?
elif depReqd.lower().endswith("lib"):
libReqd = depReqd[:-3]
for depname in deplist:
if libReqd == depname[:len(libReqd)]:
return True
return False
# Does it exist without a version number?
elif not checkDeps.verno_re.search(depReqd):
for depname in deplist:
if depReqd in addonlist or depReqd == depname[:len(depReqd)]:
return True
return False
# dependency check regular expression
checkDeps.verno_re = re.compile("-\d\.\d")
def checkWebsite(url):
""" check to see if the specified website site is available """
tracer.debug("checkWebsite: %s" % (url))
req = urllib2.Request(url)
response = None
try:
try:
req.add_header('User-Agent', useragent)
response = urllib2.urlopen(req)
except urllib2.URLError, details:
if hasattr(details, "reason"):
outWarning(_("We failed to reach %(url)s, Reason: %(dets)s") % {'url': url, 'dets': details.reason})
elif hasattr(details, "code"):
outWarning(_("%(url)s couldn\'t fulfill the request, Error code: %(dets)s") % {'url': url, 'dets': details.code})
isAvailable["WUUsite"] = False
else:
isAvailable["WUUsite"] = True
finally:
if response:
response.close()
del response
def check4SCMClient(scmType):
# Check to see if the SCM client exists somewhere
scmClient = None
hasClient = False
if scmType == 'SVN':
scmClient = 'svn'
elif scmType == 'Git':
scmClient = 'git'
if not scmClient:
return False, None
# Define the SCM executable name
if os.name == 'nt':
scmClient = scmClient + '.exe'
for path in os.getenv('PATH').split(os.pathsep) + ['./']: # have to use pathsep, since Windows uses ';', not ':'
if os.path.isfile(os.path.join(path, scmClient)):
hasClient = True
break
del path
# Handle scmClient not on the Path on a Mac
# try the default directory
if not hasClient and sys.platform == 'darwin':
if os.path.isfile(os.path.join('/usr/local/bin', scmClient)):
scmClient = os.path.join('/usr/local/bin', scmClient)
hasClient = True
elif os.path.isfile(os.path.join('/opt/local/bin', scmClient)):
scmClient = os.path.join('/opt/local/bin', scmClient)
hasClient = True
# Test default installation directory on Windows
if not hasClient and os.name == 'nt':
if scmType == 'SVN':
windir = 'Subversion'
elif scmType == 'Git':
windir = 'Git'
defscmpath = os.path.join(os.getenv('PROGRAMFILES'), windir, 'bin', scmClient)
if os.path.isfile(defscmpath):
scmClient = defscmpath
hasClient = True
del windir, defscmpath
if not hasClient:
scmClient = None
return hasClient, scmClient
def clearCachePages(expiredonly = False):
""" Clears either only the expired, or all pages from the cache """
global pagecache
size = cacheSize()
outMessage(_("Cache size before flush: %(size)dKb in %(count)d pages") % {'count': size[0], 'size':size[1]/1024} )
if not expiredonly:
pagecache = {}
else:
todelete = []
for url in pagecache:
if not pagecache[url].isValid():
todelete.append(url)
for url in todelete:
del pagecache[url]
size = cacheSize()
outMessage(_("Cache size after flush: %(size)dKb in %(count)d pages") % {'count': size[0], 'size':size[1]/1024} )
def downloadFile(url, localfile, shorten=False, allowcoral=True):
""" File download that reports its progress """
tracer.debug("downloadFile: %s, %s" % (url, localfile))
global useragent
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon2"].acquire()
if int(getGlobalSetting(":UseCoralCDN")) == 1 and allowcoral: # allowcoral is added so we can force certain urls to direct download
# Rewrite the URL to use the Coral Content Distribution Network
o = urlparse.urlparse(url)
newurl = [x for x in o]
newhost = newurl[1]
if ':' in newhost:
newhost = newhost.replace(':', '.')
newurl[1] = "%s.nyud.net:8080" % (newhost)
url = urlparse.urlunparse(newurl)
logger.log(DEBUG5, "Changed URL to %s" % (url))
showname = url
# shorten the url so that it displays in the progress dialog, if required
if shorten:
showname = "..." + url[len(url) - 20:]
try:
try:
blocksize = 2048 # 2kb blocks
target = open(localfile, "wb")
request = urllib2.Request(url)
request.add_header('User-Agent', useragent)
source = urllib2.urlopen(request)
size = 0.0
try:
size = float(source.info()['Content-Length'])
except:
pass # ignore size error
data = "dummy"
progress = 0
while len(data) > 0:
data = source.read(blocksize)
target.write(data)
progress += len(data)
if size > 0:
outProgressPercent(_("Downloading %(sname)s") % {'sname': showname}, progress / size)
else:
# Default when size is not known is to just let the progressbar run backwards
outProgressPercent(_("Downloading %(sname)s") % {'sname': showname}, (10 - (int(progress / blocksize) % 10)) / 10)
outProgressPercent(_("Download of %(sname)s complete!") % {'sname': showname}, 1)
target.close()
source.close()
except Exception, details:
logger.exception("Error downloading %s: %s" % (url, str(details)))
if os.path.exists(localfile):
os.remove(localfile) # remove file if it exists
raise
finally:
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon2"].release()
resetProgress()
outDebug("%(url)s downloaded to %(lfile)s" % {'url': url, 'lfile': localfile})
def downloadPage(url, opener=urllib2.build_opener(urllib2.HTTPHandler), compress=True, forcereload=False, length=307200):
""" Returns the content from a webpage, but tries to use gzip encoding if the server supports it (unless explicitly told otherwise)
compress = False would disable gzip encoding
forcereload = True bypasses the cache
length = 300 Kb
"""
tracer.debug("downloadPage: %s, %d" % (url, length))
global useragent
allowcache = (int(getGlobalSetting(":WebCache", 1)) == 1) and not forcereload # Follow both global setting and parameter
if allowcache: # Try the cache first
content = getCachedPage(url)
if content:
return content
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon2"].acquire()
try:
try:
request = urllib2.Request(url)
request.add_header('User-Agent', useragent)
if compress:
request.add_header('Accept-Encoding', 'gzip')
f = opener.open(request)
data = None
# Handle potentially gzipped result
if f.headers.get('Content-Encoding', '') == 'gzip':
gzdata = f.read()
# Extract gzipped data
gzstream = StringIO(gzdata)
gzf = gzip.GzipFile(fileobj=gzstream)
data = gzf.read()
gzf.close()
del gzdata
del gzstream
else:
data = f.read(length)
f.close()
except Exception, details:
logger.exception("Error downloading %s: %s" % (url, str(details)))
raise
finally:
if threading.currentThread().getName() != "MainThread":
wurmlock["WurmCommon2"].release()
if allowcache:
addCachedPage(url, data)
logger.log(DEBUG5, "Addon webpage: %s, %d" % (url, len(data)))
return data
def findAllChildren(addonname):
""" Returns a list of all addons who are the child of this Addon """
tracer.debug("findAllChildren")
children = []
for a in addonlist:
if addonlist.getAtype(a) == "Child" and addonlist.getFlag(a, "Parent") == addonname:
children.append(a)
return children
def findAllRelated(addonname, stopiteration=3):
""" Returns a list of all addons marked [Related] with "addonname" as siteid.
It calls itself for each related Addon in case they have relations
(e.g. Babble, PeriodicTable & SpecialEvents), unless stopiteration is 0.
Default is to iterate 3 times.
"""
tracer.log(DEBUG5, "findAllRelated")
related = []
try:
for a in addonlist:
if addonlist.getAtype(a) == "[Related]" and addonlist.getSiteid(a) == addonname:
related.append(a)
if stopiteration > 0:
related += findAllRelated(a, stopiteration - 1)
except UnicodeWarning, details:
msg = "UnicodeError: [%s], %s" % (addonname, str(details))
logger.exception(msg)
outError(msg)
return related
def findDuplicateSiteIDs():
""" Scans all addons, and returns addons with the same site/siteID combination -
any such duplicates will result in multiple downloads of the same .zip, which is wrong
(usually the result of errors in the online database) """
tracer.log(DEBUG5, "findDuplicateSiteIDs")
duplicates = {}
seensiteids = {} # storing site|siteid: addon here, to quickly check if it's seen before
try:
for a in addonlist:
if addonlist.getAtype(a)[0] != "[":
sitesid = "%s|%s" % (addonlist.getAtype(a), addonlist.getSiteid(a))
if sitesid in duplicates:
duplicates[sitesid].append(a)
elif sitesid in seensiteids:
duplicates[sitesid] = [a, seensiteids[sitesid],]
else:
seensiteids[sitesid] = a
except UnicodeWarning, details:
msg = "UnicodeError: [%s], %s" % (addonname, str(details))
logger.exception(msg)
outError(msg)
if len(duplicates) > 0:
outMessage(_("%(duplicatecount)d site IDs are duplicated") % {'duplicatecount': len(duplicates)})
return duplicates
def getCachedPage(url):
""" Returns a cached page if available/still valid, otherwise None """
global pagecache
if url in pagecache:
if pagecache[url].isValid():
return pagecache[url].content
else:
del pagecache[url]
return None
def getDirectory(dirname, default=False):
""" Fetches a directory from the dictionary, if it is set, otherwise returns the default """
tracer.debug("getDirectory: %s" % (dirname))
global directories
if directories.has_key(dirname):
return directories["dirname"]
else:
return default
def getGlobalSetting(setting, default=False):
""" Gets a global setting, or sets it to the specified default if it doesn't exist """
tracer.log(DEBUG5, "getGlobalSetting: %s" % (setting))
global globalsettings
if globalsettings.has_key(setting):
return globalsettings[setting]
else:
globalsettings[setting] = default
return default
def getListVersionFromFile(filename):
""" Gets the version from the file - it's kinda "cheating", since the strings are directly comparable to check if a version is newer - no conversion necessary. """
tracer.debug("getListVersionFromFile")
return open(filename, "rt").read()
def loadLanguages():
""" Loads all languages in the <WUU dir>/lang folder """
tracer.debug("loadLanguages")
global langdir
if os.path.exists(langdir):
outMessage("Loading languages from %s" % (langdir))
for f in os.listdir(langdir):
if "lang" in f and ".xml" in f: # TODO: Better check
# outDebug("Loading language file %s" % (f))
try:
WurmLanguage.loadLanguageFile(os.path.join(langdir, f))
except Exception, details:
outWarning("Could not load entire language from %s (Reason: %s)" % (f, str(details)))
outMessage("Available languages: %s" % (", ".join(WurmLanguage.getLanguages().keys())))
def loadRegexps():
""" Loads the site regexps from file """
tracer.debug("loadRegexps")
global refilename, reurl, siteregexps
if not os.path.exists(refilename): # grab it from the web server if it's not present
downloadFile(reurl, refilename)
relines = open(refilename, "rt").readlines()
for line in relines:
re = line.strip().split("\t")
if len(re) >= 2:
siteregexps[re[0]] = re[1]
tracer.debug("Loaded regexp %s" % (re[0]))
def loadTemplates():
"""Process the templates file"""
tracer.debug("WurmCommon - loadTemplates")
global ostemplates, childtemplates, ostKeys, childtKeys
if not os.path.exists(templatefilename): # grab it from the web server if it's not present
try:
downloadFile(templateurl, templatefilename)
except:
pass # ignore download error
if not os.path.exists(templatefilename): # if it's still not there display warning and leave
outWarning(_("Templates file, %s, is unavailable") % templatefilename)
return
try:
dom = xml.dom.minidom.parse(templatefilename)
modelslist = dom.getElementsByTagName("templates")[0]
osmodels = modelslist.getElementsByTagName("othersites")
childmodels = modelslist.getElementsByTagName("child")
for ost in osmodels:
templatename = Wurm.getXMLText(ost.getElementsByTagName("templatename")[0].childNodes)
ostemplates[templatename] = {}
parameters = ost.getElementsByTagName("parm")
for parm in parameters:
if parm.hasAttribute("name"):
name = Wurm.getXMLText(parm.attributes["name"].childNodes)
ftype = "str"
if parm.hasAttribute("type"):
ftype = Wurm.getXMLText(parm.attributes["type"].childNodes)
value = Wurm.getXMLText(parm.childNodes)
if ftype == "bool":
if value in ('1', 'True'):
value = True
else:
value = False
ostemplates[templatename][name] = value
for child in childmodels:
templatename = Wurm.getXMLText(child.getElementsByTagName("templatename")[0].childNodes)
childtemplates[templatename] = {}
parameters = child.getElementsByTagName("parm")
for parm in parameters:
if parm.hasAttribute("name"):
name = Wurm.getXMLText(parm.attributes["name"].childNodes)
ftype = "str"
if parm.hasAttribute("type"):
ftype = Wurm.getXMLText(parm.attributes["type"].childNodes)
value = Wurm.getXMLText(parm.childNodes)
if ftype == "bool":
if value in ('1', 'True'):
value = True
else:
value = False
childtemplates[templatename][name] = value
except Exception, details:
logger.exception("Error processing %s: %s" % (templatefilename, str(details)))
raise
tracer.debug("Template tables: %s\n%s" % (str(ostemplates), str(childtemplates)))
# now sort the keys ready for the dialogs
ostKeys = ostemplates.keys()
ostKeys.sort()
childtKeys = childtemplates.keys()
childtKeys.sort()
tracer.debug("Template keys: %s\n%s" % (str(ostKeys), str(childtKeys)))
def nullOutput():
""" Resets the message callbacks to default """
tracer.debug("nullOutput")
global cbOutMessage, cbOutDebug, cbOutWarning, cbOutError, cbOutStatus, cbProgressPercent, cbResetProgress, cbProgressPercent2, cbResetProgress2
cbOutMessage = None
cbOutDebug = None
cbOutWarning = None
cbOutError = None
cbOutStatus = None
cbProgressPercent = None
cbResetProgress = None
cbProgressPercent2 = None
cbResetProgress2 = None
def outDebug(msg):
""""""
tracer.log(DEBUG7, "outDebug")
if cbOutDebug is None:
logger.debug(msg)
else:
cbOutDebug(msg)
def outMessage(msg):
""""""
tracer.log(DEBUG7, "outMessage")
if cbOutMessage is None:
logger.info(msg)
else:
cbOutMessage(msg)
def outWarning(msg):
""""""
tracer.log(DEBUG7, "outWarning")
if cbOutWarning is None:
logger.warn(msg)
else:
cbOutWarning(msg)