Diff for /cdli/cdli_files.py between versions 1.19 and 1.20

version 1.19, 2006/06/13 18:34:10 version 1.20, 2006/06/13 20:42:47
Line 17  from threading import Thread Line 17  from threading import Thread
 from ZPublisher.HTTPRequest import HTTPRequest  from ZPublisher.HTTPRequest import HTTPRequest
 from ZPublisher.HTTPResponse import HTTPResponse  from ZPublisher.HTTPResponse import HTTPResponse
 from ZPublisher.BaseRequest import RequestContainer  from ZPublisher.BaseRequest import RequestContainer
   import threading
 global tmpVar  global tmpVar
   
 class uploadATFfinallyThread(Thread):  class uploadATFfinallyThread(Thread):
       """class for adding uploaded filed (temporarily stored in the staging area at /tmp"""
     
     def __init__(self):      def __init__(self):
           """init for uploadATFfinallyThread"""
         self.continueVar=True          self.continueVar=True
         self.returnValue=None          self.returnValue=None
         self.end=False          self.end=False
         Thread.__init__(self)          Thread.__init__(self)
                         
     def set(self,procedure,comment="",basketname='',unlock=None,SESSION=None,username=None,serverport="8080"):      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.procedure=procedure
         self.comment=comment          self.comment=comment
         self.basketname=basketname          self.basketname=basketname
