cura_app.py 4.2 KB

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