hook-rtree.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # ------------------------------------------------------------------
  2. # Copyright (c) 2021 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. import pathlib
  13. from PyInstaller import compat
  14. from PyInstaller.utils.hooks import collect_dynamic_libs, get_installer, get_package_paths
  15. # Query the installer of the `rtree` package; in PyInstaller prior to 6.0, this might raise an exception, whereas in
  16. # later versions, None is returned.
  17. try:
  18. package_installer = get_installer('rtree')
  19. except Exception:
  20. package_installer = None
  21. if package_installer == 'conda':
  22. from PyInstaller.utils.hooks import conda
  23. # In Anaconda-packaged `rtree`, `libspatialindex` and `libspatialindex_c` shared libs are packaged in a separate
  24. # `libspatialindex` package. Collect the libraries into `rtree/lib` sub-directory to simulate PyPI wheel layout.
  25. binaries = conda.collect_dynamic_libs('libspatialindex', dest='rtree/lib', dependencies=False)
  26. else:
  27. # pip-installed package. The shared libs are usually placed in `rtree/lib` directory.
  28. binaries = collect_dynamic_libs('rtree')
  29. # With rtree >= 1.1.0, Linux PyPI wheels place the shared library in a `Rtree.libs` top-level directory.
  30. # In rtree 1.4.0, the directory was renamed to `rtree.libs`
  31. if compat.is_linux:
  32. _, rtree_dir = get_package_paths('rtree')
  33. for candidate_dir_name in ('rtree.libs', 'Rtree.libs'):
  34. rtree_libs_dir = pathlib.Path(rtree_dir).parent / candidate_dir_name
  35. if not rtree_libs_dir.is_dir():
  36. continue
  37. binaries += [
  38. (str(lib_file), candidate_dir_name) for lib_file in rtree_libs_dir.glob("libspatialindex*.so*")
  39. ]