-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2p.py
143 lines (121 loc) · 3.87 KB
/
p2p.py
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
139
140
141
142
143
import os
import sys
def main():
if sys.version_info[:2] < (3, 10):
print("Error: Python version 3.10 or later is required.")
sys.exit(0)
if "--silent" not in sys.argv:
print("- Starting the Peer-to-Peer Network…")
main = None
try:
import main
main.start()
except Exception as err:
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 = config.log_dir + "/error.log"
traceback.print_exc(file=open(error_log_path, "w"))
print("---")
print(
"Please report it:"
" https://github.com/peertopeernetwork/p2p/issues/new"
)
if (
sys.platform.startswith("win")
and "python.exe" not in sys.executable
):
displayErrorMessage(err, error_log_path)
if main and (main.update_after_shutdown or main.restart_after_shutdown):
if main.update_after_shutdown:
print("Shutting down…")
prepareShutdown()
import update
print("Updating…")
update.update()
if main.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_ICONEXCLAIMATION = 0x30
ID_YES = 0x6
ID_NO = 0x7
ID_CANCEL = 0x2
err_message = "%s: %s" % (type(err).__name__, err)
err_title = "Unhandled exception: %s\nReport error?" % err_message
res = ctypes.windll.user32.MessageBoxW(
0,
err_title,
"Peer-to-Peer Network error",
MB_YESNOCANCEL | MB_ICONEXCLAIMATION,
)
if res == ID_YES:
import webbrowser
report_url = "https://github.com/peertopeernetwork/p2p/issues/new"
webbrowser.open(
report_url
% urllib.parse.quote("Unhandled exception: %s" % err_message)
)
if res in [ID_YES, ID_NO]:
subprocess.Popen(["notepad.exe", error_log_path])
def prepareShutdown():
import atexit
atexit._run_exitfuncs()
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)
def restart():
args = sys.argv[:]
sys.executable = sys.executable.replace(".pkg", "")
if not getattr(sys, "frozen", False):
args.insert(0, sys.executable)
if "--open_browser" in args:
del args[args.index("--open_browser") + 1]
del args[args.index("--open_browser")]
if getattr(sys, "frozen", False):
pos_first_arg = 1
else:
pos_first_arg = 2
args.insert(pos_first_arg, "--open_browser")
args.insert(pos_first_arg + 1, "False")
if sys.platform == "win32":
args = ['"%s"' % arg for arg in args]
try:
print("Executing %s %s" % (sys.executable, args))
os.execv(sys.executable, args)
except Exception as err:
print("Execution error: %s" % err)
print("Bye.")
def start():
app_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(app_dir)
sys.path.insert(0, os.path.join(app_dir, "src/lib"))
sys.path.insert(0, os.path.join(app_dir, "src"))
if "--update" in sys.argv:
sys.argv.remove("--update")
print("Updating…")
import update
update.update()
else:
main()
if __name__ == "__main__":
start()