121 lines
3.3 KiB
Python
121 lines
3.3 KiB
Python
|
|
import os
|
||
|
|
import subprocess
|
||
|
|
import tempfile
|
||
|
|
|
||
|
|
import requests
|
||
|
|
from aza_version import APP_CHANNEL, APP_VERSION
|
||
|
|
|
||
|
|
UPDATE_URL = "https://your-domain/release/version.json"
|
||
|
|
CURRENT_VERSION = APP_VERSION
|
||
|
|
CURRENT_CHANNEL = APP_CHANNEL
|
||
|
|
|
||
|
|
|
||
|
|
def _is_newer_version(latest: str, current: str) -> bool:
|
||
|
|
def parse(v: str):
|
||
|
|
parts = []
|
||
|
|
for p in str(v).strip().split("."):
|
||
|
|
try:
|
||
|
|
parts.append(int(p))
|
||
|
|
except Exception:
|
||
|
|
parts.append(0)
|
||
|
|
while len(parts) < 3:
|
||
|
|
parts.append(0)
|
||
|
|
return tuple(parts[:3])
|
||
|
|
|
||
|
|
return parse(latest) > parse(current)
|
||
|
|
|
||
|
|
|
||
|
|
def check_for_updates():
|
||
|
|
try:
|
||
|
|
r = requests.get(UPDATE_URL, timeout=5)
|
||
|
|
if r.status_code != 200:
|
||
|
|
return None
|
||
|
|
|
||
|
|
data = r.json()
|
||
|
|
latest = str(data.get("version") or "").strip()
|
||
|
|
channel = str(data.get("channel") or "stable").strip()
|
||
|
|
download = data.get("download_url")
|
||
|
|
minimum_supported_version = str(data.get("minimum_supported_version") or "").strip()
|
||
|
|
installer_type = str(data.get("installer_type") or "").strip()
|
||
|
|
release_notes = data.get("release_notes") or []
|
||
|
|
|
||
|
|
if channel != CURRENT_CHANNEL:
|
||
|
|
return {
|
||
|
|
"update_available": False,
|
||
|
|
"reason": "channel_mismatch",
|
||
|
|
"channel": channel,
|
||
|
|
}
|
||
|
|
|
||
|
|
if latest and _is_newer_version(latest, CURRENT_VERSION):
|
||
|
|
return {
|
||
|
|
"update_available": True,
|
||
|
|
"latest_version": latest,
|
||
|
|
"download_url": download,
|
||
|
|
"minimum_supported_version": minimum_supported_version,
|
||
|
|
"installer_type": installer_type,
|
||
|
|
"release_notes": release_notes,
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
"update_available": False,
|
||
|
|
"channel": channel,
|
||
|
|
"minimum_supported_version": minimum_supported_version,
|
||
|
|
"installer_type": installer_type,
|
||
|
|
}
|
||
|
|
|
||
|
|
except Exception:
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def download_update_installer(download_url: str, target_dir: str | None = None) -> str | None:
|
||
|
|
try:
|
||
|
|
url = str(download_url or "").strip()
|
||
|
|
if not url:
|
||
|
|
return None
|
||
|
|
|
||
|
|
if target_dir is None:
|
||
|
|
target_dir = os.path.join(tempfile.gettempdir(), "aza_updates")
|
||
|
|
|
||
|
|
os.makedirs(target_dir, exist_ok=True)
|
||
|
|
target_path = os.path.join(target_dir, "aza_desktop_setup.exe")
|
||
|
|
|
||
|
|
with requests.get(url, stream=True, timeout=30) as r:
|
||
|
|
if r.status_code != 200:
|
||
|
|
return None
|
||
|
|
|
||
|
|
with open(target_path, "wb") as f:
|
||
|
|
for chunk in r.iter_content(chunk_size=1024 * 1024):
|
||
|
|
if chunk:
|
||
|
|
f.write(chunk)
|
||
|
|
|
||
|
|
if not os.path.exists(target_path):
|
||
|
|
return None
|
||
|
|
|
||
|
|
if os.path.getsize(target_path) <= 0:
|
||
|
|
return None
|
||
|
|
|
||
|
|
return target_path
|
||
|
|
|
||
|
|
except Exception:
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def launch_update_installer(installer_path: str) -> bool:
|
||
|
|
try:
|
||
|
|
path = str(installer_path or "").strip()
|
||
|
|
if not path or not os.path.exists(path):
|
||
|
|
return False
|
||
|
|
|
||
|
|
if os.name == "nt":
|
||
|
|
subprocess.Popen([path], shell=False)
|
||
|
|
return True
|
||
|
|
|
||
|
|
return False
|
||
|
|
except Exception:
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
result = check_for_updates()
|
||
|
|
print(result)
|