45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""应用版本信息。"""
|
|
|
|
import hashlib
|
|
import tomllib
|
|
from functools import lru_cache
|
|
from importlib.metadata import PackageNotFoundError
|
|
from importlib.metadata import version as package_version
|
|
from pathlib import Path
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def get_app_version() -> str:
|
|
"""从 pyproject.toml 读取应用版本,读取失败时回退到包元数据。"""
|
|
pyproject_path = Path(__file__).resolve().parents[2] / "pyproject.toml"
|
|
if pyproject_path.exists():
|
|
with pyproject_path.open("rb") as file:
|
|
pyproject = tomllib.load(file)
|
|
version = pyproject.get("project", {}).get("version")
|
|
if isinstance(version, str) and version:
|
|
return version
|
|
|
|
try:
|
|
return package_version("live-hub-py")
|
|
except PackageNotFoundError:
|
|
return "0.0.0"
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def get_frontend_release_id() -> str:
|
|
"""当前前端构建的稳定标识。
|
|
|
|
Vite 的 index.html 引用带内容哈希的 JS/CSS 文件,因此构建产物变化时
|
|
index.html 也会变化。只升级 pyproject 版本不足以识别同版本重新部署,
|
|
所以把它和版本号组合成 release_id,供前端判断是否需要刷新。
|
|
"""
|
|
version = get_app_version()
|
|
index_path = (
|
|
Path(__file__).resolve().parents[2] / "web" / "frontend" / "dist" / "index.html"
|
|
)
|
|
try:
|
|
digest = hashlib.sha256(index_path.read_bytes()).hexdigest()[:12]
|
|
except OSError:
|
|
digest = "source"
|
|
return f"{version}+{digest}"
|