def restructureBagForComplianceFileUUIDsAssigned( unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith="%transferDirectory%"): bagFileDefaultDest = os.path.join(unitPath, "logs", "BagIt") requiredDirectories.append(bagFileDefaultDest) unitDataPath = os.path.join(unitPath, "data") for dir in requiredDirectories: dirPath = os.path.join(unitPath, dir) dirDataPath = os.path.join(unitPath, "data", dir) if os.path.isdir(dirDataPath): #move to the top level src = dirDataPath dst = dirPath updateDirectoryLocation(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) print "moving directory ", dir else: if not os.path.isdir(dirPath): print "creating: ", dir os.mkdir(dirPath) for item in os.listdir(unitPath): src = os.path.join(unitPath, item) if os.path.isfile(src): if item.startswith("manifest"): dst = os.path.join(unitPath, "metadata", item) else: dst = os.path.join(bagFileDefaultDest, item) updateFileLocation2(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) for item in os.listdir(unitDataPath): itemPath = os.path.join(unitDataPath, item) if os.path.isdir(itemPath) and item not in requiredDirectories: print "moving directory to objects: ", item dst = os.path.join(unitPath, "objects", item) updateDirectoryLocation(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) elif os.path.isfile(itemPath) and item not in optionalFiles: print "moving file to objects: ", item dst = os.path.join(unitPath, "objects", item) updateFileLocation2(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) elif item in optionalFiles: dst = os.path.join(unitPath, item) updateFileLocation2(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) print "removing empty data directory" os.rmdir(unitDataPath)
def restructureBagForComplianceFileUUIDsAssigned(job, unitPath, unitIdentifier, unitIdentifierType="transfer_id", unitPathReplaceWith="%transferDirectory%"): bagFileDefaultDest = os.path.join(unitPath, "logs", "BagIt") REQUIRED_DIRECTORIES.append(bagFileDefaultDest) # This needs to be cast to a string since we're calling os.path.join(), # and any of the other arguments could contain arbitrary, non-Unicode # characters. unitPath = str(unitPath) unitDataPath = str(os.path.join(unitPath, "data")) for dir in REQUIRED_DIRECTORIES: dirPath = os.path.join(unitPath, dir) dirDataPath = os.path.join(unitPath, "data", dir) if os.path.isdir(dirDataPath): # move to the top level src = dirDataPath dst = dirPath fileOperations.updateDirectoryLocation(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) job.pyprint("moving directory ", dir) else: if not os.path.isdir(dirPath): job.pyprint("creating: ", dir) os.mkdir(dirPath) for item in os.listdir(unitPath): src = os.path.join(unitPath, item) if os.path.isfile(src): if item.startswith("manifest"): dst = os.path.join(unitPath, "metadata", item) fileOperations.updateFileLocation2(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint) elif item in OPTIONAL_FILES: job.pyprint("not moving:", item) else: dst = os.path.join(bagFileDefaultDest, item) fileOperations.updateFileLocation2(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint) for item in os.listdir(unitDataPath): itemPath = os.path.join(unitDataPath, item) if os.path.isdir(itemPath) and item not in REQUIRED_DIRECTORIES: job.pyprint("moving directory to objects: ", item) dst = os.path.join(unitPath, "objects", item) fileOperations.updateDirectoryLocation(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) elif os.path.isfile(itemPath) and item not in OPTIONAL_FILES: job.pyprint("moving file to objects: ", item) dst = os.path.join(unitPath, "objects", item) fileOperations.updateFileLocation2(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint) elif item in OPTIONAL_FILES: dst = os.path.join(unitPath, item) fileOperations.updateFileLocation2(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint) job.pyprint("removing empty data directory") os.rmdir(unitDataPath)
def restructureBagForComplianceFileUUIDsAssigned(unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith = "%transferDirectory%"): bagFileDefaultDest = os.path.join(unitPath, "logs", "BagIt") requiredDirectories.append(bagFileDefaultDest) unitDataPath = os.path.join(unitPath, "data") for dir in requiredDirectories: dirPath = os.path.join(unitPath, dir) dirDataPath = os.path.join(unitPath, "data", dir) if os.path.isdir(dirDataPath): #move to the top level src = dirDataPath dst = dirPath updateDirectoryLocation(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) print "moving directory ", dir else: if not os.path.isdir(dirPath): print "creating: ", dir os.mkdir(dirPath) for item in os.listdir(unitPath): src = os.path.join(unitPath, item) if os.path.isfile(src): if item.startswith("manifest"): dst = os.path.join(unitPath, "metadata", item) else: dst = os.path.join(bagFileDefaultDest, item) updateFileLocation2(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) for item in os.listdir(unitDataPath): itemPath = os.path.join(unitDataPath, item) if os.path.isdir(itemPath) and item not in requiredDirectories: print "moving directory to objects: ", item dst = os.path.join(unitPath, "objects", item) updateDirectoryLocation(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) elif os.path.isfile(itemPath) and item not in optionalFiles: print "moving file to objects: ", item dst = os.path.join(unitPath, "objects", item) updateFileLocation2(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) elif item in optionalFiles: dst = os.path.join(unitPath, item) updateFileLocation2(itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith) print "removing empty data directory" os.rmdir(unitDataPath)
def restructureBagForComplianceFileUUIDsAssigned( job, unitPath, unitIdentifier, unitIdentifierType="transfer_id", unitPathReplaceWith="%transferDirectory%", ): bagFileDefaultDest = os.path.join(unitPath, "logs", "BagIt") MY_REQUIRED_DIRECTORIES = REQUIRED_DIRECTORIES + (bagFileDefaultDest, ) # This needs to be cast to a string since we're calling os.path.join(), # and any of the other arguments could contain arbitrary, non-Unicode # characters. unitPath = str(unitPath) unitDataPath = str(os.path.join(unitPath, "data")) for dir in MY_REQUIRED_DIRECTORIES: dirPath = os.path.join(unitPath, dir) dirDataPath = os.path.join(unitPath, "data", dir) if os.path.isdir(dirDataPath): if dir == "metadata" and os.path.isdir(dirPath): # We move the existing top-level metadata folder, or merge it # with what is currently there, before the next set of # directory operations to move everything up a level below. job.pyprint("{}: moving/merging {} to {}".format( dir, dirPath, dirDataPath)) move_or_merge(dirPath, dirDataPath) # move to the top level src = dirDataPath dst = dirPath fileOperations.updateDirectoryLocation( src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, ) job.pyprint("moving directory ", dir) else: if not os.path.isdir(dirPath): job.pyprint("creating: ", dir) os.makedirs(dirPath) for item in os.listdir(unitPath): src = os.path.join(unitPath, item) if os.path.isfile(src): if item.startswith("manifest"): dst = os.path.join(unitPath, "metadata", item) fileOperations.updateFileLocation2( src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint, ) elif item in OPTIONAL_FILES: job.pyprint("not moving:", item) else: dst = os.path.join(bagFileDefaultDest, item) fileOperations.updateFileLocation2( src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint, ) for item in os.listdir(unitDataPath): itemPath = os.path.join(unitDataPath, item) if os.path.isdir(itemPath) and item not in MY_REQUIRED_DIRECTORIES: job.pyprint("moving directory to objects: ", item) dst = os.path.join(unitPath, "objects", item) fileOperations.updateDirectoryLocation( itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, ) elif os.path.isfile(itemPath) and item not in OPTIONAL_FILES: job.pyprint("moving file to objects: ", item) dst = os.path.join(unitPath, "objects", item) fileOperations.updateFileLocation2( itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint, ) elif item in OPTIONAL_FILES: dst = os.path.join(unitPath, item) fileOperations.updateFileLocation2( itemPath, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith, printfn=job.pyprint, ) job.pyprint("removing empty data directory") os.rmdir(unitDataPath)