def run(self): self.log('started AccessWalletTask') ok = need_backup = False error_message = None try: self.manager = BitcoinManager(self.log_name) self.manager.update_menu(constants.DISABLE_ITEM) if not os.path.exists(self.manager.data_dir): os.makedirs(self.manager.data_dir) ok, error_message = self.run_qt() if ok: need_backup = bitcoin_utils.need_to_backup(self.manager.data_dir, self.current_block) if need_backup: self.log('need to backup') self.manager.update_location(constants.BACKUP_URL) else: self.log('continuing to update blockchain') self.manager.update_location(constants.SYNC_URL) else: # don't allow any more backups until the user tells us it's ok state.set_backups_enabled(False) if error_message is None: self.log('unexpected error') if DEBUG: notice = format_exc() else: notice = self.BITCOIN_QT_UNEXPECTED_ERROR self.manager.update_progress('{} {}'.format( notice, bitcoin_utils.get_ok_button())) else: self.log('bitcoin-qt error') notice_and_button = '{}{}'.format( constants.RESTORE_BITCOIN, bitcoin_utils.get_ok_button()) self.manager.update_header(self.BITCOIN_QT_ERROR) self.manager.update_notice(notice_and_button) self.manager.update_subnotice(error_message) self.manager.update_menu(constants.ENABLE_ITEM) except Exception: need_backup = False error = format_exc() self.log(error) if self.manager: if DEBUG: self.manager.update_progress(error) else: self.manager.update_progress(self.BITCOIN_QT_UNEXPECTED_ERROR) self.manager.update_menu(constants.ENABLE_ITEM) self.log('finished AccessWalletTask')
def test_backup_when_backups_disabled(self): ''' Test trying to back up when backups disabled. ''' state.set_backups_enabled(False) request = self.factory.get('/bitcoin/backup/') response = views.Backup.as_view()(request) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/bitcoin/change_backup_status/') self.assertFalse(is_backup_running()) state.set_backups_enabled(True)
def post_page(self, request): backups_enabled = ('enable-backups-button' in request.POST or 'leave-backups-enabled-button' in request.POST) if state.get_backups_enabled() != backups_enabled: state.set_backups_enabled(backups_enabled) if backups_enabled: messages.success(request, BACKUPS_ENABLED) else: messages.success(request, BACKUPS_DISABLED) # send them back to the home page response = HttpResponseRedirect('/') return response
def test_change_backup_status(self): ''' Test changing the backup status. ''' state.set_backups_enabled(True) request = self.factory.get('/bitcoin/change_backup_status/') response = views.ChangeBackupStatus.as_view()(request) self.assertEqual(response.status_code, 200) self.assertTrue(b"<title>\nBackups Enabled | Blockchain Backup\n</title>" in response.content) self.assertTrue(b'Disable backups' in response.content) self.assertTrue(b'Leave backups enabled' in response.content) state.set_backups_enabled(False) request = self.factory.get('/bitcoin/change_backup_status/') response = views.ChangeBackupStatus.as_view()(request) self.assertEqual(response.status_code, 200) self.assertTrue(b"<title>\nBackups Disabled | Blockchain Backup\n</title>" in response.content) self.assertTrue(b'Leave backups disabled' in response.content) self.assertTrue(b'Enable backups' in response.content)
def test_interrupt_update(self): ''' Interrupt the update. ''' set_backups_enabled(True) update_task = UpdateTask() self.assertFalse(update_task is None) self.assertFalse(update_task.is_interrupted()) update_task.interrupt() self.assertTrue(update_task.is_interrupted()) request = self.factory.get('/bitcoin/update/') response = Update.as_view()(request) self.assertEqual(response.status_code, 200) sleep(60) request = self.factory.get('/bitcoin/interrupt_update/') InterruptUpdate.as_view()(request)
def restore(self): ''' Restore blockchain from newest backup, or the backup the user selected. >>> from blockchain_backup.bitcoin.tests import utils as test_utils >>> test_utils.init_database() >>> restore_task = RestoreTask(os.path.join(gettempdir(), 'bitcoin/data/testnet3/backups/level1')) >>> restore_task.manager = BitcoinManager(restore_task.log_name) >>> restore_task.restore() True >>> restore_task.is_interrupted() False ''' ok = True self.log(f'starting restoration from {self.restore_dir}') self.manager.update_menu(constants.DISABLE_ITEM) bitcoin_utils.check_for_updates(force=True, reason='restore') ok = self.restore_files_and_dirs() if ok and not self.is_interrupted(): # pass the args because delete_extra_files is recursive ok = self.delete_extra_files(self.restore_dir, self.manager.data_dir) if ok and not self.is_interrupted(): ok = self.restore_metadata() if ok and not self.is_interrupted(): self.restore_bcb_state() if not self.is_interrupted(): if ok: # we don't want to warn that a backup is needed # just after we restore from a backup set_last_backed_up_time(now()) else: self.remove_last_updated_file() if self.is_interrupted(): self.log('restore stopped by user') self.manager.update_header(self.RESTORE_STOPPED) self.manager.update_notice(self.RESTORE_WARNING) self.manager.update_subnotice(' ') else: if ok: self.log('finished bulk restore') state.set_backups_enabled(False) self.log( 'stopping backups after restore until user verifies everything ok' ) self.manager.update_header(self.RESTORE_DONE) self.manager.notify_done(notice=self.RESTORE_FINISHED) self.manager.update_subnotice(self.RESTORE_BACKUPS_OK) else: self.manager.update_notice(self.RESTORE_WARNING) self.manager.update_subnotice(' ') self.manager.update_menu(constants.ENABLE_ITEM) return ok