hook-gi.repository.Gst.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2023, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. # GStreamer contains a lot of plugins. We need to collect them and bundle them with the exe file. We also need to
  12. # resolve binary dependencies of these GStreamer plugins.
  13. import pathlib
  14. from PyInstaller.utils.hooks import get_hook_config, include_or_exclude_file
  15. import PyInstaller.log as logging
  16. from PyInstaller import isolated
  17. from PyInstaller.utils.hooks.gi import GiModuleInfo, collect_glib_share_files, collect_glib_translations
  18. logger = logging.getLogger(__name__)
  19. @isolated.decorate
  20. def _get_gst_plugin_path():
  21. import os
  22. import gi
  23. gi.require_version('Gst', '1.0')
  24. from gi.repository import Gst
  25. Gst.init(None)
  26. reg = Gst.Registry.get()
  27. plug = reg.find_plugin('coreelements')
  28. path = plug.get_filename()
  29. return os.path.dirname(path)
  30. def _format_plugin_pattern(plugin_name):
  31. return f"**/*gst{plugin_name}.*"
  32. def hook(hook_api):
  33. module_info = GiModuleInfo('Gst', '1.0')
  34. if not module_info.available:
  35. return
  36. binaries, datas, hiddenimports = module_info.collect_typelib_data()
  37. hiddenimports += ["gi.repository.Gio"]
  38. # Collect data files
  39. datas += collect_glib_share_files('gstreamer-1.0')
  40. # Translations
  41. lang_list = get_hook_config(hook_api, "gi", "languages")
  42. for prog in [
  43. 'gst-plugins-bad-1.0',
  44. 'gst-plugins-base-1.0',
  45. 'gst-plugins-good-1.0',
  46. 'gst-plugins-ugly-1.0',
  47. 'gstreamer-1.0',
  48. ]:
  49. datas += collect_glib_translations(prog, lang_list)
  50. # Plugins
  51. try:
  52. plugin_path = _get_gst_plugin_path()
  53. except Exception as e:
  54. logger.warning("Failed to determine gstreamer plugin path: %s", e)
  55. plugin_path = None
  56. if plugin_path:
  57. plugin_path = pathlib.Path(plugin_path)
  58. # Obtain optional include/exclude list from hook config
  59. include_list = get_hook_config(hook_api, "gstreamer", "include_plugins")
  60. exclude_list = get_hook_config(hook_api, "gstreamer", "exclude_plugins")
  61. # Format plugin basenames into filename patterns for matching
  62. if include_list is not None:
  63. include_list = [_format_plugin_pattern(name) for name in include_list]
  64. if exclude_list is not None:
  65. exclude_list = [_format_plugin_pattern(name) for name in exclude_list]
  66. # The names of GStreamer plugins typically start with libgst (or just gst, depending on the toolchain). We also
  67. # need to account for different extensions that might be used on a particular OS (for example, on macOS, the
  68. # extension may be either .so or .dylib).
  69. for lib_pattern in ['*gst*.dll', '*gst*.dylib', '*gst*.so']:
  70. binaries += [(str(filename), 'gst_plugins') for filename in plugin_path.glob(lib_pattern)
  71. if include_or_exclude_file(filename, include_list, exclude_list)]
  72. hook_api.add_datas(datas)
  73. hook_api.add_binaries(binaries)
  74. hook_api.add_imports(*hiddenimports)