Skip to content

Commit

Permalink
Fix _MEIPASS / sys.frozen handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Apr 24, 2024
1 parent 53621ea commit 6af13e0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/tribler/core/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import yaml

from tribler.core.utilities.install_dir import get_core_path

LOG_CONFIG_FILENAME = 'logger.yaml'

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -37,11 +39,7 @@ def load_logger_config(app_mode, log_dir, current_process_is_primary=True):


def get_logger_config_path():
if not hasattr(sys, '_MEIPASS'):
dirname = Path(__file__).absolute().parent
else:
dirname = Path(getattr(sys, '_MEIPASS')) / "tribler_source/tribler/core/logger"
return dirname / LOG_CONFIG_FILENAME
return get_core_path() / 'logger' / LOG_CONFIG_FILENAME


def setup_logging(app_mode, log_dir: Path, config_path: Path):
Expand Down
11 changes: 8 additions & 3 deletions src/tribler/core/logger/tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
from tribler.core.utilities.path_util import Path


@patch('tribler.core.logger.logger.__file__', '/a/b/c/logger.py')
@patch('tribler.core.__file__', 'xyz/tribler/core/__init__.py')
def test_get_logger_config_path():
config_path = get_logger_config_path()
# take the last part of the path to ignore a drive name on Windows
assert config_path.parts[-4:] == ('a', 'b', 'c', 'logger.yaml')
assert config_path.parts[-4:] == ('tribler', 'core', 'logger', 'logger.yaml')

with patch('sys.frozen', True, create=True):
with patch('sys.executable', '/a/b/c/tribler.exe', create=True):
config_path = get_logger_config_path()
assert config_path == Path('//?//a/b/c/tribler_source/tribler/core/logger/logger.yaml')

with patch('sys._MEIPASS', '/x/y/z/', create=True):
config_path = get_logger_config_path()
assert config_path == Path('/x/y/z/tribler_source/tribler/core/logger/logger.yaml')
assert config_path == Path('//?//x/y/z/tribler_source/tribler/core/logger/logger.yaml')


@patch('logging.basicConfig')
Expand Down
15 changes: 9 additions & 6 deletions src/tribler/core/utilities/install_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
"""
import sys

import tribler.core
import tribler
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.utilities import is_frozen


def get_base_path():
""" Get absolute path to resource, works for dev and for PyInstaller """
""" Get absolute path to resource, works for dev and for PyInstaller/cx_freeze"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = Path(sys._MEIPASS)
except Exception:
base_path = Path(tribler.core.__file__).parent
base_path = Path(getattr(sys, '_MEIPASS'))
except AttributeError:
if getattr(sys, 'frozen', False): # cx_freeze
base_path = Path(sys.executable).parent
else:
base_path = Path(tribler.__file__).parent

fixed_filename = Path.fix_win_long_file(base_path)
return Path(fixed_filename)
Expand All @@ -25,4 +28,4 @@ def get_base_path():
def get_core_path():
if is_frozen():
return get_base_path() / 'tribler_source/tribler/core'
return get_base_path()
return get_base_path() / 'core'
13 changes: 7 additions & 6 deletions src/tribler/core/utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,13 @@ def is_frozen():
"""
Return whether we are running in a frozen environment
"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
sys._MEIPASS # pylint: disable=protected-access
except Exception: # pylint: disable=broad-except
return False
return True
if hasattr(sys, '_MEIPASS'):
return True # PyInstaller creates a temp folder and stores path in _MEIPASS

if getattr(sys, 'frozen', False):
return True # cx_freeze creates 'frozen' attribute

return False


fts_query_re = re.compile(r'\w+', re.UNICODE)
Expand Down
13 changes: 6 additions & 7 deletions src/tribler/gui/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import tribler.gui
from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.utilities.install_dir import get_base_path
from tribler.core.utilities.utilities import is_frozen
from tribler.gui.defs import CORRUPTED_DB_WAS_FIXED_MESSAGE, HEALTH_DEAD, HEALTH_GOOD, HEALTH_MOOT, HEALTH_UNCHECKED

# fmt: off
Expand Down Expand Up @@ -195,13 +197,10 @@ def duration_to_string(seconds):


def get_gui_path():
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(tribler.gui.__file__)
return base_path
""" Get absolute path to resource, works for dev and for PyInstaller/cx_freeze"""
if is_frozen():
return get_base_path() / 'tribler_source/tribler/gui'
return get_base_path() / 'gui'


TRANSLATIONS_DIR = os.path.join(get_gui_path(), "i18n")
Expand Down

0 comments on commit 6af13e0

Please sign in to comment.