You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

  1. '''
  2. Utils for retrieving resources when when in a frozen state.
  3. MEIPASS explanation:
  4. https://pythonhosted.org/PyInstaller/#run-time-operation
  5. '''
  6. import os
  7. import sys
  8. def is_frozen():
  9. return getattr(sys, 'frozen', False)
  10. def getResourcePath(*args):
  11. if is_frozen():
  12. # MEIPASS explanation:
  13. # https://pythonhosted.org/PyInstaller/#run-time-operation
  14. basedir = getattr(sys, '_MEIPASS', None)
  15. if not basedir:
  16. basedir = os.path.dirname(sys.executable)
  17. resource_dir = os.path.join(basedir, 'gooey')
  18. if not os.path.isdir(resource_dir):
  19. raise IOError(
  20. (
  21. "Cannot locate Gooey resources. It seems that the program was frozen, "
  22. "but resource files were not copied into directory of the executable "
  23. "file. Please copy `languages` and `images` folders from gooey module "
  24. "directory into `{}{}` directory. Using PyInstaller, a.datas in .spec "
  25. "file must be specified.".format(resource_dir, os.sep)))
  26. else:
  27. resource_dir = os.path.normpath(
  28. os.path.join(os.path.dirname(__file__), '..', '..'))
  29. return os.path.join(resource_dir, *args)
  30. def localResourcePath(path):
  31. """
  32. A packaging aware util for getting the path to the local working directory.
  33. When non-packaged, this is os.getcwd(), when packaged, it will be the local
  34. (dynamic) directory where PyInstaller decompresses content.
  35. """
  36. if is_frozen():
  37. basedir = getattr(sys, '_MEIPASS', None)
  38. return os.path.join(basedir or sys.executable, path)
  39. else:
  40. return os.path.join(os.getcwd(), path)