fix(client): improve version retrieval logic and handle JSON errors

This commit is contained in:
dw-0
2025-10-27 19:00:08 +01:00
parent 229f317025
commit 6f0e0146ef

View File

@@ -11,6 +11,7 @@ from __future__ import annotations
import json import json
import re import re
import shutil import shutil
from json import JSONDecodeError
from pathlib import Path from pathlib import Path
from subprocess import PIPE, CalledProcessError, run from subprocess import PIPE, CalledProcessError, run
from typing import List, get_args from typing import List, get_args
@@ -151,18 +152,36 @@ def symlink_webui_nginx_log(
def get_local_client_version(client: BaseWebClient) -> str | None: def get_local_client_version(client: BaseWebClient) -> str | None:
relinfo_file = client.client_dir.joinpath("release_info.json") relinfo_file = client.client_dir.joinpath("release_info.json")
version_file = client.client_dir.joinpath(".version") version_file = client.client_dir.joinpath(".version")
default = "n/a"
if not client.client_dir.exists(): if not client.client_dir.exists():
return None return default
if not relinfo_file.is_file() and not version_file.is_file():
return "n/a"
# try to get version from release_info.json first
if relinfo_file.is_file(): if relinfo_file.is_file():
with open(relinfo_file, "r") as f: try:
return str(json.load(f)["version"]) if relinfo_file.stat().st_size == 0:
else: raise JSONDecodeError("Empty file", "", 0)
with open(version_file, "r") as f: with open(relinfo_file, "r", encoding="utf-8") as f:
return f.readlines()[0] data = json.load(f)
raw_version = data.get("version")
if raw_version is not None:
parsed = str(raw_version).strip()
if parsed:
return parsed
except (JSONDecodeError, OSError):
Logger.print_error("Invalid 'release_info.json'")
# fallback to .version file
if version_file.is_file():
try:
with open(version_file, "r") as f:
line = f.readline().strip()
return line or default
except OSError:
Logger.print_error("Unable to read '.version'")
return default
def get_remote_client_version(client: BaseWebClient) -> str | None: def get_remote_client_version(client: BaseWebClient) -> str | None: