def restore(configuration, path=''): """Handle the back-up restoration command. :param configuration: Configuration :param path: str :return: bool """ confirm_label = 'Restore my back-up, possibly overwriting newer files.' confirm_question = 'Restoring back-ups may result in (newer) files on the source location being overwritten by (older) files from your back-ups. Confirm that this is indeed your intention.' if configuration.interactive and not ask_confirm( confirm_label, question=confirm_question): configuration.notifier.confirm('Aborting back-up restoration...') return True task.restore(configuration, path)
def test_restore_with_file_path(self, path): # Create the target directory. with TemporaryDirectory() as target_path: latest_path = os.path.join(target_path, 'latest') os.makedirs(latest_path) build_files_stage_1(latest_path) # Create the source directory. with TemporaryDirectory() as source_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = restore(configuration, path) self.assertTrue(result) with self.assertRaises(AssertionError): assert_paths_identical(self, source_path, os.path.join(target_path, 'latest')) assert_paths_identical( self, os.path.join(source_path, path), os.path.join(target_path, 'latest', path))
def test_restore_with_unavailable_target(self): # Create the source directory. with TemporaryDirectory() as source_path: # Create the target directory. with TemporaryDirectory() as target_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path) configuration.target = PathTarget( configuration.logger, configuration.notifier, target_path + '/NonExistentPath') result = restore(configuration) self.assertFalse(result)
def test_restore_with_subprocess_error(self, m): m.side_effect = subprocess.CalledProcessError(7, '') # Create the target directory. with TemporaryDirectory() as target_path: # Create the source directory. with TemporaryDirectory() as source_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = restore(configuration) self.assertFalse(result)
def test_restore_all(self): # Create the target directory. with TemporaryDirectory() as target_path: latest_path = os.path.join(target_path, 'latest') os.makedirs(latest_path) build_files_stage_1(latest_path) # Create the source directory. with TemporaryDirectory() as source_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = restore(configuration) self.assertTrue(result) subprocess.check_call(['ls', '-la', source_path]) assert_paths_identical(self, source_path, os.path.join(target_path, 'latest'))