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 getFile (archiveDir, searchPattern): if (not os.path.exists(archiveDir) or not os.path.isdir(archiveDir)): print "The backup archive has not been created! Use 'mybackup init' to initialise the directory before calling 'get'."; return; try: index = utilities.loadIndex(archiveDir); except OSError as (errno, strerror): print "Failed to load archive index at '"+archiveDir+"': "+strerror; return;
def store (archiveDir, dirToBackup, logger): dirToBackup = os.path.abspath(dirToBackup); if (os.path.exists(archiveDir) and os.path.isdir(archiveDir)): if (not os.path.exists(dirToBackup)): print "Directory '"+dirToBackup+"' does not exist!"; return; elif (not os.path.isdir(dirToBackup)): print "File '"+dirToBackup+"' is not a directory!"; return; try: index = utilities.loadIndex(archiveDir); except OSError as (errno, strerror): print "Failed to load archive index at '"+archiveDir+"': "+strerror; return; except IOError as (errno, strerror): print "Failed to load archive index at '"+archiveDir+"': "+strerror; return;
def list(directory, pattern=None): if (os.path.exists(directory) and os.path.isdir(directory)): index = utilities.loadIndex(directory) if pattern is not None: #checks to see if a pattern has been given as argument pattern = pattern.lower() everythingIsFine = True for key in index: keyLower = key.lower() if pattern in keyLower: print key everythingIsFine = False if everythingIsFine: print "No matches found" else: #if no pattern given, prints out all the files in the directory for key in index: print key else: print "The backup archive has not been created! Use 'mybackup init' to initialise the directory before calling 'list'."