No internet connection handling

This commit is contained in:
Faugus
2026-01-07 19:18:58 -03:00
parent 82942be886
commit 56d67ba2c3
2 changed files with 51 additions and 29 deletions

View File

@@ -14,12 +14,14 @@ UMU_VERSION_FILE = os.path.join(UMU_INSTALL_DIR, "version.txt")
UMU_BIN_PATH = os.path.join(UMU_INSTALL_DIR, "umu-run")
def get_latest_umu_version():
r = requests.get(UMU_VERSION_API)
if r.status_code == 200:
releases = r.json()
if releases:
return releases[0]["tag_name"]
return None
try:
r = requests.get(UMU_VERSION_API, timeout=5)
if r.status_code == 200:
releases = r.json()
if releases:
return releases[0]["tag_name"]
except Exception:
return None
def get_installed_umu_version():
if os.path.exists(UMU_VERSION_FILE):
@@ -28,23 +30,34 @@ def get_installed_umu_version():
return None
def download_umu_run(version):
os.makedirs(UMU_INSTALL_DIR, exist_ok=True)
url = UMU_URL_TEMPLATE.format(version)
r = requests.get(url)
if r.status_code == 200:
try:
os.makedirs(UMU_INSTALL_DIR, exist_ok=True)
url = UMU_URL_TEMPLATE.format(version)
r = requests.get(url, timeout=10)
if r.status_code != 200:
return
with open(UMU_BIN_PATH, "wb") as f:
f.write(r.content)
os.chmod(UMU_BIN_PATH, 0o755)
with open(UMU_VERSION_FILE, "w") as f:
f.write(version)
print("Updating UMU-Launcher...", version, flush=True)
else:
print("Failed to download UMU-Launcher:", r.status_code, flush=True)
except Exception:
return
def update_umu():
latest = get_latest_umu_version()
current = get_installed_umu_version()
if latest and (latest != current or not os.path.exists(UMU_BIN_PATH)):
if latest is None:
return
if latest != current or not os.path.exists(UMU_BIN_PATH):
download_umu_run(latest)
else:
print("UMU-Launcher is up to date.", flush=True)
@@ -59,12 +72,12 @@ REPO_URL = "https://api.github.com/repos/Faugus/components/releases/latest"
VERSION_FILE = f"{DOWNLOAD_DIR}/version.txt"
def get_latest_version():
response = requests.get(REPO_URL)
if response.status_code == 200:
release_info = response.json()
return release_info['tag_name']
else:
print(f"Failed to access {REPO_URL}. Status code: {response.status_code}", flush=True)
try:
response = requests.get(REPO_URL, timeout=5)
if response.status_code == 200:
release_info = response.json()
return release_info['tag_name']
except Exception:
return None
def get_installed_version():
@@ -74,11 +87,14 @@ def get_installed_version():
return None
def download_and_extract(url, download_dir):
file_name = url.split('/')[-1]
download_path = os.path.join(download_dir, file_name)
try:
file_name = url.split('/')[-1]
download_path = os.path.join(download_dir, file_name)
response = requests.get(url, stream=True, timeout=15)
if response.status_code != 200:
return
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(download_path, "wb") as f:
f.write(response.content)
@@ -86,8 +102,8 @@ def download_and_extract(url, download_dir):
tar.extractall(path=download_dir, filter=lambda tarinfo, path: tarinfo)
os.remove(download_path)
else:
print(f"Failed to download {file_name}. Status code: {response.status_code}", flush=True)
except Exception:
return
def check_for_updates():
latest_version = get_latest_version()

View File

@@ -27,8 +27,11 @@ CONFIGS = {
def get_latest_tag_and_url(api, archive_ext):
with urllib.request.urlopen(api) as r:
data = json.loads(r.read())
try:
with urllib.request.urlopen(api, timeout=5) as r:
data = json.loads(r.read())
except Exception:
return None, None
asset = next(
(a for a in data.get("assets", []) if a["name"].endswith(archive_ext)),
@@ -72,8 +75,11 @@ def install_proton_latest(proton_dir, url, label):
archive = STEAM_COMPAT_DIR / url.split("/")[-1]
tmp = STEAM_COMPAT_DIR / "__proton_tmp__"
print(f"Downloading {label}...", flush=True)
urllib.request.urlretrieve(url, archive)
try:
print(f"Downloading {label}...", flush=True)
urllib.request.urlretrieve(url, archive)
except Exception:
return
print(f"Extracting {label}...", flush=True)
tmp.mkdir(exist_ok=True)