diff --git a/src/run_tribler.py b/src/run_tribler.py index 4ad94400be8..9b64bc1ca9f 100644 --- a/src/run_tribler.py +++ b/src/run_tribler.py @@ -158,6 +158,7 @@ def init_boot_logger(): logger.info('Start Tribler Window') window = TriblerWindow(settings, + root_state_dir, api_port=api_port, api_key=api_key, run_core=True) diff --git a/src/tribler-core/run_tribler_upgrader.py b/src/tribler-core/run_tribler_upgrader.py index 38c4bcfcf56..4f383b8c1e7 100755 --- a/src/tribler-core/run_tribler_upgrader.py +++ b/src/tribler-core/run_tribler_upgrader.py @@ -13,8 +13,7 @@ def upgrade_state_dir(root_state_dir: Path, update_status_callback=None, - interrupt_upgrade_event=None, - test_mode=False): + interrupt_upgrade_event=None): # Before any upgrade, prepare a separate state directory for the update version so it does not # affect the older version state directory. This allows for safe rollback. version_history = VersionHistory(root_state_dir) @@ -33,8 +32,7 @@ def upgrade_state_dir(root_state_dir: Path, upgrader = TriblerUpgrader(state_dir, channels_dir, primary_key, update_status_callback=update_status_callback, - interrupt_upgrade_event=interrupt_upgrade_event, - test_mode=test_mode) + interrupt_upgrade_event=interrupt_upgrade_event) upgrader.run() diff --git a/src/tribler-core/tribler_core/upgrade/upgrade.py b/src/tribler-core/tribler_core/upgrade/upgrade.py index 70717bd932c..f37f145aec5 100644 --- a/src/tribler-core/tribler_core/upgrade/upgrade.py +++ b/src/tribler-core/tribler_core/upgrade/upgrade.py @@ -2,7 +2,6 @@ import os import shutil from configparser import MissingSectionHeaderError, ParsingError -from time import sleep from ipv8.keyvault.private.libnaclkey import LibNaCLSK @@ -68,14 +67,12 @@ class TriblerUpgrader: def __init__(self, state_dir: Path, channels_dir: Path, trustchain_keypair: LibNaCLSK, interrupt_upgrade_event=None, - update_status_callback=None, - test_mode=False): + update_status_callback=None): self._logger = logging.getLogger(self.__class__.__name__) self.state_dir = state_dir self.channels_dir = channels_dir self.trustchain_keypair = trustchain_keypair self._update_status_callback = update_status_callback - self.test_mode = test_mode self.interrupt_upgrade_event = interrupt_upgrade_event self.failed = True @@ -96,15 +93,6 @@ def run(self): """ Run the upgrader if it is enabled in the config. """ - if self.test_mode: - self.update_status("STARTING UPGRADE") - count = 0 - while not self.shutting_down and count < 5: - count += 1 - self.update_status(f"Performing upgrade message {count}") - sleep(0.1) - return - self.upgrade_pony_db_8to10() self.upgrade_pony_db_10to11() convert_config_to_tribler76(self.state_dir) diff --git a/src/tribler-gui/tribler_gui/core_manager.py b/src/tribler-gui/tribler_gui/core_manager.py index 5ac4e838df4..39ed2059f86 100644 --- a/src/tribler-gui/tribler_gui/core_manager.py +++ b/src/tribler-gui/tribler_gui/core_manager.py @@ -1,5 +1,4 @@ import logging -import os import sys import time @@ -7,11 +6,9 @@ from PyQt5.QtNetwork import QNetworkRequest from PyQt5.QtWidgets import QApplication -from tribler_common.utilities import is_frozen - from tribler_gui.event_request_manager import EventRequestManager from tribler_gui.tribler_request_manager import TriblerNetworkRequest -from tribler_gui.utilities import connect, get_base_path +from tribler_gui.utilities import connect class CoreManager(QObject): @@ -23,15 +20,12 @@ class CoreManager(QObject): tribler_stopped = pyqtSignal() core_state_update = pyqtSignal(str) - def __init__(self, api_port, api_key, error_handler): + def __init__(self, root_state_dir, api_port, api_key, error_handler): QObject.__init__(self, None) self._logger = logging.getLogger(self.__class__.__name__) - self.base_path = get_base_path() - if not is_frozen(): - self.base_path = os.path.join(get_base_path(), "..") - + self.root_state_dir = root_state_dir self.core_process = None self.api_port = api_port self.api_key = api_key @@ -105,6 +99,7 @@ def start_tribler_core(self, core_args=None, core_env=None): core_env = QProcessEnvironment.systemEnvironment() core_env.insert("CORE_API_PORT", f"{self.api_port}") core_env.insert("CORE_API_KEY", self.api_key.decode('utf-8')) + core_env.insert("TSTATEDIR", str(self.root_state_dir)) if not core_args: core_args = sys.argv + ['--core'] diff --git a/src/tribler-gui/tribler_gui/tests/test_gui.py b/src/tribler-gui/tribler_gui/tests/test_gui.py index a21248d786b..070bdcf26c8 100644 --- a/src/tribler-gui/tribler_gui/tests/test_gui.py +++ b/src/tribler-gui/tribler_gui/tests/test_gui.py @@ -3,7 +3,7 @@ import sys from pathlib import Path -from PyQt5.QtCore import QMetaObject, QPoint, QProcessEnvironment, QSettings, QTimer, Q_ARG, Qt +from PyQt5.QtCore import QMetaObject, QPoint, QSettings, QTimer, Q_ARG, Qt from PyQt5.QtGui import QKeySequence, QPixmap, QRegion from PyQt5.QtTest import QTest from PyQt5.QtWidgets import QApplication, QListWidget, QTableView, QTextEdit, QTreeWidget, QTreeWidgetItem @@ -42,19 +42,15 @@ def api_port(): def window(api_port, tmpdir_factory): api_key = hexlify(os.urandom(16)).encode('utf-8') - core_env = QProcessEnvironment.systemEnvironment() - core_env.insert("CORE_API_PORT", f"{api_port}") - core_env.insert("CORE_API_KEY", api_key.decode('utf-8')) - core_env.insert("TSTATEDIR", str(tmpdir_factory.mktemp('tribler_state_dir'))) + root_state_dir = str(tmpdir_factory.mktemp('tribler_state_dir')) app = TriblerApplication("triblerapp-guitest", sys.argv) window = TriblerWindow( - settings=QSettings(), + QSettings(), + root_state_dir, api_port=api_port, api_key=api_key, - core_env=core_env, core_args=[str(RUN_TRIBLER_PY.absolute()), '--gui_test_mode'], - test_mode=True, ) # pylint: disable=W0621 app.set_activation_window(window) QTest.qWaitForWindowExposed(window) @@ -62,8 +58,7 @@ def window(api_port, tmpdir_factory): screenshot(window, name="tribler_loading") wait_for_signal( window.core_manager.events_manager.tribler_started, - timeout=20, - flag=window.core_manager.events_manager.tribler_started, + flag=window.core_manager.events_manager.tribler_started_flag, ) window.downloads_page.can_update_items = True yield window diff --git a/src/tribler-gui/tribler_gui/tribler_window.py b/src/tribler-gui/tribler_gui/tribler_window.py index 063d18d088a..78439e4229c 100644 --- a/src/tribler-gui/tribler_gui/tribler_window.py +++ b/src/tribler-gui/tribler_gui/tribler_window.py @@ -39,9 +39,9 @@ from psutil import LINUX from tribler_common.network_utils import NetworkUtils -from tribler_common.osutils import get_root_state_directory from tribler_common.process_checker import ProcessChecker from tribler_common.utilities import uri_to_path +from tribler_common.version_manager import VersionHistory from tribler_core.utilities.unicode import hexlify from tribler_core.version import version_id @@ -125,7 +125,14 @@ class TriblerWindow(QMainWindow): received_search_completions = pyqtSignal(object) def __init__( - self, settings, core_args=None, core_env=None, api_port=None, api_key=None, run_core=True, test_mode=False + self, + settings, + root_state_dir, + core_args=None, + core_env=None, + api_port=None, + api_key=None, + run_core=True, ): QMainWindow.__init__(self) self._logger = logging.getLogger(self.__class__.__name__) @@ -136,6 +143,7 @@ def __init__( self.setWindowIcon(QIcon(QPixmap(get_image_path('tribler.png')))) + self.root_state_dir = Path(root_state_dir) self.gui_settings = settings api_port = api_port or int(get_gui_setting(self.gui_settings, "api_port", DEFAULT_API_PORT)) api_key = api_key or get_gui_setting(self.gui_settings, "api_key", hexlify(os.urandom(16)).encode('utf-8')) @@ -154,8 +162,9 @@ def __init__( self.core_env = core_env self.error_handler = ErrorHandler(self) - self.core_manager = CoreManager(api_port, api_key, self.error_handler) - self.upgrade_manager = UpgradeManager(test_mode=test_mode) + self.core_manager = CoreManager(self.root_state_dir, api_port, api_key, self.error_handler) + self.version_history = VersionHistory(self.root_state_dir) + self.upgrade_manager = UpgradeManager(self.version_history) self.pending_requests = {} self.pending_uri_requests = [] self.dialog = None @@ -221,7 +230,7 @@ def on_state_update(new_state): self.core_manager.events_manager.received_remote_query_results, self.search_results_page.received_remote_results.emit, ) - self.settings_page.initialize_settings_page() + self.settings_page.initialize_settings_page(version_history=self.version_history) self.downloads_page.initialize_downloads_page() self.loading_page.initialize_loading_page() self.discovering_page.initialize_discovering_page() @@ -1137,8 +1146,7 @@ def dropEvent(self, e): e.accept() def clicked_force_shutdown(self): - root_state_dir = get_root_state_directory() - process_checker = ProcessChecker(root_state_dir) + process_checker = ProcessChecker(self.root_state_dir) if process_checker.already_running: core_pid = process_checker.get_pid_from_lock_file() os.kill(int(core_pid), 9) diff --git a/src/tribler-gui/tribler_gui/upgrade_manager.py b/src/tribler-gui/tribler_gui/upgrade_manager.py index 7bfbbfda17e..eaa94f570b4 100644 --- a/src/tribler-gui/tribler_gui/upgrade_manager.py +++ b/src/tribler-gui/tribler_gui/upgrade_manager.py @@ -4,7 +4,6 @@ from PyQt5.QtCore import QObject, QThread, pyqtSignal from PyQt5.QtWidgets import QMessageBox -from tribler_common.osutils import get_root_state_directory from tribler_common.simpledefs import UpgradeInterruptedEvent from tribler_common.version_manager import TriblerVersion, VersionHistory @@ -16,10 +15,9 @@ class StateDirUpgradeWorker(QObject): status_update = pyqtSignal(str) stop_upgrade = pyqtSignal() - def __init__(self, root_state_dir, test_mode=False): + def __init__(self, root_state_dir): super().__init__() self.root_state_dir = root_state_dir - self.test_mode = test_mode self._upgrade_interrupted_event = UpgradeInterruptedEvent() connect(self.stop_upgrade, self._stop_upgrade) from run_tribler_upgrader import upgrade_state_dir @@ -37,7 +35,6 @@ def run(self): self.root_state_dir, update_status_callback=self._update_status_callback, interrupt_upgrade_event=self._upgrade_interrupted_event, - test_mode=self.test_mode, ) self.finished.emit() @@ -50,15 +47,12 @@ class UpgradeManager(QObject): upgrader_tick = pyqtSignal(str) upgrader_finished = pyqtSignal() - def __init__(self, test_mode=False): + def __init__(self, version_history: VersionHistory): QObject.__init__(self, None) - self.root_state_dir = get_root_state_directory() - self.version_history = VersionHistory(self.root_state_dir) + self.version_history = version_history self._logger = logging.getLogger(self.__class__.__name__) - self.test_mode = test_mode - self._upgrade_worker = None self._upgrade_thread = None @@ -114,7 +108,7 @@ def start(self): if self.version_history.code_version.should_be_copied: self.upgrader_tick.emit(tr('Backing up state directory, please wait')) - self._upgrade_worker = StateDirUpgradeWorker(self.root_state_dir, test_mode=self.test_mode) + self._upgrade_worker = StateDirUpgradeWorker(self.version_history.root_state_dir) self._upgrade_thread = QThread() self._upgrade_worker.moveToThread(self._upgrade_thread) connect(self._upgrade_thread.started, self._upgrade_worker.run) diff --git a/src/tribler-gui/tribler_gui/widgets/settingspage.py b/src/tribler-gui/tribler_gui/widgets/settingspage.py index d5cfec59dcb..05059aa9d5d 100644 --- a/src/tribler-gui/tribler_gui/widgets/settingspage.py +++ b/src/tribler-gui/tribler_gui/widgets/settingspage.py @@ -6,7 +6,7 @@ from tribler_common.osutils import get_root_state_directory from tribler_common.sentry_reporter.sentry_mixin import AddBreadcrumbOnShowMixin from tribler_common.simpledefs import MAX_LIBTORRENT_RATE_LIMIT -from tribler_common.version_manager import VersionHistory, remove_state_dirs +from tribler_common.version_manager import remove_state_dirs from tribler_gui.defs import ( DARWIN, @@ -37,16 +37,18 @@ class SettingsPage(AddBreadcrumbOnShowMixin, QWidget): """ This class is responsible for displaying and adjusting the settings present in Tribler. """ + settings_edited = pyqtSignal() def __init__(self): QWidget.__init__(self) self.settings = None - self.version_history = VersionHistory(get_root_state_directory()) + self.version_history = None self.lang_list = sorted([lang_name for lang_name, lang_code in AVAILABLE_TRANSLATIONS.items()]) self.lang_list.insert(0, tr("System default")) - def initialize_settings_page(self): + def initialize_settings_page(self, version_history): + self.version_history = version_history if DARWIN: self.window().minimize_to_tray_checkbox.setHidden(True) self.window().settings_tab.initialize() @@ -254,12 +256,10 @@ def load_settings_data_tab(self): self.refresh_old_version_checkboxes() def refresh_current_version_checkbox(self): - get_root_state_directory() code_version_dir = self.version_history.code_version.directory self.refresh_version_checkboxes(self.window().state_dir_current, [code_version_dir], enabled=False) def refresh_old_version_checkboxes(self): - get_root_state_directory() old_state_dirs = self.version_history.get_disposable_state_directories() self.refresh_version_checkboxes(self.window().state_dir_list, old_state_dirs, enabled=True)