hook-sounddevice.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. sounddevice:
  14. https://github.com/spatialaudio/python-sounddevice/
  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 _sounddevice_data directory,
  21. # located next to the sounddevice.py module file (i.e., in the site-packages directory).
  22. module_dir = pathlib.Path(get_module_file_attribute('sounddevice')).parent
  23. data_dir = module_dir / '_sounddevice_data' / 'portaudio-binaries'
  24. if data_dir.is_dir():
  25. destdir = str(data_dir.relative_to(module_dir))
  26. # Collect the shared library (known variants: libportaudio64bit.dll, libportaudio32bit.dll, libportaudio.dylib)
  27. for lib_file in data_dir.glob("libportaudio*.*"):
  28. binaries += [(str(lib_file), destdir)]
  29. # Collect the README.md file
  30. readme_file = data_dir / "README.md"
  31. if readme_file.is_file():
  32. datas += [(str(readme_file), destdir)]
  33. else:
  34. # On linux and in Anaconda in all OSes, the system-installed portaudio library needs to be collected.
  35. def _find_system_portaudio_library():
  36. import os
  37. import ctypes.util
  38. from PyInstaller.depend.utils import _resolveCtypesImports
  39. libname = ctypes.util.find_library("portaudio")
  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_portaudio_library()
  46. except Exception as e:
  47. logger.warning("Error while trying to find system-installed portaudio library: %s", e)
  48. lib_file = None
  49. if lib_file:
  50. binaries += [(lib_file, '.')]
  51. if not binaries:
  52. logger.warning("portaudio shared library not found - sounddevice will likely fail to work!")