hook-pkg_resources.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. from PyInstaller.utils.hooks import collect_submodules, can_import_module
  12. from PyInstaller.utils.hooks.setuptools import setuptools_info
  13. hiddenimports = []
  14. excludedimports = ['__main__']
  15. # pkg_resources keeps vendored modules in its _vendor subpackage, and does sys.meta_path based import magic to expose
  16. # them as pkg_resources.extern.*
  17. #
  18. # With setuptools >= 71.0, pkg_resources ceased to vendor packages, because vendoring is now done at the setuptools
  19. # level.
  20. if setuptools_info.available and setuptools_info.version < (71, 0, 0):
  21. # The `railroad` package is an optional requirement for `pyparsing`. `pyparsing.diagrams` depends on `railroad`, so
  22. # filter it out when `railroad` is not available.
  23. if can_import_module('railroad'):
  24. hiddenimports += collect_submodules('pkg_resources._vendor')
  25. else:
  26. hiddenimports += collect_submodules(
  27. 'pkg_resources._vendor', filter=lambda name: 'pkg_resources._vendor.pyparsing.diagram' not in name
  28. )
  29. # pkg_resources v45.0 dropped support for Python 2 and added this module printing a warning. We could save some
  30. # bytes if we would replace this by a fake module.
  31. if setuptools_info.version >= (45, 0, 0) and setuptools_info.version < (49, 1, 1):
  32. hiddenimports += ['pkg_resources.py2_warn']
  33. # As of v60.7, setuptools vendored jaraco and has pkg_resources use it. Currently, the pkg_resources._vendor.jaraco
  34. # namespace package cannot be automatically scanned due to limited support for pure namespace packages in our hook
  35. # utilities.
  36. #
  37. # In setuptools 60.7.0, the vendored jaraco.text package included "Lorem Ipsum.txt" data file, which also has to be
  38. # collected. However, the presence of the data file (and the resulting directory hierarchy) confuses the importer's
  39. # redirection logic; instead of trying to work-around that, tell user to upgrade or downgrade their setuptools.
  40. if setuptools_info.version == (60, 7, 0):
  41. raise SystemExit(
  42. "ERROR: Setuptools 60.7.0 is incompatible with PyInstaller. "
  43. "Downgrade to an earlier version or upgrade to a later version."
  44. )
  45. # In setuptools 60.7.1, the "Lorem Ipsum.txt" data file was dropped from the vendored jaraco.text package, so we can
  46. # accommodate it with couple of hidden imports.
  47. elif setuptools_info.version >= (60, 7, 1):
  48. hiddenimports += [
  49. 'pkg_resources._vendor.jaraco.functools',
  50. 'pkg_resources._vendor.jaraco.context',
  51. 'pkg_resources._vendor.jaraco.text',
  52. ]
  53. # As of setuptools 70.0.0, we need pkg_resources.extern added to hidden imports.
  54. if setuptools_info.version >= (70, 0, 0):
  55. hiddenimports += [
  56. 'pkg_resources.extern',
  57. ]
  58. # Some more hidden imports. See:
  59. # https://github.com/pyinstaller/pyinstaller-hooks-contrib/issues/15#issuecomment-663699288 `packaging` can either be
  60. # its own package, or embedded in `pkg_resources._vendor.packaging`, or both.
  61. hiddenimports += collect_submodules('packaging')