cura_app.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015 Ultimaker B.V.
  3. # Cura is released under the terms of the LGPLv3 or higher.
  4. import os
  5. import sys
  6. from UM.Platform import Platform
  7. def get_cura_dir_path():
  8. if Platform.isWindows():
  9. return os.path.expanduser("~/AppData/Roaming/cura/")
  10. elif Platform.isLinux():
  11. return os.path.expanduser("~/.local/share/cura")
  12. elif Platform.isOSX():
  13. return os.path.expanduser("~/Library/Logs/cura")
  14. if hasattr(sys, "frozen"):
  15. dirpath = get_cura_dir_path()
  16. os.makedirs(dirpath, exist_ok = True)
  17. sys.stdout = open(os.path.join(dirpath, "stdout.log"), "w")
  18. sys.stderr = open(os.path.join(dirpath, "stderr.log"), "w")
  19. import platform
  20. import faulthandler
  21. #WORKAROUND: GITHUB-88 GITHUB-385 GITHUB-612
  22. if Platform.isLinux(): # Needed for platform.linux_distribution, which is not available on Windows and OSX
  23. # For Ubuntu: https://bugs.launchpad.net/ubuntu/+source/python-qt4/+bug/941826
  24. linux_distro_name = platform.linux_distribution()[0].lower()
  25. if linux_distro_name in ("debian", "ubuntu", "linuxmint", "fedora"): # TODO: Needs a "if X11_GFX == 'nvidia'" here. The workaround is only needed on Ubuntu+NVidia drivers. Other drivers are not affected, but fine with this fix.
  26. import ctypes
  27. from ctypes.util import find_library
  28. libGL = find_library("GL")
  29. ctypes.CDLL(libGL, ctypes.RTLD_GLOBAL)
  30. # When frozen, i.e. installer version, don't let PYTHONPATH mess up the search path for DLLs.
  31. if Platform.isWindows() and hasattr(sys, "frozen"):
  32. try:
  33. del os.environ["PYTHONPATH"]
  34. except KeyError:
  35. pass
  36. # WORKAROUND: GITHUB-704 GITHUB-708
  37. # It looks like setuptools creates a .pth file in
  38. # the default /usr/lib which causes the default site-packages
  39. # to be inserted into sys.path before PYTHONPATH.
  40. # This can cause issues such as having libsip loaded from
  41. # the system instead of the one provided with Cura, which causes
  42. # incompatibility issues with libArcus
  43. if "PYTHONPATH" in os.environ.keys(): # If PYTHONPATH is used
  44. PYTHONPATH = os.environ["PYTHONPATH"].split(os.pathsep) # Get the value, split it..
  45. PYTHONPATH.reverse() # and reverse it, because we always insert at 1
  46. for PATH in PYTHONPATH: # Now beginning with the last PATH
  47. PATH_real = os.path.realpath(PATH) # Making the the path "real"
  48. if PATH_real in sys.path: # This should always work, but keep it to be sure..
  49. sys.path.remove(PATH_real)
  50. sys.path.insert(1, PATH_real) # Insert it at 1 after os.curdir, which is 0.
  51. def exceptHook(hook_type, value, traceback):
  52. from cura.CrashHandler import CrashHandler
  53. _crash_handler = CrashHandler(hook_type, value, traceback)
  54. _crash_handler.show()
  55. sys.excepthook = exceptHook
  56. # Workaround for a race condition on certain systems where there
  57. # is a race condition between Arcus and PyQt. Importing Arcus
  58. # first seems to prevent Sip from going into a state where it
  59. # tries to create PyQt objects on a non-main thread.
  60. import Arcus #@UnusedImport
  61. import cura.CuraApplication
  62. import cura.Settings.CuraContainerRegistry
  63. faulthandler.enable()
  64. # Force an instance of CuraContainerRegistry to be created and reused later.
  65. cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance()
  66. # This pre-start up check is needed to determine if we should start the application at all.
  67. if not cura.CuraApplication.CuraApplication.preStartUp():
  68. sys.exit(0)
  69. app = cura.CuraApplication.CuraApplication.getInstance()
  70. app.run()