Skip to content

Commit

Permalink
hooks: fix pypylon hook for PyInstaller 6.0 and later
Browse files Browse the repository at this point in the history
Fix the `pypylon` hook for compatibility with PyInstaller >= 6.0.
The `collect_data_files(..., include_py_files=True)` does not
include binary extensions anymore, and PyInstaller 6.2
(pyinstaller/pyinstaller@ecc218c) fixed the module exclusion for
relative imports; so the extensions that hook tries to exclude
actually end up excluded without being collected via the
alternative codepath.

Presumably that part of hook was needed on older PyInstaller
versions, so keep it around, but disable it for PyInstaller >= 6.0.
  • Loading branch information
rokm committed Jan 18, 2024
1 parent 1c9b69c commit 21d3e53
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions news/691.update.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update ``pypylon`` hook for compatibility with PyInstaller 6.0 and later.
1 change: 1 addition & 0 deletions requirements-test-libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ seedir==0.4.2
cel-python==0.1.5
pygwalker==0.4.2
eth-hash==0.6.0
pypylon==3.0.1

# ------------------- Platform (OS) specifics

Expand Down
29 changes: 19 additions & 10 deletions src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-pypylon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@

import os

from PyInstaller.utils.hooks import collect_data_files
from PyInstaller.utils.hooks import collect_dynamic_libs
from PyInstaller.utils.hooks import (
collect_data_files,
collect_dynamic_libs,
is_module_satisfies
)

# Collect dynamic libs as data (to prevent pyinstaller from modifying them)
# Collect dynamic libs as data (to prevent pyinstaller from modifying them).
# NOTE: under PyInstaller 6.x, these files end up re-classified as binaries anyway.
datas = collect_dynamic_libs('pypylon')

# Collect data files, looking for pypylon/pylonCXP/bin/ProducerCXP.cti, but other files may also be needed
datas += collect_data_files('pypylon')

# Exclude the C++-extensions from automatic search, add them manually as data files
# their dependencies were already handled with collect_dynamic_libs
excludedimports = ['pypylon._pylon', 'pypylon._genicam']
for filename, module in collect_data_files('pypylon', include_py_files=True):
if (os.path.basename(filename).startswith('_pylon.')
or os.path.basename(filename).startswith('_genicam.')):
datas += [(filename, module)]
# NOTE: the part below is incompatible with PyInstaller 6.x, because `collect_data_files(..., include_py_files=True)`
# does not include binary extensions anymore. In addition, `pyinstaller/pyinstaller@ecc218c` in PyInstaller 6.2 fixed
# the module exclusion for relative imports, so the modules listed below actually end up excluded. Presumably this
# part was necessary with older PyInstaller versions, so we keep it around, but disable it for PyInstaller >= 6.0.
if is_module_satisfies('PyInstaller < 6.0'):
# Exclude the C++-extensions from automatic search, add them manually as data files
# their dependencies were already handled with collect_dynamic_libs
excludedimports = ['pypylon._pylon', 'pypylon._genicam']
for filename, module in collect_data_files('pypylon', include_py_files=True):
if (os.path.basename(filename).startswith('_pylon.')
or os.path.basename(filename).startswith('_genicam.')):
datas += [(filename, module)]
7 changes: 7 additions & 0 deletions src/_pyinstaller_hooks_contrib/tests/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,3 +1888,10 @@ def test_pygwalker(pyi_builder):
pyi_builder.test_source("""
import pygwalker
""")


@importorskip('pypylon')
def test_pypylon(pyi_builder):
pyi_builder.test_source("""
from pypylon import pylon
""")

0 comments on commit 21d3e53

Please sign in to comment.