From 6b6666c6187496be410a3fb02bf27feb87043389 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 10 Jul 2020 18:56:23 +0300 Subject: [PATCH] Properly handle resource loading and sys.frozen. --- src/bitmessageqt/widgets.py | 18 ++++++++++++++---- src/network/tls.py | 18 ++++++++++-------- src/paths.py | 11 +++++++---- src/pyelliptic/openssl.py | 2 ++ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/bitmessageqt/widgets.py b/src/bitmessageqt/widgets.py index e3232fe6..22426707 100644 --- a/src/bitmessageqt/widgets.py +++ b/src/bitmessageqt/widgets.py @@ -1,11 +1,21 @@ -from qtpy import uic -import os.path -import paths +import os +from io import BytesIO + +from qtpy import QtCore, uic + +from pybitmessage import paths def resource_path(resFile): baseDir = paths.codePath() - for subDir in ("ui", "bitmessageqt"): + if baseDir is None: # pyqtdeploy bundle + resFile = QtCore.QFile( + ':/pybitmessage/bitmessageqt/{}'.format(resFile)) + resFile.open(QtCore.QIODevice.ReadOnly) + data = resFile.readAll() + resFile.close() + return BytesIO(bytes(data)) + for subDir in ('bitmessageqt', 'ui'): path = os.path.join(baseDir, subDir, resFile) if os.path.isfile(path): return path diff --git a/src/network/tls.py b/src/network/tls.py index 1b325696..d5566b27 100644 --- a/src/network/tls.py +++ b/src/network/tls.py @@ -7,10 +7,11 @@ import socket import ssl import sys -import network.asyncore_pollchoose as asyncore -import paths -from network.advanceddispatcher import AdvancedDispatcher -from queues import receiveDataQueue +import asyncore_pollchoose as asyncore +from pybitmessage import paths +from pybitmessage.queues import receiveDataQueue +from advanceddispatcher import AdvancedDispatcher + logger = logging.getLogger('default') @@ -47,14 +48,15 @@ class TLSDispatcher(AdvancedDispatcher): def __init__(self, _=None, sock=None, certfile=None, keyfile=None, server_side=False, ciphers=sslProtocolCiphers): self.want_read = self.want_write = True - if certfile is None: + code_path = paths.codePath() + if certfile is None and code_path: self.certfile = os.path.join( - paths.codePath(), 'sslkeys', 'cert.pem') + code_path, 'sslkeys', 'cert.pem') else: self.certfile = certfile - if keyfile is None: + if keyfile is None and code_path: self.keyfile = os.path.join( - paths.codePath(), 'sslkeys', 'key.pem') + code_path, 'sslkeys', 'key.pem') else: self.keyfile = keyfile self.server_side = server_side diff --git a/src/paths.py b/src/paths.py index e2f8c97e..59f8593c 100644 --- a/src/paths.py +++ b/src/paths.py @@ -76,10 +76,13 @@ def codePath(): """Returns path to the program sources""" if not frozen: return os.path.dirname(__file__) - return ( - os.environ.get('RESOURCEPATH') - # pylint: disable=protected-access - if frozen == "macosx_app" else sys._MEIPASS) + if frozen == "macosx_app": + return os.getenv('RESOURCEPATH') + else: + try: # pylint: disable=protected-access + return sys._MEIPASS + except AttributeError: + return None def tail(f, lines=20): diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index accaaa94..e752926c 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -758,6 +758,8 @@ def loadOpenSSL(): environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.0.9.8.dylib'), ]) + elif not getattr(sys, '_MEIPASS', None): + pass elif 'win32' in sys.platform or 'win64' in sys.platform: libdir.append(path.join(sys._MEIPASS, 'libeay32.dll')) else: