From 4da27cfe3e9fecbcf90647347be266aa4fd72ac9 Mon Sep 17 00:00:00 2001 From: Samir Aguiar Date: Wed, 22 Apr 2015 10:15:32 +0200 Subject: [PATCH] Add a callback function to the restore process By having a function called for every file that's about to be restore, we can keep track of the progress of the extraction. --- deltatar/deltatar.py | 15 +++++++++++---- 1 files changed, 11 insertions(+), 4 deletions(-) diff --git a/deltatar/deltatar.py b/deltatar/deltatar.py index 6f4984c..8d1df90 100644 --- a/deltatar/deltatar.py +++ b/deltatar/deltatar.py @@ -1181,7 +1181,7 @@ class DeltaTar(object): tarobj.close() def restore_backup(self, target_path, backup_indexes_paths=[], - backup_tar_path=None): + backup_tar_path=None, restore_callback=None): ''' Restores a backup. @@ -1196,6 +1196,8 @@ class DeltaTar(object): to backup_indexes_paths to restore directly from a tar file without using any file index. If it's a multivol tarfile, volume_name_func will be called. + - restore_callback: callback function to be called during restore. + This is passed to the helper and gets called for every file NOTE: If you want to use an index to restore a backup, this function only supports to do so when the tarfile mode is either uncompressed or @@ -1295,7 +1297,7 @@ class DeltaTar(object): continue try: self.logger.debug("restore %s" % ipath['path']) - helper.restore(ipath, l_no) + helper.restore(ipath, l_no, restore_callback) except Exception as e: self.logger.error("FAILED to restore: " + ipath.get('path', '')) continue @@ -1313,7 +1315,7 @@ class DeltaTar(object): if ipath['type'] != 'directory' or ipath['path'].startswith('delete://'): helper.delete(upath) self.logger.debug("restore %s" % ipath['path']) - helper.restore(ipath, l_no) + helper.restore(ipath, l_no, restore_callback) # if the file is not in the index (so it comes from the target # directory) then we have to delete it @@ -1455,13 +1457,18 @@ class RestoreHelper(object): # now we restore parent_directory mtime os.utime(parent_dir, (parent_dir_mtime, parent_dir_mtime)) - def restore(self, itpath, l_no): + def restore(self, itpath, l_no, callback=None): ''' Restore the path from the appropiate backup. Receives the current path from the first index iterator. itpath must be not null. + callback is a custom function that gets called for every file ''' path = itpath['path'] + # Calls the callback function + if callback: + callback() + if path.startswith('delete://'): # the file has previously been deleted already in restore_backup in # all cases so we just need to finish -- 1.7.1