hook-pypylon.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # ------------------------------------------------------------------
  2. # Copyright (c) 2020 PyInstaller Development Team.
  3. #
  4. # This file is distributed under the terms of the GNU General Public
  5. # License (version 2.0 or later).
  6. #
  7. # The full license is available in LICENSE, distributed with
  8. # this software.
  9. #
  10. # SPDX-License-Identifier: GPL-2.0-or-later
  11. # ------------------------------------------------------------------
  12. # PyPylon is a tricky library to bundle. It encapsulates the pylon C++ SDK inside
  13. # it with modified library references to make the module relocatable.
  14. # PyInstaller is able to find those libraries and preserve the linkage for almost
  15. # all of them. However - there is an additional linking step happening at runtime,
  16. # when the library is creating the transport layer for the camera. This linking
  17. # will fail with the library files modified by pyinstaller.
  18. # As the module is already relocatable, we circumvent this issue by bundling
  19. # pypylon as-is - for pyinstaller we treat the shared library files as just data.
  20. import os
  21. from PyInstaller.utils.hooks import (
  22. collect_data_files,
  23. collect_dynamic_libs,
  24. is_module_satisfies
  25. )
  26. # Collect dynamic libs as data (to prevent pyinstaller from modifying them).
  27. # NOTE: under PyInstaller 6.x, these files end up re-classified as binaries anyway.
  28. datas = collect_dynamic_libs('pypylon')
  29. # Collect data files, looking for pypylon/pylonCXP/bin/ProducerCXP.cti, but other files may also be needed
  30. datas += collect_data_files('pypylon')
  31. # NOTE: the part below is incompatible with PyInstaller 6.x, because `collect_data_files(..., include_py_files=True)`
  32. # does not include binary extensions anymore. In addition, `pyinstaller/pyinstaller@ecc218c` in PyInstaller 6.2 fixed
  33. # the module exclusion for relative imports, so the modules listed below actually end up excluded. Presumably this
  34. # part was necessary with older PyInstaller versions, so we keep it around, but disable it for PyInstaller >= 6.0.
  35. if is_module_satisfies('PyInstaller < 6.0'):
  36. # Exclude the C++-extensions from automatic search, add them manually as data files
  37. # their dependencies were already handled with collect_dynamic_libs
  38. excludedimports = ['pypylon._pylon', 'pypylon._genicam']
  39. for filename, module in collect_data_files('pypylon', include_py_files=True):
  40. if (os.path.basename(filename).startswith('_pylon.')
  41. or os.path.basename(filename).startswith('_genicam.')):
  42. datas += [(filename, module)]