update
This commit is contained in:
126
AzA march 2026 - Kopie (18)/desktop_update_check.py
Normal file
126
AzA march 2026 - Kopie (18)/desktop_update_check.py
Normal file
@@ -0,0 +1,126 @@
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import webbrowser
|
||||
|
||||
import requests
|
||||
from aza_version import APP_CHANNEL, APP_VERSION
|
||||
|
||||
UPDATE_MANIFEST_URL = "https://api.aza-medwork.ch/download/version.json"
|
||||
_MIN_INSTALLER_BYTES = 1_000_000
|
||||
|
||||
|
||||
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_MANIFEST_URL, timeout=4)
|
||||
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_url = str(data.get("download_url") or "").strip()
|
||||
|
||||
if channel != APP_CHANNEL:
|
||||
return None
|
||||
|
||||
if latest and _is_newer_version(latest, APP_VERSION):
|
||||
return {
|
||||
"update_available": True,
|
||||
"latest_version": latest,
|
||||
"download_url": download_url,
|
||||
}
|
||||
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _download_installer(download_url: str) -> str | None:
|
||||
"""Downloads installer to TEMP. Returns path on success, None on failure."""
|
||||
try:
|
||||
target = os.path.join(tempfile.gettempdir(), "aza_desktop_setup_latest.exe")
|
||||
with requests.get(download_url, stream=True, timeout=60) as r:
|
||||
if r.status_code != 200:
|
||||
return None
|
||||
with open(target, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=512 * 1024):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
if os.path.isfile(target) and os.path.getsize(target) >= _MIN_INSTALLER_BYTES:
|
||||
return target
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def prompt_update_if_available():
|
||||
"""Prueft auf Update, zeigt Dialog, laedt Installer herunter und startet ihn."""
|
||||
info = check_for_updates()
|
||||
if not info or not info.get("update_available"):
|
||||
return
|
||||
|
||||
download_url = info.get("download_url", "")
|
||||
latest_version = info.get("latest_version", "")
|
||||
|
||||
try:
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox
|
||||
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
|
||||
answer = messagebox.askyesno(
|
||||
"Update verfuegbar",
|
||||
f"AZA {latest_version} ist verfuegbar.\n\n"
|
||||
f"Ihre Version: {APP_VERSION}\n\n"
|
||||
"Moechten Sie das Update jetzt installieren?",
|
||||
parent=root,
|
||||
)
|
||||
|
||||
if not answer:
|
||||
root.destroy()
|
||||
return
|
||||
|
||||
root.destroy()
|
||||
|
||||
installer_path = _download_installer(download_url)
|
||||
|
||||
if installer_path:
|
||||
os.startfile(installer_path)
|
||||
sys.exit(0)
|
||||
|
||||
_root2 = tk.Tk()
|
||||
_root2.withdraw()
|
||||
retry = messagebox.askyesno(
|
||||
"Download fehlgeschlagen",
|
||||
"Das Update konnte nicht heruntergeladen werden.\n\n"
|
||||
"Moechten Sie die Download-Seite im Browser oeffnen?",
|
||||
parent=_root2,
|
||||
)
|
||||
if retry and download_url:
|
||||
webbrowser.open(download_url)
|
||||
_root2.destroy()
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = check_for_updates()
|
||||
print(result)
|
||||
Reference in New Issue
Block a user