Skip to content

Commit

Permalink
Add support for pyinstaller (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein authored Nov 6, 2024
1 parent c04c42c commit 6257b49
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -U .
pip install pytest pyinstaller
pip install pytest pyinstaller psutil glfw
- name: Unit tests
run: |
pushd $HOME
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ glfw = ["glfw>=1.9"]
lint = ["ruff", "pre-commit"]
examples = ["numpy", "wgpu", "glfw", "pyside6"]
docs = ["sphinx>7.2", "sphinx_rtd_theme", "sphinx-gallery"]
tests = ["pytest", "numpy", "psutil", "wgpu", "glfw"]
tests = ["pytest", "numpy", "wgpu", "glfw"]
dev = ["rendercanvas[lint,tests,examples,docs]"]

[project.entry-points."pyinstaller40"]
Expand Down
12 changes: 12 additions & 0 deletions rendercanvas/__pyinstaller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from os.path import dirname


HERE = dirname(__file__)


def get_hook_dirs():
return [HERE]


def get_test_dirs():
return [HERE]
1 change: 1 addition & 0 deletions rendercanvas/__pyinstaller/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from PyInstaller.utils.conftest import * # noqa: F403
22 changes: 22 additions & 0 deletions rendercanvas/__pyinstaller/hook-rendercanvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ruff: noqa: N999

from PyInstaller.utils.hooks import collect_dynamic_libs

# Init variables that PyInstaller will pick up.
hiddenimports = []
datas = []
binaries = []

# Add modules that are safe to add, i.e. don't pull in dependencies that we don't want.
hiddenimports += ["rendercanvas.offscreen"]

# Since glfw does not have a hook like this, it does not include the glfw binary
# when freezing. We can solve this with the code below. Makes the binary a bit
# larger, but only marginally (less than 300kb).
try:
import glfw # noqa: F401
except ImportError:
pass
else:
hiddenimports += ["rendercanvas.glfw"]
binaries += collect_dynamic_libs("glfw")
47 changes: 47 additions & 0 deletions rendercanvas/__pyinstaller/test_rendercanvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
script = """
# The script part
import sys
import importlib
from rendercanvas.auto import RenderCanvas
if "glfw" not in RenderCanvas.__name__.lower():
raise RuntimeError(f"Expected a glfw canvas, got {RenderCanvas.__name__}")
# The test part
if "is_test" in sys.argv:
included_modules = [
"rendercanvas.glfw",
"rendercanvas.offscreen",
"glfw",
]
excluded_modules = [
"PySide6.QtGui",
"PyQt6.QtGui",
]
for module_name in included_modules:
importlib.import_module(module_name)
for module_name in excluded_modules:
try:
importlib.import_module(module_name)
except ModuleNotFoundError:
continue
raise RuntimeError(module_name + " is not supposed to be importable.")
"""


def test_pyi_rendercanvas(pyi_builder):
pyi_builder.test_source(script, app_args=["is_test"])


# We could also test the script below, but it's not that interesting since it uses direct imports.
# To safe CI-time we don't actively test it.
script_qt = """
import sys
import importlib
import PySide6
from rendercanvas.qt import RenderCanvas
assert "qt" in RenderCanvas.__name__.lower()
"""

0 comments on commit 6257b49

Please sign in to comment.