hook-setuptools.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-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 import compat
  12. from PyInstaller.utils.hooks.setuptools import setuptools_info
  13. datas = []
  14. hiddenimports = [
  15. # Test case import/test_zipimport2 fails during importing pkg_resources or setuptools when module not present.
  16. 'distutils.command.build_ext',
  17. 'setuptools.msvc',
  18. ]
  19. # Necessary for setuptools on Mac/Unix
  20. if compat.is_unix or compat.is_darwin:
  21. hiddenimports.append('syslog')
  22. # Prevent the following modules from being collected solely due to reference from anywhere within setuptools (or
  23. # its vendored dependencies).
  24. excludedimports = [
  25. 'pytest',
  26. 'numpy', # originally from hook-setuptools.msvc
  27. 'docutils', # originally from hool-setuptools._distutils.command.check
  28. ]
  29. # setuptools >= 39.0.0 is "vendoring" its own direct dependencies from "_vendor" to "extern". This also requires
  30. # 'pre_safe_import_module/hook-setuptools.extern.six.moves.py' to make the moves defined in 'setuptools._vendor.six'
  31. # importable under 'setuptools.extern.six'.
  32. #
  33. # With setuptools 71.0.0, the vendored packages are exposed to the outside world by `setuptools._vendor` location being
  34. # appended to `sys.path`, and the `VendorImporter` is gone (i.e., no more mapping to `setuptools.extern`). Since the
  35. # vendored dependencies are now exposed as top-level modules (provided upstream versions are not available, as they
  36. # would take precedence due to `sys.path` ordering), we need pre-safe-import-module hooks that detect when only vendored
  37. # version is available, and add aliases to prevent duplicated collection. For list of vendored packages for which we
  38. # need such pre-safe-import-module hooks, see the code in `PyInstaller.utils.hooks.setuptools`.
  39. #
  40. # The list of submodules from `setuptools._vendor` is now available in `setuptools_info.vendored_modules` (and covers
  41. # all setuptools versions).
  42. #
  43. # NOTE: with setuptools >= 71.0, we do not need to add modules from `setuptools._vendored` to hidden imports anymore,
  44. # because the aliases we set up should ensure that the necessary parts get collected. We still need them for earlier
  45. # versions of setuptools, though.
  46. if setuptools_info.version < (71, 0):
  47. hiddenimports += setuptools_info.vendored_modules
  48. # The situation with vendored distutils (from `setuptools._distutils`) is a bit more complicated; python >= 3.12 does
  49. # not provide stdlib version of `distutils` anymore, so our corresponding pre-safe-import-module hook sets up aliases.
  50. # In earlier python versions, stdlib version is available as well, and at run-time, we might need both versions present,
  51. # so that whichever is applicable can be used. Therefore, for python < 3.12, we need to add the vendored distuils
  52. # modules to hidden imports.
  53. if setuptools_info.distutils_vendored and not compat.is_py312:
  54. hiddenimports += setuptools_info.distutils_modules
  55. # With setuptools >= 71.0.0, the vendored packages also have metadata, and might also contain data files that need to
  56. # be collected. The list of corresponding data files is kept cached in `setuptools_info.vendored_data` (to minimize the
  57. # number of times we need to call collect_data_files()).
  58. #
  59. # While it might be tempting to simply collect all data files and be done with it, we actually need to match the
  60. # collection behavior for the stand-alone versions of these packages; i.e., we should collect metadata (and/or data
  61. # files) for the vendored package only if the same data is also collected for stand-alone version. Otherwise, we risk
  62. # inconsistent behavior and potential mismatches; for example, if we collected metadata for vendored package A here,
  63. # but end up collecting stand-alone A, for which we normally do not collect the metadata, then at run-time, we will end
  64. # up with stand-alone copy of A and vendored copy of its metadata being discoverable.
  65. #
  66. # Therefore, if metadata and/or metadata needs to be collected, do it in corresponding sub-package hook (for an example,
  67. # see `hook-setuptools._vendor.jaraco.text.py`).