hook-pydicom.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. from PyInstaller.utils.hooks import is_module_satisfies, collect_data_files
  13. hiddenimports = []
  14. datas = []
  15. # In pydicom 3.0.0, the `pydicom.encoders` plugins were renamed to `pydicom.pixels.encoders`, and
  16. # `pydicom.pixels.decoders` were also added. We need to collect them all, because they are loaded during
  17. # `pydicom` module initialization. We intentionally avoid using `collect_submodules` here, because that causes
  18. # import of `pydicom` with logging framework initialized, which results in error tracebacks being logged for all plugins
  19. # with missing libraries (see https://github.com/pydicom/pydicom/issues/2128).
  20. if is_module_satisfies('pydicom >= 3.0.0'):
  21. hiddenimports += [
  22. "pydicom.pixels.decoders.gdcm",
  23. "pydicom.pixels.decoders.pylibjpeg",
  24. "pydicom.pixels.decoders.pillow",
  25. "pydicom.pixels.decoders.pyjpegls",
  26. "pydicom.pixels.decoders.rle",
  27. "pydicom.pixels.encoders.gdcm",
  28. "pydicom.pixels.encoders.pylibjpeg",
  29. "pydicom.pixels.encoders.native",
  30. "pydicom.pixels.encoders.pyjpegls",
  31. ]
  32. # With pydicom 3.0.0, initialization of `pydicom` (unnecessarily) imports `pydicom.examples`, which attempts to set
  33. # up several test datasets: https://github.com/pydicom/pydicom/blob/v3.0.0/src/pydicom/examples/__init__.py#L10-L24
  34. # Some of those are bundled with the package itself, some are downloaded (into `.pydicom/data` directory in user's
  35. # home directory) on he first `pydicom.examples` import.
  36. #
  37. # The download code requires `pydicom/data/urls.json` and `pydicom/data/hashes.json`; the lack of former results in
  38. # run-time error, while the lack of latter results in warnings about dataset download failure.
  39. #
  40. # The test data files that are bundled with the package are not listed in `urls.json`, so if they are missing, there
  41. # is not attempt to download them. Therefore, try to get away without collecting them here - if anyone actually
  42. # requires them in the frozen application, let them explicitly collect them.
  43. additional_data_patterns = [
  44. 'urls.json',
  45. 'hashes.json',
  46. ]
  47. else:
  48. hiddenimports += [
  49. "pydicom.encoders.gdcm",
  50. "pydicom.encoders.pylibjpeg",
  51. "pydicom.encoders.native",
  52. ]
  53. additional_data_patterns = []
  54. # Collect data files from `pydicom.data`; charset files and palettes might be needed during processing, so always
  55. # collect them. Some other data files became required in v3.0.0 - the corresponding patterns are set accordingly in
  56. # `additional_data_patterns` in the above if/else block.
  57. datas += collect_data_files(
  58. 'pydicom.data',
  59. includes=[
  60. 'charset_files/*',
  61. 'palettes/*',
  62. *additional_data_patterns,
  63. ],
  64. )