-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpp_browser.py
More file actions
69 lines (60 loc) · 2.71 KB
/
Copy pathpp_browser.py
File metadata and controls
69 lines (60 loc) · 2.71 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
"""PP 浏览器 —— 自定义指纹浏览器(入口)。
仅负责日志初始化与启动主窗口;具体实现见 app 包。
"""
import logging
import os
import shutil
import sys
# QtWebEngine 在 Nuitka standalone 打包后,Chromium 渲染子进程
# (QtWebEngineProcess) 常因 sandbox / GPU / dev-shm 限制无法启动,
# 表现为创建 QWebEngineProfile 时静默卡死。必须在创建 QApplication
# 之前设置以下环境变量禁用相关限制。
os.environ.setdefault(
"QTWEBENGINE_CHROMIUM_FLAGS",
"--no-sandbox --disable-gpu --disable-dev-shm-usage",
)
os.environ.setdefault("QTWEBENGINE_DISABLE_SANDBOX", "1")
# 配置日志:必须在导入 app 之前完成,
# 以便 browserforge 离线补丁阶段的日志能正常输出到文件与控制台。
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("browser.log", encoding="utf-8"),
logging.StreamHandler(sys.stdout),
],
)
def _prepare_webengine_resources():
"""Nuitka 打包后,QtWebEngine 的 Chromium 渲染子进程
(QtWebEngineProcess.exe) 需要在自身同级目录找到 icudtl.dat、
qtwebengine_*.pak 以及 locales/,但 Nuitka 默认把它们放在
resources/ 与 translations/qtwebengine_locales/ 子目录,
导致 'Couldn't mmap icu data file' 并卡死在 QWebEngineProfile
创建。这里在启动前(若缺失)把它们复制到 exe 同级目录,实现自愈。
"""
if not getattr(sys, "frozen", False):
return
base = os.path.dirname(os.path.abspath(sys.executable))
res_src = os.path.join(base, "resources")
if os.path.isdir(res_src):
for name in os.listdir(res_src):
if name == "icudtl.dat" or name.endswith(".pak"):
dst = os.path.join(base, name)
if not os.path.exists(dst):
try:
shutil.copy2(os.path.join(res_src, name), dst)
logging.debug("WebEngine 资源已就位: %s", name)
except OSError as exc: # noqa: BLE001
logging.warning("复制 WebEngine 资源 %s 失败: %s", name, exc)
loc_src = os.path.join(base, "translations", "qtwebengine_locales")
loc_dst = os.path.join(base, "locales")
if os.path.isdir(loc_src) and not os.path.isdir(loc_dst):
try:
shutil.copytree(loc_src, loc_dst)
logging.debug("WebEngine locales 已就位: %s", loc_dst)
except OSError as exc: # noqa: BLE001
logging.warning("复制 WebEngine locales 失败: %s", exc)
_prepare_webengine_resources()
from app.main_window import main
if __name__ == "__main__":
main()