Skip to content

Commit

Permalink
hooks: the optimized mode is the default for pip installations (#2500)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte authored Jul 15, 2024
1 parent 91baad0 commit 129ea48
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 87 deletions.
15 changes: 4 additions & 11 deletions cx_Freeze/freezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from setuptools import Distribution

from cx_Freeze._compat import IS_CONDA, IS_MACOS, IS_MINGW, IS_WINDOWS
from cx_Freeze._compat import IS_MACOS, IS_MINGW, IS_WINDOWS
from cx_Freeze.common import get_resource_file_path, process_path_specs
from cx_Freeze.exception import FileError, OptionError
from cx_Freeze.executable import Executable
Expand Down Expand Up @@ -678,9 +678,7 @@ def _write_modules(self) -> None:
parts.append(module.file.name)
target_name = target_lib_dir.joinpath(*parts)
self._copy_file(
module.file,
target_name,
copy_dependent_files=True,
module.file, target_name, copy_dependent_files=True
)
else:
if module.path is not None:
Expand Down Expand Up @@ -756,9 +754,8 @@ def freeze(self) -> None:
)
finder.add_constant("_EXECUTABLES_NUMBER", len(self.executables))

# Write the modules baefore the included files to fix an issue
if IS_WINDOWS and not IS_CONDA:
self._write_modules()
# Write the modules
self._write_modules()

# Include user-defined files and hooks-defined files
target_dir = self.target_dir
Expand Down Expand Up @@ -786,10 +783,6 @@ def freeze(self) -> None:
fulltarget = target_dir / target_path
self._copy_file(source_path, fulltarget, copy_dependent_files)

# Write the modules after the included files to avoid duplicate files
if not (IS_WINDOWS and not IS_CONDA):
self._write_modules()

# do any platform-specific post-Freeze work
self._post_freeze_hook()

Expand Down
7 changes: 4 additions & 3 deletions cx_Freeze/hooks/_qthooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ def load_qt_qtvirtualkeyboard(finder: ModuleFinder, module: Module) -> None:
def load_qt_qtwebenginecore(finder: ModuleFinder, module: Module) -> None:
"""Include module dependency and QtWebEngineProcess files."""
name = _qt_implementation(module)
distribution = module.parent.distribution
environment = (distribution and distribution.installer) or "pip"

