runtime/PLCObject.py
changeset 2487 6a4f9a061994
parent 2486 44c2a4e2b84d
child 2492 7dd551ac2fa0
equal deleted inserted replaced
2486:44c2a4e2b84d 2487:6a4f9a061994
    42 from runtime.typemapping import TypeTranslator
    42 from runtime.typemapping import TypeTranslator
    43 from runtime.loglevels import LogLevelsDefault, LogLevelsCount
    43 from runtime.loglevels import LogLevelsDefault, LogLevelsCount
    44 from runtime.Stunnel import getPSKID
    44 from runtime.Stunnel import getPSKID
    45 from runtime import PlcStatus
    45 from runtime import PlcStatus
    46 from runtime import MainWorker
    46 from runtime import MainWorker
    47 
       
    48 empty_md5_digest = md5.new().digest()
       
    49 
    47 
    50 if os.name in ("nt", "ce"):
    48 if os.name in ("nt", "ce"):
    51     dlopen = _ctypes.LoadLibrary
    49     dlopen = _ctypes.LoadLibrary
    52     dlclose = _ctypes.FreeLibrary
    50     dlclose = _ctypes.FreeLibrary
    53 elif os.name == "posix":
    51 elif os.name == "posix":
   466         if os.path.exists(self.tmpdir):
   464         if os.path.exists(self.tmpdir):
   467             shutil.rmtree(self.tmpdir)
   465             shutil.rmtree(self.tmpdir)
   468         os.mkdir(self.tmpdir)
   466         os.mkdir(self.tmpdir)
   469     
   467     
   470     @RunInMain
   468     @RunInMain
       
   469     def SeedBlob(self, seed):
       
   470         blob = (mkstemp(dir=self.tmpdir) + (md5.new(),))
       
   471         fobj, path, md5sum = blob
       
   472         md5sum.update(seed)
       
   473         newBlobID = md5sum.digest()
       
   474         self.blobs[newBlobID] = blob
       
   475         return newBlobID
       
   476 
       
   477     @RunInMain
   471     def AppendChunkToBlob(self, data, blobID):
   478     def AppendChunkToBlob(self, data, blobID):
   472         blob = ((mkstemp(dir=self.tmpdir) if data else None)\
   479         blob = self.blobs.pop(blobID, None)
   473                    + (md5.new(),)) \
       
   474                if blobID == empty_md5_digest \
       
   475                else self.blobs.pop(blobID, None)
       
   476 
   480 
   477         if blob is None:
   481         if blob is None:
   478             return None
   482             return None
   479 
   483 
   480         fobj, path, md5sum = blob
   484         fobj, path, md5sum = blob
   481         md5sum.update(data)
   485         md5sum.update(data)
   482         newBlobID = md5sum.digest()
   486         newBlobID = md5sum.digest()
   483         if data:
   487         os.write(fobj,data)
   484             os.write(fobj,data)
   488         self.blobs[newBlobID] = blob
   485             self.blobs[newBlobID] = blob
       
   486         return newBlobID
   489         return newBlobID
   487 
   490 
   488     @RunInMain
   491     @RunInMain
   489     def PurgeBlobs(self):
   492     def PurgeBlobs(self):
   490         for fobj, path, md5sum in self.blobs:
   493         for fobj, path, md5sum in self.blobs:
   493 
   496 
   494     def _BlobAsFile(self, blobID, newpath):
   497     def _BlobAsFile(self, blobID, newpath):
   495         blob = self.blobs.pop(blobID, None)
   498         blob = self.blobs.pop(blobID, None)
   496 
   499 
   497         if blob is None:
   500         if blob is None:
   498             if blobID == md5.new().digest():
       
   499                 # create empty file
       
   500                 open(newpath,'r').close()
       
   501                 return
       
   502             raise Exception(_("Missing data to create file: {}").format(newpath))
   501             raise Exception(_("Missing data to create file: {}").format(newpath))
   503 
   502 
   504         fobj, path, md5sum = blob
   503         fobj, path, md5sum = blob
   505         os.close(fobj)
   504         os.close(fobj)
   506         shutil.move(path, newpath)
   505         shutil.move(path, newpath)