def store(self, item): #Check if the item in folder storage is newer than the target one fullPath = item.getFullPath() relaPath = item.getRelaPath() localFullPath = os.path.join(self.rootDir, relaPath) localItem = folderStorageItem(self.rootDir, localFullPath) localPathDir = os.path.dirname(localFullPath) if (not os.path.exists(localFullPath)): #Copy the updated file to local path misc.ensureDir(localPathDir) shutil.move(fullPath, localFullPath) #print localFullPath self.lastState.update(item) elif isSameItems(item, localItem): print 'same, ignore:', fullPath, localFullPath elif isItem1Newer(item, localItem): #The file need to be stored is newer, copy the older file to backup storage backupFullPath = fileTools.getFreeNameFromFullPath(os.path.join(self.backupDir, relaPath)) backupPathDir = os.path.dirname(backupFullPath) misc.ensureDir(backupPathDir) shutil.move(localFullPath, backupFullPath) #Copy the updated file to local path misc.ensureDir(localPathDir) shutil.copy(fullPath, localFullPath) self.lastState.update(item) else: #The file need to be stored is old, copy it to backup storage backupFullPath = fileTools.getFreeNameFromFullPath(os.path.join(self.backupDir, relaPath)) backupPathDir = os.path.dirname(backupFullPath) misc.ensureDir(backupPathDir) shutil.copy(fullPath, backupPathDir)
def store(self, item): #Check if the item in folder storage is newer than the target one relaPath = item.getRelaPath() localFullPath = os.path.join(self.rootDir, relaPath) localItem = folderStorageItem(self.rootDir, localFullPath) extItem = externalItem(self.rootDir, localFullPath, item.getItemInfo()) extItemLocalCopy = folderStorageItem(self.rootDir, localFullPath, self.lastState.getItemState(item)) localPathDir = os.path.dirname(localFullPath) fullPath = item.getFullPath() if (not os.path.exists(localFullPath)): #Copy the updated file to local path misc.ensureDir(localPathDir) item.saveTo(self.rootDir) #Check if the timestamp are updated if localItem.getTimestamp() != extItem.timestamp(): raise 'not work as expected, need to manually update time for local copy' self.lastState.update(extItem) elif self.lastState.updated(item): #print 'item updated:', item.getRelaPath() #The item has been updated, check content if isSameItems(item, localItem): print 'same, ignore:', fullPath.encode('gbk','replace'), localFullPath.encode('gbk','replace') #Need to update the item state self.lastState.update(extItem) else: #Seems external storage and local storage both updated this file. What to do? Ignore for now? pass else: #Not updated, check if the 2 items are same if isSameItems(item, extItemLocalCopy): print 'According to saved state, same, ignore:', fullPath.encode('gbk','replace'), localFullPath.encode('gbk','replace') #Need to update the item state self.lastState.update(extItem) elif extItemLocalCopy.isExternal(): #It is an external file, check time if (extItemLocalCopy.getExtTs() < item.getTimestamp()): #External storage has updated the item, update the local one backupFullPath = fileTools.getFreeNameFromFullPath(os.path.join(self.backupDir, relaPath)) backupPathDir = os.path.dirname(backupFullPath) misc.ensureDir(backupPathDir) shutil.move(localFullPath, backupFullPath) #Copy the updated file to local path misc.ensureDir(localPathDir) item.saveTo(self.rootDir) self.lastState.update(extItem) else: if extItemLocalCopy.getTimestamp < item.getTimestamp(): #External storage has updated the item, update the local one backupFullPath = fileTools.getFreeNameFromFullPath(os.path.join(self.backupDir, relaPath)) backupPathDir = os.path.dirname(backupFullPath) misc.ensureDir(backupPathDir) shutil.move(localFullPath, backupFullPath) #Copy the updated file to local path misc.ensureDir(localPathDir) item.saveTo(self.rootDir) self.lastState.update(extItem)
def store(self, item, pendingCollection): #Check if the item in folder storage is newer than the target one relaPath = item["idInCol"] localFullPath = transform.transformDirToInternal(os.path.join(self.rootDir, relaPath)) localPathDir = transform.transformDirToInternal(os.path.dirname(localFullPath)) #Check the updates of the object is done in advCollectionProcess. #When we come here, the item is for sure needed for update #Backup if it exists if os.path.exists(localFullPath): backupFullPath = fileTools.getFreeNameFromFullPath(os.path.join(self.backupDir, relaPath)) backupPathDir = os.path.dirname(backupFullPath) misc.ensureDir(backupPathDir) shutil.move(localFullPath, backupFullPath) misc.ensureDir(localPathDir) item.saveTo(self.rootDir) ################################# #Update collection info ################################# #Remove the legacy timestamp objInfo = copy.copy(item.getItemInfo()) if objInfo.has_key("timestamp"): objInfo["externalTimestamp"] = objInfo["timestamp"] del objInfo["timestamp"] obj = objectDatabase.fsObjBase(localFullPath, objInfo) #ncl(obj.getItemInfo()) #print type(obj.getItemInfo()) #ncl(self.objDb.getDbObj(obj.getItemObjUrl())) itemUuid = self.objDb.addDbObj(obj.getObjUrl(), obj.getItemInfo()) ncl("collection obj uuid:", itemUuid) #ncl(self.objDb.getDbObj(obj.getItemObjUrl())) self.updateObjUuid(item["idInCol"], itemUuid) return True