Skip to content

Commit

Permalink
Core Manager arguments fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Nov 19, 2021
1 parent 11c7f22 commit 877336e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/run_tribler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/tribler-core/run_tribler_upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()


Expand Down
14 changes: 1 addition & 13 deletions src/tribler-core/tribler_core/upgrade/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import shutil
from configparser import MissingSectionHeaderError, ParsingError
from time import sleep

from ipv8.keyvault.private.libnaclkey import LibNaCLSK

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions src/tribler-gui/tribler_gui/core_manager.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import logging
import os
import sys
import time

from PyQt5.QtCore import QObject, QProcess, QProcessEnvironment, QTimer, pyqtSignal
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):
Expand All @@ -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
Expand Down Expand Up @@ -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']

Expand Down
15 changes: 5 additions & 10 deletions src/tribler-gui/tribler_gui/tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,28 +42,23 @@ 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)

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
Expand Down
22 changes: 15 additions & 7 deletions src/tribler-gui/tribler_gui/tribler_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__)
Expand All @@ -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'))
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions src/tribler-gui/tribler_gui/upgrade_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/tribler-gui/tribler_gui/widgets/settingspage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 877336e

Please sign in to comment.