Files
live-hub-py/web/backend/version.py
T

25 lines
807 B
Python

"""应用版本信息。"""
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("douyu-login-py")
except PackageNotFoundError:
return "0.0.0"