Skip to content

Commit

Permalink
#433 Add a shim to patch the current issues with bottle with Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvollebregt committed Oct 7, 2023
1 parent 6b7d254 commit b7c15a6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions auto_py_to_exe/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import shutil
import tempfile

from . import shims
shims.install_shims()

from . import __version__
from . import config
from . import validation
Expand Down
38 changes: 38 additions & 0 deletions auto_py_to_exe/shims.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

from . import utils


def install_shims():
"""Install shims to fix version incompatibility"""
install_bottle_import_redirect_shim()


def install_bottle_import_redirect_shim():
"""
https://github.com/brentvollebregt/auto-py-to-exe/issues/433 explains that a ModuleNotFoundError is raised when trying
to import bottle extensions using Python 3.12.
This shim will patch this issue with some code that is currently on bottle's main branch.
This shim is only needed on Python versions >=3.12 and bottle versions <=0.12.25 (hoping the next version fixes this issue)
"""

# First check if setting this up is needed
if sys.version_info < (3, 12):
return

import bottle
if utils.parse_version_tuple(bottle.__version__) > (0, 12, 25):
return

if hasattr(bottle._ImportRedirect, 'find_spec'):
return

print(f'Warning: Installing shim for bottle import redirects (using Python={sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]} and bottle={bottle.__version__})')

# Add the shim
def find_spec(self, fullname, path, target=None):
if '.' not in fullname: return
if fullname.rsplit('.', 1)[0] != self.name: return
from importlib.util import spec_from_loader
return spec_from_loader(fullname, self)
bottle._ImportRedirect.find_spec = find_spec
9 changes: 8 additions & 1 deletion auto_py_to_exe/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os

import eel
from eel import chrome

from . import config
from . import utils
Expand Down Expand Up @@ -57,6 +58,12 @@ def __get_pyinstaller_options():
return options


def __can_use_chrome():
""" Identify if Chrome is available for Eel to use """
chrome_instance_path = chrome.find_path()
return chrome_instance_path is not None and os.path.exists(chrome_instance_path)


@eel.expose
def initialise():
""" Called by the UI when opened. Used to pass initial values and setup state we couldn't set until now. """
Expand Down Expand Up @@ -174,7 +181,7 @@ def send_message_to_ui_output(message):
def start(open_mode):
""" Start the UI using Eel """
try:
chrome_available = utils.can_use_chrome()
chrome_available = __can_use_chrome()
if open_mode == UIOpenMode.CHROME and chrome_available:
eel.start('index.html', size=(650, 701), port=0)
elif open_mode == UIOpenMode.USER_DEFAULT or (open_mode == UIOpenMode.CHROME and not chrome_available):
Expand Down
7 changes: 0 additions & 7 deletions auto_py_to_exe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import socket
import sys

from eel import chrome
from PyInstaller import __version__ as pyinstaller_version_string


Expand All @@ -19,12 +18,6 @@ def write(self, string):
return len(string)


def can_use_chrome():
""" Identify if Chrome is available for Eel to use """
chrome_instance_path = chrome.find_path()
return chrome_instance_path is not None and os.path.exists(chrome_instance_path)


def open_output_in_explorer(output_directory, input_filename, is_one_file):
""" Open the output in the local file explorer """
folder_directory = os.path.abspath(output_directory)
Expand Down

0 comments on commit b7c15a6

Please sign in to comment.