cura_app.py 4.2 KB

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