Skip to content

Commit

Permalink
hooks: Support for PyQt5/PySide2 QtWebEngine in macOS (#2127)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte committed Nov 13, 2023
1 parent e0122f8 commit 5b196bc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
38 changes: 32 additions & 6 deletions cx_Freeze/hooks/pyqt5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from contextlib import suppress
from textwrap import dedent

from cx_Freeze._compat import IS_CONDA
from cx_Freeze._compat import IS_CONDA, IS_MACOS
from cx_Freeze.common import get_resource_file_path
from cx_Freeze.finder import ModuleFinder
from cx_Freeze.module import Module
Expand Down Expand Up @@ -36,7 +36,7 @@
from .._qthooks import load_qt_qttest as load_pyqt5_qttest
from .._qthooks import load_qt_qtuitools as load_pyqt5_qtuitools
from .._qthooks import load_qt_qtwebengine as load_pyqt5_qtwebengine
from .._qthooks import load_qt_qtwebenginecore as load_pyqt5_qtwebenginecore
from .._qthooks import load_qt_qtwebenginecore as _load_qt_qtwebenginecore
from .._qthooks import (
load_qt_qtwebenginewidgets as load_pyqt5_qtwebenginewidgets,
)
Expand Down Expand Up @@ -71,19 +71,29 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
resource = get_resource_file_path("hooks/pyqt5", "resource", ".py")
finder.include_file_as_module(resource, "PyQt5._cx_freeze_resource")

# Include the optional qt.conf used by QtWebEngine (Prefix = ..)
# Include an optional qt.conf to be used by QtWebEngine (Prefix = ..)
copy_qt_files(finder, "PyQt5", "LibraryExecutablesPath", "qt.conf")

# Inject code to the end of init
code_string = module.file.read_text(encoding="utf_8")
code_string += dedent(
f"""
# cx_Freeze patch start
if {IS_CONDA}:
import os, sys
if {IS_CONDA}: # conda-forge linux, macos and windows
import PyQt5._cx_freeze_resource
elif {IS_MACOS}: # macos using 'pip install pyqt5'
# Support for QtWebEngine (bdist_mac differs from build_exe)
helpers = os.path.join(os.path.dirname(sys.frozen_dir), "Helpers")
if not os.path.isdir(helpers):
helpers = os.path.join(sys.frozen_dir, "share")
os.environ["QTWEBENGINEPROCESS_PATH"] = os.path.join(
helpers,
"QtWebEngineProcess.app/Contents/MacOS/QtWebEngineProcess"
)
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--single-process"
else:
# Support for QtWebEngine
import os
# Support for QtWebEngine (linux and windows using pip)
os.environ["QTWEBENGINE_DISABLE_SANDBOX"] = "1"
import PyQt5._cx_freeze_append_to_init
import PyQt5._cx_freeze_debug
Expand All @@ -107,6 +117,22 @@ def load_pyqt5_qtcore(
finder.include_module("PyQt5._qt")


def load_pyqt5_qtwebenginecore(finder: ModuleFinder, module: Module) -> None:
"""Include module dependency and QtWebEngineProcess files."""
_load_qt_qtwebenginecore(finder, module)
if IS_MACOS and not IS_CONDA:
# duplicate resource files
for source, target in finder.included_files[:]:
if any(
filter(source.match, ("Resources/*.pak", "Resources/*.dat"))
):
finder.include_files(
source,
target.parent.parent / target.name,
copy_dependent_files=False,
)


__all__ = [
"load_pyqt5",
"load_pyqt5_qt",
Expand Down
40 changes: 33 additions & 7 deletions cx_Freeze/hooks/pyside2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from textwrap import dedent

from cx_Freeze._compat import IS_CONDA, IS_MINGW
from cx_Freeze._compat import IS_CONDA, IS_MACOS, IS_MINGW
from cx_Freeze.common import (
code_object_replace_function,
get_resource_file_path,
Expand Down Expand Up @@ -37,7 +37,7 @@
from .._qthooks import load_qt_qttest as load_pyside2_qttest
from .._qthooks import load_qt_qtuitools as load_pyside2_qtuitools
from .._qthooks import load_qt_qtwebengine as load_pyside2_qtwebengine
from .._qthooks import load_qt_qtwebenginecore as load_pyside2_qtwebenginecore
from .._qthooks import load_qt_qtwebenginecore as _load_qt_qtwebenginecore
from .._qthooks import (
load_qt_qtwebenginewidgets as load_pyside2_qtwebenginewidgets,
)
Expand Down Expand Up @@ -68,24 +68,34 @@ def load_pyside2(finder: ModuleFinder, module: Module) -> None:
resource = get_resource_file_path("hooks/pyside2", "resource", ".py")
finder.include_file_as_module(resource, "PySide2._cx_freeze_resource")

# Include a qt.conf in the module path (Prefix = lib/PySide2) for msys2
if IS_MINGW:
# Include a qt.conf in the module path (Prefix = lib/PySide2)
qt_conf = get_resource_file_path("hooks/pyside2", "qt", ".conf")
finder.include_files(qt_conf, qt_conf.name)

# Include the optional qt.conf used by QtWebEngine (Prefix = ..)
# Include an optional qt.conf to be used by QtWebEngine (Prefix = ..)
copy_qt_files(finder, "PySide2", "LibraryExecutablesPath", "qt.conf")

# Inject code to init
code_string = module.file.read_text(encoding="utf_8")
code_string += dedent(
f"""
# cx_Freeze patch start
if {IS_CONDA}:
import os, sys
if {IS_CONDA}: # conda-forge linux, macos and windows
import PySide2._cx_freeze_resource
elif {IS_MACOS}: # macos using 'pip install pyside2'
# Support for QtWebEngine (bdist_mac differs from build_exe)
helpers = os.path.join(os.path.dirname(sys.frozen_dir), "Helpers")
if not os.path.isdir(helpers):
helpers = os.path.join(sys.frozen_dir, "share")
os.environ["QTWEBENGINEPROCESS_PATH"] = os.path.join(
helpers,
"QtWebEngineProcess.app/Contents/MacOS/QtWebEngineProcess"
)
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--single-process"
else:
# Support for QtWebEngine
import os
# Support for QtWebEngine (linux and windows using pip)
os.environ["QTWEBENGINE_DISABLE_SANDBOX"] = "1"
import PySide2._cx_freeze_debug
# cx_Freeze patch end
Expand All @@ -107,6 +117,22 @@ def {name}(package_dir):
module.code = code


def load_pyside2_qtwebenginecore(finder: ModuleFinder, module: Module) -> None:
"""Include module dependency and QtWebEngineProcess files."""
_load_qt_qtwebenginecore(finder, module)
if IS_MACOS and not IS_CONDA:
# duplicate resource files
for source, target in finder.included_files[:]:
if any(
filter(source.match, ("Resources/*.pak", "Resources/*.dat"))
):
finder.include_files(
source,
target.parent.parent / target.name,
copy_dependent_files=False,
)


__all__ = [
"load_pyside2",
"load_pyside2_qt",
Expand Down

0 comments on commit 5b196bc

Please sign in to comment.