hook-soundfile.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. """
  13. pysoundfile:
  14. https://github.com/bastibe/SoundFile
  15. """
  16. import pathlib
  17. from PyInstaller.utils.hooks import get_module_file_attribute, logger
  18. binaries = []
  19. datas = []
  20. # PyPI wheels for Windows and macOS ship the sndfile shared library in _soundfile_data directory,
  21. # located next to the soundfile.py module file (i.e., in the site-packages directory).
  22. module_dir = pathlib.Path(get_module_file_attribute('soundfile')).parent
  23. data_dir = module_dir / '_soundfile_data'
  24. if data_dir.is_dir():
  25. destdir = str(data_dir.relative_to(module_dir))
  26. # Collect the shared library (known variants: libsndfile64bit.dll, libsndfile32bit.dll, libsndfile.dylib)
  27. for lib_file in data_dir.glob("libsndfile*.*"):
  28. binaries += [(str(lib_file), destdir)]
  29. # Collect the COPYING file
  30. copying_file = data_dir / "COPYING"
  31. if copying_file.is_file():
  32. datas += [(str(copying_file), destdir)]
  33. else:
  34. # On linux and in Anaconda in all OSes, the system-installed sndfile library needs to be collected.
  35. def _find_system_sndfile_library():
  36. import os
  37. import ctypes.util
  38. from PyInstaller.depend.utils import _resolveCtypesImports
  39. libname = ctypes.util.find_library("sndfile")
  40. if libname is not None:
  41. resolved_binary = _resolveCtypesImports([os.path.basename(libname)])
  42. if resolved_binary:
  43. return resolved_binary[0][1]
  44. try:
  45. lib_file = _find_system_sndfile_library()
  46. except Exception as e:
  47. logger.warning("Error while trying to find system-installed sndfile library: %s", e)
  48. lib_file = None
  49. if lib_file:
  50. binaries += [(lib_file, '.')]
  51. if not binaries:
  52. logger.warning("sndfile shared library not found - soundfile will likely fail to work!")