Skip to content

Commit 61c0c08

Browse files
committed
Fix _MEIPASS / sys.frozen handling
1 parent 1735909 commit 61c0c08

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

Diff for: src/tribler/core/logger/logger.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import yaml
77

8+
from tribler.core.utilities.install_dir import get_core_path
9+
810
LOG_CONFIG_FILENAME = 'logger.yaml'
911

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

3840

3941
def get_logger_config_path():
40-
if not hasattr(sys, '_MEIPASS'):
41-
dirname = Path(__file__).absolute().parent
42-
else:
43-
dirname = Path(getattr(sys, '_MEIPASS')) / "tribler_source/tribler/core/logger"
44-
return dirname / LOG_CONFIG_FILENAME
42+
return get_core_path() / 'logger' / LOG_CONFIG_FILENAME
4543

4644

4745
def setup_logging(app_mode, log_dir: Path, config_path: Path):

Diff for: src/tribler/core/logger/tests/test_logger.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from io import BytesIO, TextIOWrapper
23
from unittest.mock import MagicMock, Mock, call, patch
34

@@ -6,15 +7,22 @@
67
from tribler.core.utilities.path_util import Path
78

89

9-
@patch('tribler.core.logger.logger.__file__', '/a/b/c/logger.py')
10+
@patch('tribler.core.__file__', 'xyz/tribler/core/__init__.py')
1011
def test_get_logger_config_path():
1112
config_path = get_logger_config_path()
1213
# take the last part of the path to ignore a drive name on Windows
13-
assert config_path.parts[-4:] == ('a', 'b', 'c', 'logger.yaml')
14+
assert config_path.parts[-4:] == ('tribler', 'core', 'logger', 'logger.yaml')
15+
16+
win_prefix = '//?/' if sys.platform.startswith('win') else '' # added by Path.fix_win_long_file in get_base_path
17+
18+
with patch('sys.frozen', True, create=True):
19+
with patch('sys.executable', '/a/b/c/tribler.exe', create=True):
20+
config_path = get_logger_config_path()
21+
assert config_path == Path(win_prefix + '/a/b/c/tribler_source/tribler/core/logger/logger.yaml')
1422

1523
with patch('sys._MEIPASS', '/x/y/z/', create=True):
1624
config_path = get_logger_config_path()
17-
assert config_path == Path('/x/y/z/tribler_source/tribler/core/logger/logger.yaml')
25+
assert config_path == Path(win_prefix + '/x/y/z/tribler_source/tribler/core/logger/logger.yaml')
1826

1927

2028
@patch('logging.basicConfig')

Diff for: src/tribler/core/utilities/install_dir.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
"""
66
import sys
77

8-
import tribler.core
8+
import tribler
99
from tribler.core.utilities.path_util import Path
1010
from tribler.core.utilities.utilities import is_frozen
1111

1212

1313
def get_base_path():
14-
""" Get absolute path to resource, works for dev and for PyInstaller """
14+
""" Get absolute path to resource, works for dev and for PyInstaller/cx_freeze"""
1515
try:
1616
# PyInstaller creates a temp folder and stores path in _MEIPASS
17-
base_path = Path(sys._MEIPASS)
18-
except Exception:
19-
base_path = Path(tribler.core.__file__).parent
17+
base_path = Path(getattr(sys, '_MEIPASS'))
18+
except AttributeError:
19+
if getattr(sys, 'frozen', False): # cx_freeze
20+
base_path = Path(sys.executable).parent
21+
else:
22+
base_path = Path(tribler.__file__).parent
2023

2124
fixed_filename = Path.fix_win_long_file(base_path)
2225
return Path(fixed_filename)
@@ -25,4 +28,4 @@ def get_base_path():
2528
def get_core_path():
2629
if is_frozen():
2730
return get_base_path() / 'tribler_source/tribler/core'
28-
return get_base_path()
31+
return get_base_path() / 'core'

Diff for: src/tribler/core/utilities/utilities.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,13 @@ def is_frozen():
195195
"""
196196
Return whether we are running in a frozen environment
197197
"""
198-
try:
199-
# PyInstaller creates a temp folder and stores path in _MEIPASS
200-
sys._MEIPASS # pylint: disable=protected-access
201-
except Exception: # pylint: disable=broad-except
202-
return False
203-
return True
198+
if hasattr(sys, '_MEIPASS'):
199+
return True # PyInstaller creates a temp folder and stores path in _MEIPASS
200+
201+
if getattr(sys, 'frozen', False):
202+
return True # cx_freeze creates 'frozen' attribute
203+
204+
return False
204205

205206

206207
fts_query_re = re.compile(r'\w+', re.UNICODE)

Diff for: src/tribler/gui/utilities.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import tribler.gui
2929
from tribler.core.components.knowledge.db.knowledge_db import ResourceType
30+
from tribler.core.utilities.install_dir import get_base_path
31+
from tribler.core.utilities.utilities import is_frozen
3032
from tribler.gui.defs import CORRUPTED_DB_WAS_FIXED_MESSAGE, HEALTH_DEAD, HEALTH_GOOD, HEALTH_MOOT, HEALTH_UNCHECKED
3133

3234
# fmt: off
@@ -195,13 +197,10 @@ def duration_to_string(seconds):
195197

196198

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

206205

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

0 commit comments

Comments
 (0)