def test(directory, logger): if (os.path.exists(directory) and os.path.isdir(directory)): index = utilities.loadIndex(directory) objectFilePath = os.path.join(directory, 'objects') everythingIsFine = True counter = 0 erroneousPaths = [] try: #Easier to ask for forgiveness than permission (EAFP) Checks the files to be iterated over exist for fileName in os.listdir(objectFilePath): filePath = os.path.join(objectFilePath, fileName) hash = utilities.createFileSignature(filePath)[2] if hash != fileName: logger.warn("This object's hash does not match its filename: " + str(hash)) #logs an unsuccessful run of test function everythingIsFine = False for filename, hash in index.items(): if not os.path.exists(os.path.join(objectFilePath, hash)): erroneousPaths.append(filename) logger.warn("This file could not be found in the objects directory: " + hash) #logs an unsuccessful run of test function everythingIsFine = False else: counter += 1 except OSError: #throws an error if the files do not exist print "Error: The file does not exist!" if everythingIsFine and len(erroneousPaths) < 1: #Logs the successful running of test function logger.info("All objects' hashes match their filename and all index entries exist as objects in the archive") logger.info("The number of valid index entries is: " + str(counter)) else: print "The backup archive has not been created! Use 'mybackup init' to initialise the directory before calling 'test'."
def backupFile (archiveDir, fileName, index, logger): fileHash = utilities.createFileSignature(fileName)[2]; if (index.has_key(fileName)): if (index[fileName] == fileHash): logger.debug("Backup file '"+fileName+"' already exists and is up-to-date; skipping."); return; #Check whether the file is already in the index #If so, remove the existing file canRemove = True; for key, value in index.items(): if (value == fileHash and key != fileName): canRemove = False;#Another file exists with the same hash if (canRemove and os.path.exists(index[fileName])): os.remove(os.path.join(archiveDir, index[fileName])); logger.info("Replacing backup file '"+fileName+"'"); logger.info("Adding new file '"+fileName+"' to backup."); index[fileName] = fileHash; shutil.copyfile(fileName, os.path.join(archiveDir, fileHash));