94 lines
2.7 KiB
Python
94 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Callable
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BootstrapUpdateSettings:
|
|
enabled: bool
|
|
manifest_url: str
|
|
fail_closed: bool = True
|
|
|
|
|
|
def _to_bool(value: object, default: bool = False) -> bool:
|
|
if isinstance(value, bool):
|
|
return value
|
|
if value is None:
|
|
return default
|
|
text = str(value).strip().lower()
|
|
if text in {"1", "true", "yes", "y", "on", "启用", "是"}:
|
|
return True
|
|
if text in {"0", "false", "no", "n", "off", "禁用", "否"}:
|
|
return False
|
|
return default
|
|
|
|
|
|
def _project_root() -> Path:
|
|
if getattr(sys, "frozen", False):
|
|
return Path(sys.executable).resolve().parent
|
|
return Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def load_update_settings() -> BootstrapUpdateSettings:
|
|
cfg: dict[str, object] = {}
|
|
try:
|
|
path = _project_root() / "config" / "config.json"
|
|
if path.exists():
|
|
obj = json.loads(path.read_text(encoding="utf-8"))
|
|
if isinstance(obj, dict):
|
|
cfg = obj
|
|
except Exception:
|
|
cfg = {}
|
|
|
|
raw_section = cfg.get("auto_update")
|
|
section = raw_section if isinstance(raw_section, dict) else {}
|
|
manifest_url = (
|
|
os.environ.get("DOUYIN_UPDATE_MANIFEST_URL", "")
|
|
or str(section.get("manifest_url", "") or "")
|
|
or str(cfg.get("update_manifest_url", "") or "")
|
|
).strip()
|
|
enabled = _to_bool(
|
|
os.environ.get("DOUYIN_AUTO_UPDATE_ENABLED"),
|
|
default=_to_bool(section.get("enabled"), False),
|
|
)
|
|
if os.environ.get("DOUYIN_DISABLE_AUTO_UPDATE", "").strip() == "1":
|
|
enabled = False
|
|
if not manifest_url:
|
|
enabled = False
|
|
return BootstrapUpdateSettings(
|
|
enabled=enabled,
|
|
manifest_url=manifest_url,
|
|
fail_closed=_to_bool(section.get("fail_closed"), True),
|
|
)
|
|
|
|
|
|
def run_startup_update_check_early(
|
|
app_name: str,
|
|
*,
|
|
update_check: Callable[..., object] | None = None,
|
|
) -> None:
|
|
"""Run the updater before importing the heavier app modules."""
|
|
try:
|
|
if update_check is None:
|
|
from douyin.updater.force_update import run_startup_update_check
|
|
|
|
check = run_startup_update_check
|
|
else:
|
|
check = update_check
|
|
check(app_name=app_name)
|
|
except SystemExit:
|
|
raise
|
|
except Exception as exc:
|
|
print(f"[更新] {app_name} 早期更新检查异常:{exc}", file=sys.stderr)
|
|
try:
|
|
settings = load_update_settings()
|
|
except Exception:
|
|
settings = None
|
|
if settings is not None and settings.enabled and settings.fail_closed:
|
|
raise SystemExit(3)
|