if IS_WINDOWS:
for filename in (
Expand All @@ -350,8 +352,7 @@ def load_qt_qtwebenginecore(finder: ModuleFinder, module: Module) -> None:
copy_qt_files(finder, name, "ArchDataPath", filename)
# pyqt5 - all files listed in LibraryExecutablesPath
copy_qt_files(finder, name, "LibraryExecutablesPath", filename)
elif IS_MACOS and not IS_CONDA:
# wheels for macOS
elif IS_MACOS and environment == "pip": # pip wheels for macOS
source_path, _ = get_qt_paths(name, "LibrariesPath")
source_framework = source_path / "QtWebEngineCore.framework"
# QtWebEngineProcess
Expand All @@ -377,7 +378,7 @@ def load_qt_qtwebenginecore(finder: ModuleFinder, module: Module) -> None:
copy_qt_files(
finder, name, "LibraryExecutablesPath", "QtWebEngineProcess"
)
if IS_CONDA: # conda-forge Linux and macOS
if environment == "conda": # conda-forge Linux and macOS
prefix = Path(sys.prefix)
conda_meta = prefix / "conda-meta"
pkg = next(conda_meta.glob("nss-*.json"))
Expand Down
22 changes: 16 additions & 6 deletions cx_Freeze/hooks/pyqt5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_CONDA, IS_MACOS
from cx_Freeze._compat import IS_MACOS
from cx_Freeze.common import get_resource_file_path
from cx_Freeze.hooks._qthooks import copy_qt_files
from cx_Freeze.hooks._qthooks import (
Expand Down Expand Up @@ -44,9 +44,17 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
"""Inject code in PyQt5 __init__ to locate and load plugins and
resources. Also, this fixes issues with conda-forge versions.
"""
# Activate an optimized mode when PyQt5 is in zip_include_packages
if module.in_file_system == 0:
# Activate the optimized mode by default in pip environments
if module.name in finder.zip_exclude_packages:
print(f"WARNING: zip_exclude_packages={module.name} ignored.")
if module.name in finder.zip_include_packages:
print(f"WARNING: zip_include_packages={module.name} ignored.")
distribution = module.distribution
environment = (distribution and distribution.installer) or "pip"
if environment == "pip":
module.in_file_system = 2
else:
module.in_file_system = 1

# Include a module that fix an issue
qt_debug = get_resource_file_path("hooks/pyqt5", "_append_to_init", ".py")
Expand All @@ -57,7 +65,7 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
finder.include_file_as_module(qt_debug, "PyQt5._cx_freeze_debug")

# Include a resource with qt.conf (Prefix = lib/PyQt5) for conda-forge
if IS_CONDA:
if environment == "conda":
resource = get_resource_file_path("hooks/pyqt5", "resource", ".py")
finder.include_file_as_module(resource, "PyQt5._cx_freeze_resource")

Expand All @@ -70,7 +78,7 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
f"""
# cx_Freeze patch start
import os, sys
if {IS_CONDA}: # conda-forge linux, macos and windows
if {environment == "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)
Expand Down Expand Up @@ -102,7 +110,9 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
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:
distribution = module.parent.distribution
environment = (distribution and distribution.installer) or "pip"
if IS_MACOS and environment == "pip":
# duplicate resource files
for source, target in finder.included_files[:]:
if any(
Expand Down
16 changes: 12 additions & 4 deletions cx_Freeze/hooks/pyqt6/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_CONDA, IS_MACOS, IS_MINGW
from cx_Freeze._compat import IS_MACOS, IS_MINGW
from cx_Freeze.common import get_resource_file_path
from cx_Freeze.hooks._qthooks import copy_qt_files
from cx_Freeze.hooks._qthooks import load_qt_qt3dinput as load_pyqt6_qt3dinput
Expand Down Expand Up @@ -74,9 +74,17 @@ def load_pyqt6(finder: ModuleFinder, module: Module) -> None:
"""Inject code in PyQt6 __init__ to locate and load plugins and
resources.
"""
# Activate an optimized mode when PyQt6 is in zip_include_packages
if module.in_file_system == 0:
# Activate the optimized mode by default in pip environments
if module.name in finder.zip_exclude_packages:
print(f"WARNING: zip_exclude_packages={module.name} ignored.")
if module.name in finder.zip_include_packages:
print(f"WARNING: zip_include_packages={module.name} ignored.")
distribution = module.distribution
environment = (distribution and distribution.installer) or "pip"
if environment == "pip":
module.in_file_system = 2
else:
module.in_file_system = 1

# Include modules that inject an optional debug code
qt_debug = get_resource_file_path("hooks/pyqt6", "debug", ".py")
Expand Down Expand Up @@ -106,7 +114,7 @@ def load_pyqt6(finder: ModuleFinder, module: Module) -> None:
code_string += dedent(
f"""
# cx_Freeze patch start
if {IS_MACOS} and not {IS_CONDA}: # conda does not support pyqt6
if {IS_MACOS} and {environment == "pip"}: # conda doesn't support pyqt6
import os, sys
# Support for QtWebEngine (bdist_mac differs from build_exe)
helpers = os.path.join(os.path.dirname(sys.frozen_dir), "Helpers")
Expand Down
24 changes: 18 additions & 6 deletions cx_Freeze/hooks/pyside2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_CONDA, IS_MACOS, IS_MINGW
from cx_Freeze._compat import IS_MACOS, IS_MINGW
from cx_Freeze.common import (
code_object_replace_function,
get_resource_file_path,
Expand Down Expand Up @@ -52,16 +52,24 @@ def load_pyside2(finder: ModuleFinder, module: Module) -> None:
"""Inject code in PySide2 __init__ to locate and load plugins and
resources. Also, this fixes issues with conda-forge versions.
"""
# Activate an optimized mode when PySide2 is in zip_include_packages
if module.in_file_system == 0:
# Activate the optimized mode by default in pip environments
if module.name in finder.zip_exclude_packages:
print(f"WARNING: zip_exclude_packages={module.name} ignored.")
if module.name in finder.zip_include_packages:
print(f"WARNING: zip_include_packages={module.name} ignored.")
distribution = module.distribution
environment = (distribution and distribution.installer) or "pip"
if environment == "pip":
module.in_file_system = 2
else:
module.in_file_system = 1

# Include a module that inject an optional debug code
qt_debug = get_resource_file_path("hooks/pyside2", "debug", ".py")
finder.include_file_as_module(qt_debug, "PySide2._cx_freeze_debug")

# Include a resource with qt.conf (Prefix = lib/PySide2) for conda-forge
if IS_CONDA:
if environment == "conda":
resource = get_resource_file_path("hooks/pyside2", "resource", ".py")
finder.include_file_as_module(resource, "PySide2._cx_freeze_resource")

Expand All @@ -79,7 +87,7 @@ def load_pyside2(finder: ModuleFinder, module: Module) -> None:
f"""
# cx_Freeze patch start
import os, sys
if {IS_CONDA}: # conda-forge linux, macos and windows
if {environment == "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)
Expand Down Expand Up @@ -108,6 +116,8 @@ def load_pyside2(finder: ModuleFinder, module: Module) -> None:

# shiboken2 in zip_include_packages
shiboken2 = finder.include_package("shiboken2")
if module.in_file_system == 2:
shiboken2.in_file_system = 0
if shiboken2.in_file_system == 0:
name = "_additional_dll_directories"
source = f"""\
Expand All @@ -123,7 +133,9 @@ def {name}(package_dir):
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:
distribution = module.parent.distribution
environment = (distribution and distribution.installer) or "pip"
if IS_MACOS and environment == "pip":
# duplicate resource files
for source, target in finder.included_files[:]:
if any(
Expand Down
20 changes: 15 additions & 5 deletions cx_Freeze/hooks/pyside6/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_CONDA, IS_MACOS, IS_MINGW
from cx_Freeze._compat import IS_MACOS, IS_MINGW
from cx_Freeze.common import (
code_object_replace_function,
get_resource_file_path,
Expand Down Expand Up @@ -81,16 +81,24 @@ def load_pyside6(finder: ModuleFinder, module: Module) -> None:
"""Inject code in PySide6 __init__ to locate and load plugins and
resources.
"""
# Activate an optimized mode when PySide6 is in zip_include_packages
if module.in_file_system == 0:
# Activate the optimized mode by default in pip environments
if module.name in finder.zip_exclude_packages:
print(f"WARNING: zip_exclude_packages={module.name} ignored.")
if module.name in finder.zip_include_packages:
print(f"WARNING: zip_include_packages={module.name} ignored.")
distribution = module.distribution
environment = (distribution and distribution.installer) or "pip"
if environment == "pip":
module.in_file_system = 2
else:
module.in_file_system = 1

# Include modules that inject an optional debug code
qt_debug = get_resource_file_path("hooks/pyside6", "debug", ".py")
finder.include_file_as_module(qt_debug, "PySide6._cx_freeze_qt_debug")

# Include a resource for conda-forge
if IS_CONDA:
if environment == "conda":
# The resource include a qt.conf (Prefix = lib/PySide6)
resource = get_resource_file_path("hooks/pyside6", "resource", ".py")
finder.include_file_as_module(resource, "PySide6._cx_freeze_resource")
Expand All @@ -105,7 +113,7 @@ def load_pyside6(finder: ModuleFinder, module: Module) -> None:
code_string += dedent(
f"""
# cx_Freeze patch start
if {IS_CONDA}:
if {environment == "conda"}:
import PySide6._cx_freeze_resource
else:
# Support for QtWebEngine
Expand Down Expand Up @@ -136,6 +144,8 @@ def load_pyside6(finder: ModuleFinder, module: Module) -> None:

# shiboken6 in zip_include_packages
shiboken6 = finder.include_package("shiboken6")
if module.in_file_system == 2:
shiboken6.in_file_system = 0
if shiboken6.in_file_system == 0:
name = "_additional_dll_directories"
source = f"""\
Expand Down
13 changes: 9 additions & 4 deletions cx_Freeze/hooks/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import os
from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand All @@ -26,8 +25,14 @@ def load_torch(finder: ModuleFinder, module: Module) -> None:
module_path = module.file.parent
site_packages_path = module_path.parent

# Activate an optimized mode when torch is in zip_include_packages
if module.in_file_system == 0:
# Activate the optimized mode by default
if module.name in finder.zip_exclude_packages:
print(f"WARNING: {module.name} hook optimizations disabled.")
module.in_file_system = 1
elif module.name in finder.zip_include_packages:
print(f"WARNING: {module.name} hook optimizations enabled.")
module.in_file_system = 2
else:
module.in_file_system = 2
# patch the code to ignore CUDA_PATH_Vxx_x installation directory
code_string = module.file.read_text(encoding="utf_8")
Expand Down Expand Up @@ -95,7 +100,7 @@ def load_torch__dynamo_skipfiles(finder: ModuleFinder, module: Module) -> None:
)
module.code = compile(
code_string,
os.fspath(module.file),
module.file.as_posix(),
"exec",
dont_inherit=True,
optimize=finder.optimize,
Expand Down
9 changes: 1 addition & 8 deletions samples/opencv/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@
options = {
"build_exe": {
# exclude packages that are not really needed
"excludes": [
"tkinter",
"unittest",
"email",
"http",
"xml",
"pydoc",
],
"excludes": ["tkinter", "unittest", "email", "http", "xml", "pydoc"],
"include_files": [("image.png", "share/image.png")],
}
}
Expand Down
12 changes: 2 additions & 10 deletions samples/pyqt5-simplebrowser/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@
options = {
"build_exe": {
# exclude packages that are not really needed
"excludes": [
"tkinter",
"unittest",
"email",
"http",
"xml",
"pydoc",
],
"zip_include_packages": ["PyQt5"],
"excludes": ["tkinter", "unittest", "email", "http", "xml", "pydoc"]
},
"bdist_mac": {"bundle_name": "PyQt5 Webengine Test"},
}
Expand All @@ -35,7 +27,7 @@

setup(
name="simplebrowser",
version="0.1",
version="7.2",
description="cx_Freeze and PyQt5 Webengine sample",
options=options,
executables=executables,
Expand Down
3 changes: 1 addition & 2 deletions samples/pyqt5/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
# exclude packages that are not really needed
"excludes": ["tkinter", "unittest", "email", "http", "xml", "pydoc"],
"include_files": include_files,
"zip_include_packages": ["PyQt5"],
}

bdist_mac_options = {
Expand All @@ -45,7 +44,7 @@

setup(
name="simple_PyQt5",
version="0.5",
version="7.2",
description="Sample cx_Freeze PyQt5 script",
options={
"build_exe": build_exe_options,
Expand Down
3 changes: 1 addition & 2 deletions samples/pyqt6-simplebrowser/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
# exclude packages that are not really needed
"excludes": ["tkinter", "unittest", "email", "http", "xml", "pydoc"],
"include_files": include_files,
"zip_include_packages": ["PyQt6"],
}

executables = [
Expand All @@ -39,7 +38,7 @@

setup(
name="simplebrowser",
version="0.2",
version="7.2",
description="Sample cx_Freeze PyQt6 simplebrowser script",
options={"build_exe": build_exe_options},
executables=executables,
Expand Down
Loading

0 comments on commit 129ea48

Please sign in to comment.