zeronet.py 4.3 KB

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