pyi_rth_gdkpixbuf.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2015-2023, PyInstaller Development Team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: Apache-2.0
  10. #-----------------------------------------------------------------------------
  11. def _pyi_rthook():
  12. import atexit
  13. import os
  14. import sys
  15. import tempfile
  16. pixbuf_file = os.path.join(sys._MEIPASS, 'lib', 'gdk-pixbuf', 'loaders.cache')
  17. # If we are not on Windows, we need to rewrite the cache -> we rewrite on macOS to support --onefile mode
  18. if os.path.exists(pixbuf_file) and sys.platform != 'win32':
  19. with open(pixbuf_file, 'rb') as fp:
  20. contents = fp.read()
  21. # Create a temporary file with the cache and cleverly replace the prefix we injected with the actual path.
  22. fd, pixbuf_file = tempfile.mkstemp()
  23. with os.fdopen(fd, 'wb') as fp:
  24. libpath = os.path.join(sys._MEIPASS, 'lib').encode('utf-8')
  25. fp.write(contents.replace(b'@executable_path/lib', libpath))
  26. try:
  27. atexit.register(os.unlink, pixbuf_file)
  28. except OSError:
  29. pass
  30. os.environ['GDK_PIXBUF_MODULE_FILE'] = pixbuf_file
  31. _pyi_rthook()
  32. del _pyi_rthook