cura_app.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2018 Ultimaker B.V.
  3. # Cura is released under the terms of the LGPLv3 or higher.
  4. import argparse
  5. import faulthandler
  6. import os
  7. import sys
  8. from UM.Platform import Platform
  9. from cura.ApplicationMetadata import CuraAppName
  10. parser = argparse.ArgumentParser(prog = "cura",
  11. add_help = False)
  12. parser.add_argument("--debug",
  13. action="store_true",
  14. default = False,
  15. help = "Turn on the debug mode by setting this option."
  16. )
  17. known_args = vars(parser.parse_known_args()[0])
  18. if not known_args["debug"]:
  19. def get_cura_dir_path():
  20. if Platform.isWindows():
  21. appdata_path = os.getenv("APPDATA")
  22. if not appdata_path: #Defensive against the environment variable missing (should never happen).
  23. appdata_path = "."
  24. return os.path.join(appdata_path, CuraAppName)
  25. elif Platform.isLinux():
  26. return os.path.expanduser("~/.local/share/" + CuraAppName)
  27. elif Platform.isOSX():
  28. return os.path.expanduser("~/Library/Logs/" + CuraAppName)
  29. # Do not redirect stdout and stderr to files if we are running CLI.
  30. if hasattr(sys, "frozen") and "cli" not in os.path.basename(sys.argv[0]).lower():
  31. dirpath = get_cura_dir_path()
  32. os.makedirs(dirpath, exist_ok = True)
  33. sys.stdout = open(os.path.join(dirpath, "stdout.log"), "w", encoding = "utf-8")
  34. sys.stderr = open(os.path.join(dirpath, "stderr.log"), "w", encoding = "utf-8")
  35. # WORKAROUND: GITHUB-88 GITHUB-385 GITHUB-612
  36. if Platform.isLinux(): # Needed for platform.linux_distribution, which is not available on Windows and OSX
  37. # For Ubuntu: https://bugs.launchpad.net/ubuntu/+source/python-qt4/+bug/941826
  38. # The workaround is only needed on Ubuntu+NVidia drivers. Other drivers are not affected, but fine with this fix.
  39. try:
  40. import ctypes
  41. from ctypes.util import find_library
  42. libGL = find_library("GL")
  43. ctypes.CDLL(libGL, ctypes.RTLD_GLOBAL)
  44. except:
  45. # GLES-only systems (e.g. ARM Mali) do not have libGL, ignore error
  46. pass
  47. # When frozen, i.e. installer version, don't let PYTHONPATH mess up the search path for DLLs.
  48. if Platform.isWindows() and hasattr(sys, "frozen"):
  49. try:
  50. del os.environ["PYTHONPATH"]
  51. except KeyError:
  52. pass
  53. # WORKAROUND: GITHUB-704 GITHUB-708
  54. # It looks like setuptools creates a .pth file in
  55. # the default /usr/lib which causes the default site-packages
  56. # to be inserted into sys.path before PYTHONPATH.
  57. # This can cause issues such as having libsip loaded from
  58. # the system instead of the one provided with Cura, which causes
  59. # incompatibility issues with libArcus
  60. if "PYTHONPATH" in os.environ.keys(): # If PYTHONPATH is used
  61. PYTHONPATH = os.environ["PYTHONPATH"].split(os.pathsep) # Get the value, split it..
  62. PYTHONPATH.reverse() # and reverse it, because we always insert at 1
  63. for PATH in PYTHONPATH: # Now beginning with the last PATH
  64. PATH_real = os.path.realpath(PATH) # Making the the path "real"
  65. if PATH_real in sys.path: # This should always work, but keep it to be sure..
  66. sys.path.remove(PATH_real)
  67. sys.path.insert(1, PATH_real) # Insert it at 1 after os.curdir, which is 0.
  68. def exceptHook(hook_type, value, traceback):
  69. from cura.CrashHandler import CrashHandler
  70. from cura.CuraApplication import CuraApplication
  71. has_started = False
  72. if CuraApplication.Created:
  73. has_started = CuraApplication.getInstance().started
  74. #
  75. # When the exception hook is triggered, the QApplication may not have been initialized yet. In this case, we don't
  76. # have an QApplication to handle the event loop, which is required by the Crash Dialog.
  77. # The flag "CuraApplication.Created" is set to True when CuraApplication finishes its constructor call.
  78. #
  79. # Before the "started" flag is set to True, the Qt event loop has not started yet. The event loop is a blocking
  80. # call to the QApplication.exec_(). In this case, we need to:
  81. # 1. Remove all scheduled events so no more unnecessary events will be processed, such as loading the main dialog,
  82. # loading the machine, etc.
  83. # 2. Start the Qt event loop with exec_() and show the Crash Dialog.
  84. #
  85. # If the application has finished its initialization and was running fine, and then something causes a crash,
  86. # we run the old routine to show the Crash Dialog.
  87. #
  88. from PyQt5.Qt import QApplication
  89. if CuraApplication.Created:
  90. _crash_handler = CrashHandler(hook_type, value, traceback, has_started)
  91. if CuraApplication.splash is not None:
  92. CuraApplication.splash.close()
  93. if not has_started:
  94. CuraApplication.getInstance().removePostedEvents(None)
  95. _crash_handler.early_crash_dialog.show()
  96. sys.exit(CuraApplication.getInstance().exec_())
  97. else:
  98. _crash_handler.show()
  99. else:
  100. application = QApplication(sys.argv)
  101. application.removePostedEvents(None)
  102. _crash_handler = CrashHandler(hook_type, value, traceback, has_started)
  103. # This means the QtApplication could be created and so the splash screen. Then Cura closes it
  104. if CuraApplication.splash is not None:
  105. CuraApplication.splash.close()
  106. _crash_handler.early_crash_dialog.show()
  107. sys.exit(application.exec_())
  108. # Set exception hook to use the crash dialog handler
  109. sys.excepthook = exceptHook
  110. # Enable dumping traceback for all threads
  111. faulthandler.enable(all_threads = True)
  112. # Workaround for a race condition on certain systems where there
  113. # is a race condition between Arcus and PyQt. Importing Arcus
  114. # first seems to prevent Sip from going into a state where it
  115. # tries to create PyQt objects on a non-main thread.
  116. import Arcus #@UnusedImport
  117. import Savitar #@UnusedImport
  118. from cura.CuraApplication import CuraApplication
  119. app = CuraApplication()
  120. app.run()