--- cdli/cdli_files.py 2006/06/13 18:34:10 1.19 +++ cdli/cdli_files.py 2006/06/13 20:42:47 1.20 @@ -17,19 +17,21 @@ from threading import Thread from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPResponse import HTTPResponse from ZPublisher.BaseRequest import RequestContainer - +import threading global tmpVar class uploadATFfinallyThread(Thread): - + """class for adding uploaded filed (temporarily stored in the staging area at /tmp""" + def __init__(self): + """init for uploadATFfinallyThread""" self.continueVar=True self.returnValue=None self.end=False Thread.__init__(self) def set(self,procedure,comment="",basketname='',unlock=None,SESSION=None,username=None,serverport="8080"): - """set values""" + """set start values for the thread""" self.procedure=procedure self.comment=comment self.basketname=basketname @@ -39,11 +41,13 @@ class uploadATFfinallyThread(Thread): self.serverport=serverport def __call__(self): - """wait""" + """call of the thread (equals run)""" self.run() return True def getContext(self, app,serverport="8080"): + """get the context within the ZODB""" + resp = HTTPResponse(stdout=None) env = { 'SERVER_NAME':'localhost', @@ -55,42 +59,56 @@ class uploadATFfinallyThread(Thread): def run(self): - + """run""" + self.result="" + #find context within ZODB from Zope import DB conn = DB.open() root = conn.root() app = root['Application'] ctx = self.getContext(app,serverport=self.serverport) + #add the files self.uploadATFfinallyThread(ctx,self.procedure,comment=self.comment,basketname=self.basketname,unlock=self.unlock,SESSION=self.SESSION,username=self.username) + #commit the transactions get_transaction().commit() conn.close() + #set flag for end of this method self.end=True return True def getResult(self): + """method for accessing result""" + return self.result def uploadATFfinallyThread(self,ctx,procedure,comment="",basketname='',unlock=None,RESPONSE=None,SESSION=None,username=None): """upload the files""" + #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot ctx2=ctx.cdliRoot self.result+="

Start processing

" + + #shall I only upload the changed files? if procedure=="uploadchanged": uploadFns=SESSION.get('changed',[])+SESSION.get('newPs',[]) + #or all elif procedure=="uploadAll": uploadFns=[] for x in os.listdir(SESSION['tmpdir']): if not x in SESSION['errors']: uploadFns.append(x) + + #or maybe nothing elif procedure=="noupload": return True else: uploadFns=[] + #do first the changed files for fn in uploadFns: founds=ctx2.CDLICatalog.search({'title':fn}) if len(founds)>0: @@ -99,16 +117,17 @@ class uploadATFfinallyThread(Thread): founds[0].getObject().manage_addCDLIFileObject('',comment,SESSION['author'],file=file(os.path.join(SESSION['tmpdir'],fn))) - + #now add the new files newPs=SESSION['newPs'] if len(newPs)>0: tmpDir=SESSION['tmpdir'] self.result+="

Adding files

" + #TODO: make this configurable, at the moment base folder for the files has to be cdli_main ctx2.cdli_main.importFiles(comment=comment,author=str(username) ,folderName=tmpDir, files=newPs,ext=self) - #unlock + #unlock locked files? if unlock: self.result+="

Unlock files

" unlockFns=[] @@ -123,6 +142,7 @@ class uploadATFfinallyThread(Thread): founds[0].getObject().lockedBy="" + #if a basketname is give, add files to the basket if not (basketname ==''): self.result+="

Add basket

" basketId=ctx2.basketContainer.getBasketIdfromName(basketname) @@ -142,8 +162,11 @@ class uploadATFfinallyThread(Thread): return True class uploadATFThread(Thread): + """class for checking the files befor uploading""" def __init__(self): + """initialise""" + self.continueVar=True self.returnValue=None @@ -151,6 +174,7 @@ class uploadATFThread(Thread): def set(self,upload,basketId,username,serverport="8080"): + """set start values for the thread""" self.result="" self.upload=upload self.basketId=basketId @@ -158,11 +182,12 @@ class uploadATFThread(Thread): self.serverport=serverport def __call__(self): - """wait""" + """call method """ self.run() return True def getContext(self, app,serverport="8080"): + """get the context within the ZODB""" resp = HTTPResponse(stdout=None) env = { 'SERVER_NAME':'localhost', @@ -175,6 +200,7 @@ class uploadATFThread(Thread): def run(self): self.result="" + #find context within ZODB from Zope import DB conn = DB.open() root = conn.root() @@ -186,9 +212,10 @@ class uploadATFThread(Thread): pass get_transaction().abort() conn.close() - + print "done" def getResult(self): + """method for accessing result""" return self.result def uploadATFThread(self,ctx,upload,basketId=0): @@ -196,22 +223,33 @@ class uploadATFThread(Thread): #TODO: add comments #TODO: finish uploadATF self.result="

I am loading your file...

" + #make sure that id is a string and not an integer basketId=str(basketId) - + #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot ctx2=ctx.cdliRoot + + #get temporary file for staging the downloaded and splitted files dir=mkdtemp() - changed=[] - errors=[] - newPs=[] - psNotInCatalog=[] + + + changed=[] # changed files + errors=[] # files with errors + newPs=[] # new p filed + psNotInCatalog=[] # files not in the catalog + + #split the uploadedd atf file basketNameFromFile, numberOfFiles=splitatf(upload,dir) + #find basketId if not set + + #get active abaket if basketId == '0': basketObj=ctx2.basketContainer.getActiveBasket() if basketObj: basketId=basketObj.getId() + #if there is no active baske and no basketid given, id is empty, else get besketname and length if basketId == '0': basketNameFromId="" basketLen=0 @@ -221,21 +259,27 @@ class uploadATFThread(Thread): self.result+="

I got the files

I am checking now the files

" - + + #start to check the files for fn in os.listdir(dir): self.result+="

check:%s

"%fn + # check if file is in the catalog + #TODO: checkCatalog is not implemented yet if ctx2.cdli_main.checkCatalog(fn): psNotInCatalog.append(fn) - + #check if p-file already at the server founds=ctx2.CDLICatalog.search({'title':fn}) + #if not than add filename to the list of newfiles if len(founds)==0: newPs.append(fn) - + + #if p file alread at the server for found in founds: + #analyse the differences to the actual file obj=found.getObject() if (not obj.lockedBy=='') and (not obj.lockedBy==self.username): @@ -247,7 +291,7 @@ class uploadATFThread(Thread): changed.append((obj,diffs)) #hochladen - + #ready, set the returnValues self.result+="

Done

" self.returnValue={} @@ -1261,9 +1305,8 @@ class CDLIFileFolder(versionedFileFolder threadName=repeat if not threadName or threadName=="": tmpVar=False - - self._v_uploadATF=uploadATFThread() - + thread=uploadATFThread() + self._v_uploadATF=thread #self._xmltrans.start() #thread=Thread(target=self._v_uploadATF) @@ -1282,6 +1325,10 @@ class CDLIFileFolder(versionedFileFolder #_v_xmltrans.run() else: + if not hasattr(self,'_v_uploadATF'): + for thread in threading.enumerate(): + if threadName == thread.getName(): + self._v_uploadATF=thread if not self._v_uploadATF.returnValue: