hook-ruamel.yaml.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # ------------------------------------------------------------------
  2. # Copyright (c) 2024 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. # `ruamel.yaml` offers several optional plugins that can be installed via additional packages
  13. # (e.g., `runamel.yaml.string`). Unfortunately, the discovery of these plugins is predicated on their `__plug_in__.py`
  14. # files being visible on filesystem.
  15. # See: https://sourceforge.net/p/ruamel-yaml/code/ci/0bef9fa8b3c43637cd90ce3f2e299e81c2122128/tree/main.py#l757
  16. import pathlib
  17. from PyInstaller.utils.hooks import get_module_file_attribute, logger
  18. ruamel_path = pathlib.Path(get_module_file_attribute('ruamel.yaml')).parent
  19. plugin_files = ruamel_path.glob('*/__plug_in__.py')
  20. plugin_names = [plugin_file.parent.name for plugin_file in plugin_files]
  21. logger.debug("hook-ruamel.yaml: found plugins: %r", plugin_names)
  22. # Add `__plug_in__` modules to hiddenimports to ensure they are collected and scanned for imports. This also implicitly
  23. # collects the plugin's `__init__` module.
  24. plugin_modules = [f"ruamel.yaml.{plugin_name}.__plug_in__" for plugin_name in plugin_names]
  25. hiddenimports = plugin_modules
  26. # Collect the plugins' `__plug_in__` modules both as byte-compiled .pyc in PYZ archive (to be actually loaded) and
  27. # source .py file (which allows plugin to be discovered).
  28. module_collection_mode = {
  29. plugin_module: "pyz+py"
  30. for plugin_module in plugin_modules
  31. }