Line 39  class uploadATFfinallyThread(Thread): Line 41  class uploadATFfinallyThread(Thread):
         self.serverport=serverport          self.serverport=serverport
                   
     def __call__(self):      def __call__(self):
         """wait"""          """call of the thread (equals run)"""
         self.run()          self.run()
         return True          return True
           
     def getContext(self, app,serverport="8080"):      def getContext(self, app,serverport="8080"):
           """get the context within the ZODB"""
           
         resp = HTTPResponse(stdout=None)          resp = HTTPResponse(stdout=None)
         env = {          env = {
             'SERVER_NAME':'localhost',              'SERVER_NAME':'localhost',
Line 55  class uploadATFfinallyThread(Thread): Line 59  class uploadATFfinallyThread(Thread):
                       
           
     def run(self):      def run(self):
           """run"""
   
         self.result=""          self.result=""
           #find context within ZODB
         from Zope import DB          from Zope import DB
         conn = DB.open()          conn = DB.open()
         root = conn.root()          root = conn.root()
         app  = root['Application']          app  = root['Application']
         ctx = self.getContext(app,serverport=self.serverport)          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)          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()          get_transaction().commit()
         conn.close()          conn.close()
           #set flag for end of this method
         self.end=True          self.end=True
         return True          return True
           
     def getResult(self):      def getResult(self):
           """method for accessing result"""
           
         return self.result          return self.result
             
     def uploadATFfinallyThread(self,ctx,procedure,comment="",basketname='',unlock=None,RESPONSE=None,SESSION=None,username=None):      def uploadATFfinallyThread(self,ctx,procedure,comment="",basketname='',unlock=None,RESPONSE=None,SESSION=None,username=None):
         """upload the files"""          """upload the files"""
           #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot
         ctx2=ctx.cdliRoot          ctx2=ctx.cdliRoot
         
         self.result+="<h2>Start processing</h2>"          self.result+="<h2>Start processing</h2>"
           
           #shall I only upload the changed files?
         if procedure=="uploadchanged":          if procedure=="uploadchanged":
               
             uploadFns=SESSION.get('changed',[])+SESSION.get('newPs',[])              uploadFns=SESSION.get('changed',[])+SESSION.get('newPs',[])
                   
           #or all
         elif procedure=="uploadAll":          elif procedure=="uploadAll":
             uploadFns=[]              uploadFns=[]
             for x in os.listdir(SESSION['tmpdir']):              for x in os.listdir(SESSION['tmpdir']):
                 if not x in SESSION['errors']:                  if not x in SESSION['errors']:
                     uploadFns.append(x)                      uploadFns.append(x)
                       
           #or maybe nothing
         elif procedure=="noupload":          elif procedure=="noupload":
                         return True                          return True
         else:          else:
             uploadFns=[]              uploadFns=[]
                           
           #do first the changed files    
         for fn in uploadFns:          for fn in uploadFns:
             founds=ctx2.CDLICatalog.search({'title':fn})              founds=ctx2.CDLICatalog.search({'title':fn})
             if len(founds)>0:              if len(founds)>0:
Line 99  class uploadATFfinallyThread(Thread): Line 117  class uploadATFfinallyThread(Thread):
                 founds[0].getObject().manage_addCDLIFileObject('',comment,SESSION['author'],file=file(os.path.join(SESSION['tmpdir'],fn)))                  founds[0].getObject().manage_addCDLIFileObject('',comment,SESSION['author'],file=file(os.path.join(SESSION['tmpdir'],fn)))
                           
                   
                           #now add the new files        
         newPs=SESSION['newPs']          newPs=SESSION['newPs']
         if len(newPs)>0:          if len(newPs)>0:
             tmpDir=SESSION['tmpdir']              tmpDir=SESSION['tmpdir']
             self.result+="<p>Adding files</p>"              self.result+="<p>Adding files</p>"
               #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)              ctx2.cdli_main.importFiles(comment=comment,author=str(username) ,folderName=tmpDir, files=newPs,ext=self)
                                   
                   
                   
         #unlock          #unlock locked files?
         if unlock:          if unlock:
             self.result+="<p>Unlock files</p>"              self.result+="<p>Unlock files</p>"
             unlockFns=[]              unlockFns=[]
Line 123  class uploadATFfinallyThread(Thread): Line 142  class uploadATFfinallyThread(Thread):
                                         
                     founds[0].getObject().lockedBy=""                      founds[0].getObject().lockedBy=""
                                           
           #if a basketname is give, add files to the basket
         if not (basketname ==''):          if not (basketname ==''):
             self.result+="<p>Add basket</p>"              self.result+="<p>Add basket</p>"
             basketId=ctx2.basketContainer.getBasketIdfromName(basketname)              basketId=ctx2.basketContainer.getBasketIdfromName(basketname)
Line 142  class uploadATFfinallyThread(Thread): Line 162  class uploadATFfinallyThread(Thread):
         return True          return True
           
 class uploadATFThread(Thread):  class uploadATFThread(Thread):
       """class for checking the files befor uploading"""
           
     def __init__(self):      def __init__(self):
           """initialise"""
           
         self.continueVar=True          self.continueVar=True
         self.returnValue=None          self.returnValue=None
                   
Line 151  class uploadATFThread(Thread): Line 174  class uploadATFThread(Thread):
                   
                   
     def set(self,upload,basketId,username,serverport="8080"):      def set(self,upload,basketId,username,serverport="8080"):
           """set start values for the thread"""
         self.result=""          self.result=""
         self.upload=upload          self.upload=upload
         self.basketId=basketId          self.basketId=basketId
Line 158  class uploadATFThread(Thread): Line 182  class uploadATFThread(Thread):
         self.serverport=serverport          self.serverport=serverport
                   
     def __call__(self):      def __call__(self):
         """wait"""          """call method """
         self.run()          self.run()
         return True          return True
           
     def getContext(self, app,serverport="8080"):      def getContext(self, app,serverport="8080"):
           """get the context within the ZODB"""
         resp = HTTPResponse(stdout=None)          resp = HTTPResponse(stdout=None)
         env = {          env = {
             'SERVER_NAME':'localhost',              'SERVER_NAME':'localhost',
Line 175  class uploadATFThread(Thread): Line 200  class uploadATFThread(Thread):
     def run(self):      def run(self):
             
         self.result=""          self.result=""
           #find context within ZODB
         from Zope import DB          from Zope import DB
         conn = DB.open()          conn = DB.open()
         root = conn.root()          root = conn.root()
Line 186  class uploadATFThread(Thread): Line 212  class uploadATFThread(Thread):
             pass              pass
         get_transaction().abort()          get_transaction().abort()
         conn.close()          conn.close()
           print "done"
                   
     def getResult(self):      def getResult(self):
           """method for accessing result"""
         return self.result          return self.result
           
     def uploadATFThread(self,ctx,upload,basketId=0):      def uploadATFThread(self,ctx,upload,basketId=0):
Line 196  class uploadATFThread(Thread): Line 223  class uploadATFThread(Thread):
         #TODO: add comments          #TODO: add comments
         #TODO: finish uploadATF          #TODO: finish uploadATF
         self.result="<html><body><h2>I am loading your file...</h2>"          self.result="<html><body><h2>I am loading your file...</h2>"
           #make sure that id is a string and not an integer
         basketId=str(basketId)          basketId=str(basketId)
                   
                   #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot
         ctx2=ctx.cdliRoot          ctx2=ctx.cdliRoot
           
           #get temporary file for staging the downloaded and splitted files
         dir=mkdtemp()          dir=mkdtemp()
         changed=[]          
         errors=[]          
         newPs=[]          changed=[] # changed files
         psNotInCatalog=[]          errors=[]  # files with errors
           newPs=[]   # new p filed
           psNotInCatalog=[] # files not in the catalog
           
           #split the uploadedd atf file
         basketNameFromFile, numberOfFiles=splitatf(upload,dir)          basketNameFromFile, numberOfFiles=splitatf(upload,dir)
                   
           #find basketId if not set
           
           #get active abaket
         if basketId == '0':          if basketId == '0':
             basketObj=ctx2.basketContainer.getActiveBasket()              basketObj=ctx2.basketContainer.getActiveBasket()
             if basketObj:              if basketObj:
                 basketId=basketObj.getId()                  basketId=basketObj.getId()
                                   
           #if there is no active baske and no basketid given, id is empty, else get besketname and length
         if basketId == '0':          if basketId == '0':
             basketNameFromId=""              basketNameFromId=""
             basketLen=0              basketLen=0
Line 222  class uploadATFThread(Thread): Line 260  class uploadATFThread(Thread):
                   
         self.result+="<html><body><h2>I got the files</h2><p>I am checking now the files</p>"          self.result+="<html><body><h2>I got the files</h2><p>I am checking now the files</p>"
                                                                                 
           #start to check the files
         for fn in os.listdir(dir):          for fn in os.listdir(dir):
                           
             self.result+="<p>check:%s</p>"%fn              self.result+="<p>check:%s</p>"%fn
                           
               # check if file is in the catalog
               #TODO: checkCatalog is not implemented yet
             if ctx2.cdli_main.checkCatalog(fn):              if ctx2.cdli_main.checkCatalog(fn):
                 psNotInCatalog.append(fn)                  psNotInCatalog.append(fn)
                                   
                               #check if p-file already at the server  
             founds=ctx2.CDLICatalog.search({'title':fn})                  founds=ctx2.CDLICatalog.search({'title':fn})    
               
               #if not than add filename to the list of newfiles
             if len(founds)==0:              if len(founds)==0:
                 newPs.append(fn)                  newPs.append(fn)
                                   
               #if p file alread at the server    
             for found in founds:              for found in founds:
                   #analyse the differences to the actual file
                 obj=found.getObject()                  obj=found.getObject()
                 
                 if (not obj.lockedBy=='') and (not obj.lockedBy==self.username):                  if (not obj.lockedBy=='') and (not obj.lockedBy==self.username):
Line 247  class uploadATFThread(Thread): Line 291  class uploadATFThread(Thread):
                         changed.append((obj,diffs))                          changed.append((obj,diffs))
                         #hochladen                          #hochladen
                   
                 #ready, set the returnValues
         self.result+="<h3>Done</h3></body></html>"          self.result+="<h3>Done</h3></body></html>"
                   
         self.returnValue={}          self.returnValue={}
Line 1261  class CDLIFileFolder(versionedFileFolder Line 1305  class CDLIFileFolder(versionedFileFolder
         threadName=repeat          threadName=repeat
         if not threadName or threadName=="":          if not threadName or threadName=="":
             tmpVar=False              tmpVar=False
               thread=uploadATFThread()
             self._v_uploadATF=uploadATFThread()              self._v_uploadATF=thread
              
             #self._xmltrans.start()              #self._xmltrans.start()
             #thread=Thread(target=self._v_uploadATF)              #thread=Thread(target=self._v_uploadATF)
                           
Line 1282  class CDLIFileFolder(versionedFileFolder Line 1325  class CDLIFileFolder(versionedFileFolder
             #_v_xmltrans.run()              #_v_xmltrans.run()
                           
         else:          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:              if not self._v_uploadATF.returnValue:
                   

Removed from v.1.19  
changed lines
  Added in v.1.20


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>