forked from MultiworldGG/MultiworldGG
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdater.py
More file actions
58 lines (50 loc) · 2.3 KB
/
Copy pathUpdater.py
File metadata and controls
58 lines (50 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import tempfile, os, subprocess, requests
import logging
from Utils import normalize_tag, tuplize_version, is_windows, is_linux, is_macos
GITHUB_OWNER = "MultiworldGG"
GITHUB_REPO = "MultiworldGG"
GITHUB_API_LATEST = (
f"https://api.github.com/repos/{GITHUB_OWNER}/{GITHUB_REPO}/releases/latest"
)
def select_installer_asset(assets: list[dict]) -> dict:
if is_windows:
release_assets = [a for a in assets if a["name"].lower().endswith(".exe")]
elif is_linux:
release_assets = [a for a in assets if a["name"].lower().endswith(".appimage")]
elif is_macos:
release_assets = [a for a in assets if a["name"].lower().endswith(".dmg")]
else:
raise RuntimeError("This platform is not supported.")
if not release_assets:
raise RuntimeError("No feasible installer found in latest release for this platform.")
return release_assets[0]
def get_latest_release_info() -> tuple:
resp = requests.get(GITHUB_API_LATEST, headers={"Accept":"application/vnd.github.v3+json"})
resp.raise_for_status()
data = resp.json()
tag = normalize_tag(data["tag_name"])
installer = select_installer_asset(data["assets"])
download_url = installer["browser_download_url"]
changelog = data.get("body") or "No changelog available."
logging.info(f"latest release {tag} under url {download_url}")
return tuplize_version(tag), download_url, changelog
def download_and_install_win(url: str, progress_callback=None):
"""Download installer to a temp file and launch it.
progress_callback(bytes_downloaded, total_bytes) is called from the
download thread whenever a chunk is written. total_bytes is -1 when the
server does not report Content-Length.
"""
fd, path = tempfile.mkstemp(suffix=".exe")
os.close(fd)
with requests.get(url, stream=True) as r:
r.raise_for_status()
total = int(r.headers.get("Content-Length", -1))
downloaded = 0
with open(path, "wb") as f:
for chunk in r.iter_content(8192):
f.write(chunk)
downloaded += len(chunk)
if progress_callback:
progress_callback(downloaded, total)
subprocess.Popen([path, "/SILENT", "/SUPPRESSMSGBOXES", "/RESTARTAPPLICATIONS", "/TASKS=deletelib"], shell=False)
os._exit(0)