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