import re import shutil import os, os.path bangbang = re.compile(r"^(\xef\xbb\xbf)?## ?") optDepSearch = re.compile(r"^## ?OptionalDeps:") optDepOkLS = re.compile(r"^## ?OptionalDeps:.*LibStub") optDepOkCBH = re.compile(r"^## ?OptionalDeps:.*CallbackHandler-1.0") hasExtLS = False hasExtCBH = False countLS = 0 countCBH = 0 def searchAndDestroy(fullpath, name, evil): found = 0 for dir in os.listdir(fullpath): path = os.path.join(fullpath, dir) file = os.path.join(path, evil+".lua") if dir == evil: shutil.rmtree(path) print "Removing "+evil+" from "+name found += 1 elif os.path.isdir(path): found += searchAndDestroy(path, name, evil) elif os.path.isfile(file): os.remove(file) print "Removing "+evil+" from "+name found += 1 return found def addToToc(tocfile, foundLS, foundCBH): f = file(tocfile, "r+w") newtext = "" edited = False for line in f: if edited: newtext += line else: if optDepSearch.match(line): newline = line.rstrip() if foundLS and not optDepOkLS.match(line): newline += ", LibStub" if foundCBH and not optDepOkCBH.match(line): newline += ", CallbackHandler-1.0" newline += os.linesep newtext += newline #print line #print newline edited = True elif not bangbang.match(line) and not line.strip() == "": newline = "## OptionalDeps: " if foundLS and foundCBH: newline += "LibStub, CallbackHandler-1.0" elif foundLS: newline += "LibStub" elif foundCBH: newline += "CallbackHandler-1.0" newline += os.linesep newline += os.linesep newtext += newline edited = True #print "No old OptDeps" #print newline newtext += line else: newtext += line if edited: f.seek(0) f.truncate() f.write(newtext) f.close() def purgeDir(fullpath, name): foundLS = searchAndDestroy(fullpath, name, "LibStub") foundCBH = searchAndDestroy(fullpath, name, "CallbackHandler-1.0") global countLS, countCBH countLS += foundLS countCBH += foundCBH if foundLS or foundCBH: tocfile = os.path.join(fullpath, name+".toc") if os.path.isfile(tocfile): print "Editing toc: ", name+".toc" addToToc(tocfile, foundLS, foundCBH) directory = os.path.join(os.getcwd(), "AddOns") print directory for name in os.listdir(directory): fullpath = os.path.join(directory, name) svnpath = os.path.join(fullpath, ".svn") gitpath = os.path.join(fullpath, ".git") if os.path.isdir(fullpath): if name == "LibStub": hasExtLS = True elif name == "CallbackHandler-1.0": hasExtCBH = True elif os.path.isdir(svnpath) or os.path.isdir(gitpath): #print name + " has .svn! Ignoring..." donothing = 1 # stupid python for not letting me have empty blocks :\ else: purgeDir(fullpath, name) print "Found and purged ", countLS, " LibStubs and ", countCBH, " CallbackHandlers." if not hasExtLS: print "Warning! No external version of LibStub found!" if not hasExtCBH: print "Warning! No external version of CallbackHandler-1.0 found!"