zeronet.py 4.1 KB

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