zeronet.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. from src.Config import config
  5. # fix further imports from src dir
  6. sys.modules['Config'] = sys.modules['src.Config']
  7. def launch():
  8. '''renamed from main to avoid clashes with main module'''
  9. if sys.version_info.major < 3:
  10. print("Error: Python 3.x is required")
  11. sys.exit(0)
  12. if '--silent' not in sys.argv:
  13. from greet import fancy_greet
  14. fancy_greet(config.version)
  15. try:
  16. import main
  17. main.start()
  18. except Exception as err: # Prevent closing
  19. import traceback
  20. try:
  21. import logging
  22. logging.exception("Unhandled exception: %s" % err)
  23. except Exception as log_err:
  24. print("Failed to log error:", log_err)
  25. traceback.print_exc()
  26. error_log_path = config.log_dir + "/error.log"
  27. traceback.print_exc(file=open(error_log_path, "w"))
  28. print("---")
  29. print("Please report it: https://github.com/zeronet-conservancy/zeronet-conservancy/issues/new?template=bug-report.md")
  30. if sys.platform.startswith("win") and "python.exe" not in sys.executable:
  31. displayErrorMessage(err, error_log_path)
  32. if main and (main.update_after_shutdown or main.restart_after_shutdown): # Updater
  33. if main.update_after_shutdown:
  34. print("Shutting down...")
  35. prepareShutdown()
  36. import update
  37. print("Updating...")
  38. update.update()
  39. if main.restart_after_shutdown:
  40. print("Restarting...")
  41. restart()
  42. else:
  43. print("Shutting down...")
  44. prepareShutdown()
  45. print("Restarting...")
  46. restart()
  47. def displayErrorMessage(err, error_log_path):
  48. import ctypes
  49. import urllib.parse
  50. import subprocess
  51. MB_YESNOCANCEL = 0x3
  52. MB_ICONEXCLAIMATION = 0x30
  53. ID_YES = 0x6
  54. ID_NO = 0x7
  55. ID_CANCEL = 0x2
  56. err_message = "%s: %s" % (type(err).__name__, err)
  57. err_title = "Unhandled exception: %s\nReport error?" % err_message
  58. res = ctypes.windll.user32.MessageBoxW(0, err_title, "ZeroNet error", MB_YESNOCANCEL | MB_ICONEXCLAIMATION)
  59. if res == ID_YES:
  60. import webbrowser
  61. report_url = "https://github.com/zeronet-conservancy/zeronet-conservancy/issues/new"
  62. webbrowser.open(report_url)
  63. if res in [ID_YES, ID_NO]:
  64. subprocess.Popen(['notepad.exe', error_log_path])
  65. def prepareShutdown():
  66. import atexit
  67. atexit._run_exitfuncs()
  68. # Close log files
  69. if "main" in sys.modules:
  70. logger = sys.modules["main"].logging.getLogger()
  71. for handler in logger.handlers[:]:
  72. handler.flush()
  73. handler.close()
  74. logger.removeHandler(handler)
  75. import time
  76. time.sleep(1) # Wait for files to close
  77. def restart():
  78. args = sys.argv[:]
  79. sys.executable = sys.executable.replace(".pkg", "") # Frozen mac fix
  80. if not getattr(sys, 'frozen', False):
  81. args.insert(0, sys.executable)
  82. # Don't open browser after restart
  83. if "--open_browser" in args:
  84. del args[args.index("--open_browser") + 1] # argument value
  85. del args[args.index("--open_browser")] # argument key
  86. if getattr(sys, 'frozen', False):
  87. pos_first_arg = 1 # Only the executable
  88. else:
  89. pos_first_arg = 2 # Interpter, .py file path
  90. args.insert(pos_first_arg, "--open_browser")
  91. args.insert(pos_first_arg + 1, "False")
  92. if sys.platform == 'win32':
  93. args = ['"%s"' % arg for arg in args]
  94. try:
  95. print("Executing %s %s" % (sys.executable, args))
  96. os.execv(sys.executable, args)
  97. except Exception as err:
  98. print("Execv error: %s" % err)
  99. print("Bye.")
  100. def start():
  101. config.working_dir = os.getcwd()
  102. app_dir = os.path.dirname(os.path.abspath(__file__))
  103. os.chdir(app_dir) # Change working dir to zeronet.py dir
  104. sys.path.insert(0, os.path.join(app_dir, "src/lib")) # External liblary directory
  105. sys.path.insert(0, os.path.join(app_dir, "src")) # Imports relative to src
  106. if "--update" in sys.argv:
  107. sys.argv.remove("--update")
  108. print("Updating...")
  109. import update
  110. update.update()
  111. else:
  112. launch()
  113. if __name__ == '__main__':
  114. start()