def init(directory): try: #try to create the directory, throw OSError otherwise objectFilePath = os.path.join(directory, 'objects') indexFilePath = os.path.join(directory, 'index.txt') if not os.path.isdir(objectFilePath): #checks if the objects directory has been created. If not, creates directory os.makedirs(os.path.join(directory, 'objects')) if not os.path.exists(indexFilePath): #checks if the index file has been created. If not, creates file open(indexFilePath, 'a') utilities.saveIndex(directory, {}) print "Archive directory created with subdirectory 'objects' and file 'index.txt' in " + directory else: print "Backup already intialised" except OSError: #throws an OSError if something went wrong with the creation of directory or files print "Error: Could not create directories or files"
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; objectsDir = os.path.join(archiveDir, "objects"); for root, dirs, files in os.walk(dirToBackup): for name in files: try: backupFile(objectsDir, os.path.join(root, name), index, logger); except OSError as (errno, strerror): print "Failed to backup file '"+name+"': "+strerror; except IOError as (errno, strerror): print "Failed to backup file '"+name+"': "+strerror; utilities.saveIndex(archiveDir, index); else: print "The backup archive has not been created! Use 'mybackup init' to initialise the directory before calling 'store'."; 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):