修改打包方式
All checks were successful
Publish Python Package to Gitea / publish (push) Successful in 1m2s
All checks were successful
Publish Python Package to Gitea / publish (push) Successful in 1m2s
This commit is contained in:
@@ -134,3 +134,74 @@ def apply_cli_local_defaults() -> None:
|
||||
os.environ["JIANGCHANG_DATA_ROOT"] = platform_default_data_root()
|
||||
if not (os.getenv("JIANGCHANG_USER_ID") or "").strip():
|
||||
os.environ["JIANGCHANG_USER_ID"] = DEFAULT_LOCAL_USER_ID.strip()
|
||||
def find_chrome_executable() -> "str | None":
|
||||
"""查找系统已安装的 Chrome 或 Edge 可执行文件路径。
|
||||
|
||||
查找顺序:
|
||||
1. 常见文件路径(PROGRAMFILES、LOCALAPPDATA、macOS、Linux)
|
||||
2. Windows 注册表 App Paths(HKLM 优先,HKCU 其次)
|
||||
3. PATH shutil.which 兜底
|
||||
|
||||
返回找到的路径字符串,找不到返回 None。
|
||||
宿主/技能调用 Playwright launch 时应传 executable_path=find_chrome_executable()
|
||||
而非 channel="chrome",以避免 Playwright 内部检测在系统级安装时失效。
|
||||
"""
|
||||
import shutil
|
||||
|
||||
if sys.platform == "win32":
|
||||
local_app_data = os.environ.get("LOCALAPPDATA", "")
|
||||
prog_files = os.environ.get("PROGRAMFILES", r"C:\Program Files")
|
||||
prog_files_x86 = os.environ.get("PROGRAMFILES(X86)", r"C:\Program Files (x86)")
|
||||
candidates = [
|
||||
os.path.join(prog_files, r"Google\Chrome\Application\chrome.exe"),
|
||||
os.path.join(prog_files_x86, r"Google\Chrome\Application\chrome.exe"),
|
||||
os.path.join(local_app_data, r"Google\Chrome\Application\chrome.exe"),
|
||||
os.path.join(prog_files, r"Microsoft\Edge\Application\msedge.exe"),
|
||||
os.path.join(prog_files_x86, r"Microsoft\Edge\Application\msedge.exe"),
|
||||
os.path.join(local_app_data, r"Microsoft\Edge\Application\msedge.exe"),
|
||||
]
|
||||
elif sys.platform == "darwin":
|
||||
candidates = [
|
||||
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
||||
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
||||
"/Applications/Chromium.app/Contents/MacOS/Chromium",
|
||||
]
|
||||
else:
|
||||
candidates = [
|
||||
"/usr/bin/google-chrome-stable",
|
||||
"/usr/bin/google-chrome",
|
||||
"/usr/bin/chromium-browser",
|
||||
"/usr/bin/chromium",
|
||||
"/usr/bin/microsoft-edge",
|
||||
]
|
||||
|
||||
for path in candidates:
|
||||
if path and os.path.isfile(path):
|
||||
return path
|
||||
|
||||
# Windows 注册表兜底(覆盖系统级 HKLM 安装)
|
||||
if sys.platform == "win32":
|
||||
try:
|
||||
import winreg
|
||||
for hive in (winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER):
|
||||
for reg_path in (
|
||||
r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe",
|
||||
r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe",
|
||||
):
|
||||
try:
|
||||
with winreg.OpenKey(hive, reg_path) as key:
|
||||
val, _ = winreg.QueryValueEx(key, "")
|
||||
if val and os.path.isfile(val):
|
||||
return val
|
||||
except OSError:
|
||||
continue
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# PATH 兜底
|
||||
for name in ("google-chrome-stable", "google-chrome", "chrome", "msedge", "chromium"):
|
||||
found = shutil.which(name)
|
||||
if found:
|
||||
return found
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user