示例#1
0
    def run(self):
        '''
            Start the backup task.

            >>> from blockchain_backup.bitcoin.tests import utils as test_utils
            >>> test_utils.init_database()
            >>> backup_task = BackupTask()
            >>> backup_task.run()
            True
            >>> backup_task.is_interrupted()
            False
        '''

        self.log('started BackupTask')

        ok = True
        try:
            self.manager = BitcoinManager(self.log_name)
            self.manager.update_menu(constants.DISABLE_ITEM)

            # be sure to check for updates regularly
            check_for_updates()

            ok = self.backup()
            if self.is_interrupted():
                self.interrupt_backup()

            else:
                if ok:
                    self.manager.update_header(backup_utils.FINISHED)
                    self.manager.update_notice(backup_utils.WALLET_REMINDER)
                    self.manager.update_subnotice('')
                    self.manager.update_menu(constants.ENABLE_ITEM)

                    self.log('starting to update the blockchain')
                    self.manager.update_location(constants.SYNC_URL)
                else:
                    notice_and_button = '{}{}'.format(
                        constants.CLOSE_WINDOW_NOW, get_ok_button())
                    self.manager.update_header(backup_utils.HEADER_ERROR)
                    self.manager.update_notice(notice_and_button)
                    self.manager.update_subnotice(backup_utils.CONTACT_US)
                    self.manager.update_progress('')
                    self.manager.update_menu(constants.ENABLE_ITEM)

        except:  # 'bare except' because it catches more than "except Exception"
            ok = False
            error = format_exc()
            self.log(error)
            if self.manager:
                self.manager.update_notice(backup_utils.UNEXPECTED_ERROR)
                if DEBUG:
                    self.manager.update_progress(error)
                else:
                    self.manager.update_progress(' ')
                self.manager.update_menu(constants.ENABLE_ITEM)

        self.log('finished BackupTask')

        return ok
示例#2
0
    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')
示例#3
0
    def run(self):
        '''
            Start the update task.

            >>> from blockchain_backup.bitcoin.tests import utils as test_utils
            >>> test_utils.init_database()
            >>> update_task = UpdateTask()
            >>> update_task.run()
            >>> update_task.is_interrupted()
            False
        '''

        self.log('started UpdateTask')
        try:
            need_backup = False
            ok = False
            error = None

            self.manager = BitcoinManager(self.log_name)

            if not os.path.exists(self.manager.data_dir):
                os.makedirs(self.manager.data_dir)

            try:
                if self.is_interrupted():
                    ok = True
                    self.log('self interrupted before update started')
                else:
                    # don't start the update if a backup needs to be run
                    UNKNOWN_BLOCKS = -1
                    if need_to_backup(self.manager.data_dir, UNKNOWN_BLOCKS):
                        ok = True
                        need_backup = True
                    else:
                        ok, need_backup = self.update()
            except Exception:
                self.log(format_exc())

            if self.current_block > 0:
                state.set_last_block_updated(self.current_block)

            self.manager.update_menu(constants.ENABLE_ITEM)
            if need_backup and not self.is_interrupted():
                self.log('starting backup')
                self.manager.update_location(constants.BACKUP_URL)
            else:
                self.report_update_stopped(ok, error)

        except:  # 'bare except' because it catches more than "except Exception"
            ok = False
            need_backup = False
            self.log(format_exc())

        self.log('ended UpdateTask')
示例#4
0
def check_bitcoin_log(is_app_running_func=None):
    '''
        Check bitcoin log to see if app shutdown properly.

        >>> check_bitcoin_log(bitcoin_utils.is_bitcoind_running)
        (True, None)
    '''
    from blockchain_backup.bitcoin.manager import BitcoinManager

    manager = BitcoinManager(os.path.basename(get_log_path()), use_fresh_debug_log=False)
    log(f'checking {get_log_path()} for errors')

    shutdown, error_message = manager.check_bitcoin_log(is_app_running_func)
    log(f'shutdown: {shutdown}')
    log(f'error_message: {error_message}')

    return shutdown, error_message
示例#5
0
    def run(self):
        '''
            Start the restore task.

            >>> 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.run()
            True
            >>> restore_task.is_interrupted()
            False
            >>> restore_task = RestoreTask('/bad/bitcoin/data/testnet3/backups/level1')
            >>> restore_task.run()
            False
            >>> restore_task.is_interrupted()
            False
        '''
        ok = False
        try:
            self.log('started RestoreTask')

            self.manager = BitcoinManager(self.log_name)

            if os.path.exists(self.manager.data_dir) and os.path.exists(
                    self.restore_dir):
                ok = self.restore()
                if self.is_interrupted():
                    self.interrupt_restore()
                self.log('finished RestoreTask')
            else:
                ok = False
                self.log(
                    f'data dir exists: {os.path.exists(self.manager.data_dir)}'
                )
                self.log(f'restore exists: {os.path.exists(self.restore_dir)}')
                self.log('RestoreTask terminated')
                self.manager.update_progress(self.RESTORE_UNEXPECTED_ERROR)
                self.manager.update_menu(constants.ENABLE_ITEM)

        except FileNotFoundError as fne:
            FILE_NOT_FOUND_PREFIX = '[Errno 2] No such file or directory: '
            ok = False
            error = str(fne)
            i = error.find(FILE_NOT_FOUND_PREFIX)
            if i >= 0:
                error = error[len(FILE_NOT_FOUND_PREFIX):]
            self.log(f'file not found: {error}')
            self.remove_last_updated_file()
            if self.manager:
                self.manager.update_notice(error)
                self.manager.update_progress(self.RESTORE_UNEXPECTED_ERROR)
                self.manager.update_menu(constants.ENABLE_ITEM)

        except:  # 'bare except' because it catches more than "except Exception"
            ok = False
            self.log(format_exc())
            if self.manager:
                self.manager.update_progress(self.RESTORE_UNEXPECTED_ERROR)
                self.manager.update_menu(constants.ENABLE_ITEM)

        return ok