hook-osgeo.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # ------------------------------------------------------------------
  2. # Copyright (c) 2020 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. from PyInstaller.utils.hooks import collect_data_files
  13. from PyInstaller.compat import is_win, is_darwin
  14. import os
  15. import sys
  16. # The osgeo libraries require auxiliary data and may have hidden dependencies.
  17. # There are several possible configurations on how these libraries can be
  18. # deployed.
  19. # This hook evaluates the cases when:
  20. # - the `data` folder is present "in-source" (sharing the same namespace folder
  21. # as the code libraries)
  22. # - the `data` folder is present "out-source" (for instance, on Anaconda for
  23. # Windows, in PYTHONHOME/Library/data)
  24. # In this latter case, the hook also checks for the presence of `proj` library
  25. # (e.g., on Windows in PYTHONHOME) for being added to the bundle.
  26. #
  27. # This hook has been tested with gdal (v.1.11.2 and 1.11.3) on:
  28. # - Win 7 and 10 64bit
  29. # - Ubuntu 15.04 64bit
  30. # - Mac OS X Yosemite 10.10
  31. #
  32. # TODO: Fix for gdal>=2.0.0, <2.0.3: 'NameError: global name 'help' is not defined'
  33. # flag used to identify an Anaconda environment
  34. is_conda = False
  35. # Auxiliary data:
  36. #
  37. # - general case (data in 'osgeo/data'):
  38. datas = collect_data_files('osgeo', subdir='data')
  39. # check if the data has been effectively found in 'osgeo/data/gdal'
  40. if len(datas) == 0:
  41. if hasattr(sys, 'real_prefix'): # check if in a virtual environment
  42. root_path = sys.real_prefix
  43. else:
  44. root_path = sys.prefix
  45. # - conda-specific
  46. if is_win:
  47. tgt_gdal_data = os.path.join('Library', 'share', 'gdal')
  48. src_gdal_data = os.path.join(root_path, 'Library', 'share', 'gdal')
  49. if not os.path.exists(src_gdal_data):
  50. tgt_gdal_data = os.path.join('Library', 'data')
  51. src_gdal_data = os.path.join(root_path, 'Library', 'data')
  52. else: # both linux and darwin
  53. tgt_gdal_data = os.path.join('share', 'gdal')
  54. src_gdal_data = os.path.join(root_path, 'share', 'gdal')
  55. if os.path.exists(src_gdal_data):
  56. is_conda = True
  57. datas.append((src_gdal_data, tgt_gdal_data))
  58. # a real-time hook takes case to define the path for `GDAL_DATA`
  59. # Hidden dependencies
  60. if is_conda:
  61. # if `proj.4` is present, it provides additional functionalities
  62. if is_win:
  63. proj4_lib = os.path.join(root_path, 'proj.dll')
  64. elif is_darwin:
  65. proj4_lib = os.path.join(root_path, 'lib', 'libproj.dylib')
  66. else: # assumed linux-like settings
  67. proj4_lib = os.path.join(root_path, 'lib', 'libproj.so')
  68. if os.path.exists(proj4_lib):
  69. binaries = [(proj4_lib, ".")]