From 21d3e539f8c49a5367a3acd09371b6bc6cddfafa Mon Sep 17 00:00:00 2001 From: Rok Mandeljc Date: Thu, 18 Jan 2024 11:52:01 +0100 Subject: [PATCH] hooks: fix pypylon hook for PyInstaller 6.0 and later 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. --- news/691.update.rst | 1 + requirements-test-libraries.txt | 1 + .../hooks/stdhooks/hook-pypylon.py | 29 ++++++++++++------- .../tests/test_libraries.py | 7 +++++ 4 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 news/691.update.rst diff --git a/news/691.update.rst b/news/691.update.rst new file mode 100644 index 000000000..447e9803d --- /dev/null +++ b/news/691.update.rst @@ -0,0 +1 @@ +Update ``pypylon`` hook for compatibility with PyInstaller 6.0 and later. diff --git a/requirements-test-libraries.txt b/requirements-test-libraries.txt index 589a25a81..a7f08201d 100644 --- a/requirements-test-libraries.txt +++ b/requirements-test-libraries.txt @@ -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 diff --git a/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-pypylon.py b/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-pypylon.py index ab7c573fb..d9cb44716 100644 --- a/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-pypylon.py +++ b/src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-pypylon.py @@ -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)] diff --git a/src/_pyinstaller_hooks_contrib/tests/test_libraries.py b/src/_pyinstaller_hooks_contrib/tests/test_libraries.py index e316993f6..8ed8a83cf 100644 --- a/src/_pyinstaller_hooks_contrib/tests/test_libraries.py +++ b/src/_pyinstaller_hooks_contrib/tests/test_libraries.py @@ -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 + """)