cura_app.py 3.5 KB

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