-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupdate_manager.py
More file actions
1153 lines (1007 loc) · 41.7 KB
/
update_manager.py
File metadata and controls
1153 lines (1007 loc) · 41.7 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# update_manager.py
# -*- coding: utf-8 -*-
"""
Cross-platform in-app updater for SaveState.
Design constraints honoured:
- No network activity unless the user opts in ("check_updates_on_startup").
- No admin/root privileges required: the swap is performed via a helper
script spawned at exit, which only touches files the running user owns.
- Updates are driven exclusively by GitHub Releases (stable tagged builds).
Commits / branches / pre-releases are ignored, so the author can push
experimental work to `main` without triggering updates.
- Supported install layouts (see detect_install_type):
* "frozen" - Nuitka / PyInstaller binary (onefile .exe or onefolder .zip).
Linux .AppImage / .tar.gz also handled here.
* "source" - running from source (`python main.py`). The updater pulls
the GitHub-generated source zipball and overlays it on
the project root, then restarts the interpreter.
* "managed" - Flatpak / Snap / read-only install. Download works, but
auto-install is refused and the user gets a GitHub link.
Threading model:
- UpdateManager is a QObject living on the GUI thread. It owns two
QThread workers (check / download). The download worker survives the
UpdateDialog being closed, so progress keeps going while the UI is
hidden. The dialog is just a view on UpdateManager's state.
"""
from __future__ import annotations
import logging
import os
import platform
import re
import shutil
import stat
import sys
import tempfile
import zipfile
from typing import Optional
import requests
from PySide6.QtCore import QObject, QThread, Signal, Slot
import config
# --- Public state strings (also used as UI hints) ---
STATE_IDLE = "idle"
STATE_CHECKING = "checking"
STATE_UP_TO_DATE = "up_to_date"
STATE_UPDATE_AVAILABLE = "update_available"
STATE_DOWNLOADING = "downloading"
STATE_DOWNLOADED = "downloaded"
STATE_ERROR = "error"
STATE_UNSUPPORTED = "unsupported"
# --- Install type enum (plain strings to keep signals simple) ---
INSTALL_SOURCE = "source"
INSTALL_FROZEN = "frozen"
INSTALL_MANAGED = "managed"
# Captured at import time so that later argv mutation (e.g. by Qt, tests,
# or our own code) can't break the "restart the same command" logic in
# source mode.
_ORIGINAL_ARGV = list(sys.argv)
_ORIGINAL_PYTHON = sys.executable
# ---------------------------------------------------------------------------
# Install layout detection
# ---------------------------------------------------------------------------
def _is_frozen() -> bool:
"""True when running from a packaged binary (Nuitka / PyInstaller)."""
if getattr(sys, "frozen", False):
return True
try:
import builtins # noqa: WPS433
if hasattr(builtins, "__compiled__"):
return True
except Exception:
pass
return "__compiled__" in globals()
def _get_nuitka_compiled_attr(name: str) -> Optional[str]:
"""Return a Nuitka ``__compiled__`` attribute when available."""
compiled = globals().get("__compiled__")
if compiled is None:
try:
import builtins # noqa: WPS433
compiled = getattr(builtins, "__compiled__", None)
except Exception:
compiled = None
value = getattr(compiled, name, None) if compiled is not None else None
return value if isinstance(value, str) and value else None
def _normalise_exe_candidate(path: Optional[str]) -> Optional[str]:
"""Expand and validate an executable path candidate."""
if not path:
return None
cleaned = os.path.expandvars(os.path.expanduser(str(path).strip().strip('"')))
if not cleaned:
return None
return os.path.abspath(cleaned)
def _is_temp_path(path: str) -> bool:
"""Best-effort check for paths inside the current user's temp folder."""
try:
temp_root = os.path.abspath(tempfile.gettempdir())
return os.path.commonpath([temp_root, os.path.abspath(path)]) == temp_root
except Exception:
return False
def _get_executable_path() -> str:
"""Path of the on-disk executable for frozen builds.
Several special cases must be handled:
- Linux AppImage: ``APPIMAGE`` env var points to the outer .AppImage
the user launched. ``sys.executable`` would point inside the mounted
AppImage filesystem, which is read-only.
- Nuitka one-file: at runtime Nuitka extracts the program into a temp
directory and ``sys.executable`` may point there. The real exe is
exposed through newer ``__compiled__.onefile_argv0`` /
``NUITKA_ONEFILE_ARGV0`` builds, and in older builds ``sys.argv[0]``
is the most reliable fallback.
- PyInstaller: ``sys.executable`` already points to the real exe (the
extraction dir is in ``sys._MEIPASS`` and is separate), so the
fallback works as-is.
"""
candidates = [
os.environ.get("APPIMAGE"),
os.environ.get("NUITKA_ONEFILE_BINARY"),
_get_nuitka_compiled_attr("onefile_argv0"),
os.environ.get("NUITKA_ONEFILE_ARGV0"),
]
# Nuitka onefile needs argv[0] before sys.executable because the latter
# can be the unpacked temporary launcher.
if not hasattr(sys, "_MEIPASS"):
candidates.extend([
_ORIGINAL_ARGV[0] if _ORIGINAL_ARGV else None,
sys.argv[0] if sys.argv else None,
])
candidates.append(sys.executable)
valid: list[str] = []
for raw in candidates:
candidate = _normalise_exe_candidate(raw)
if candidate and os.path.isfile(candidate) and candidate not in valid:
valid.append(candidate)
for candidate in valid:
if not _is_temp_path(candidate):
return candidate
return valid[0] if valid else ""
def _get_source_project_root() -> str:
"""Best-effort path of the source tree when running `python main.py`."""
# The module update_manager.py lives in the project root, so its __file__
# is a very reliable anchor (more so than sys.argv[0], which may be
# relative or launched from another cwd).
try:
return os.path.dirname(os.path.abspath(__file__))
except Exception:
if _ORIGINAL_ARGV and _ORIGINAL_ARGV[0]:
return os.path.dirname(os.path.abspath(_ORIGINAL_ARGV[0]))
return os.getcwd()
def _is_flatpak() -> bool:
return bool(os.environ.get("FLATPAK_ID")) or os.path.exists("/.flatpak-info")
def _is_snap() -> bool:
return bool(os.environ.get("SNAP"))
def _is_on_desktop(path: str) -> bool:
"""Return True if `path` lives inside a folder named "Desktop".
Matches common Desktop locations on Windows (`%USERPROFILE%\\Desktop`,
`%OneDrive%\\Desktop`) and Linux (`~/Desktop`, `~/Scrivania`, etc).
We don't rely on localized folder names — we just pattern-match any
path component equal to "Desktop" (case-insensitive). That catches the
practical cases without pulling in OS-specific API.
"""
try:
parts = [p.lower() for p in os.path.normpath(path).split(os.sep)]
except Exception:
return False
return "desktop" in parts
def detect_install_type() -> tuple[str, str]:
"""Return (type, reason). `reason` is empty when auto-install is ok."""
if _is_flatpak():
return (INSTALL_MANAGED, "Flatpak install (use Flathub to update)")
if _is_snap():
return (INSTALL_MANAGED, "Snap install (use snap refresh)")
if not _is_frozen():
root = _get_source_project_root()
if not os.access(root, os.W_OK):
return (INSTALL_MANAGED, "Source folder is not writable")
return (INSTALL_SOURCE, "")
exe = _get_executable_path()
if not exe or not os.path.isfile(exe):
return (INSTALL_MANAGED, "Executable path could not be determined")
target_dir = os.path.dirname(exe) or "."
if not os.access(target_dir, os.W_OK):
return (INSTALL_MANAGED, "Install folder is not writable (system install?)")
if os.path.isfile(exe) and not os.access(exe, os.W_OK):
return (INSTALL_MANAGED, "Executable file is not writable")
return (INSTALL_FROZEN, "")
# Backwards-compatible alias. True when we can replace files in place.
def is_swap_supported() -> tuple[bool, str]:
it, reason = detect_install_type()
return (it != INSTALL_MANAGED, reason)
def is_updater_supported() -> tuple[bool, str]:
return is_swap_supported()
# ---------------------------------------------------------------------------
# Asset picker
# ---------------------------------------------------------------------------
def _pick_release_asset(release: dict, install_type: str) -> Optional[dict]:
"""Pick the right asset from a GitHub release.
- INSTALL_SOURCE -> synthetic asset pointing at zipball_url.
- INSTALL_FROZEN -> Windows: .exe > .zip ; Linux: .AppImage > .tar.gz.
- INSTALL_MANAGED -> same as frozen (so the user can still download a
binary and install it manually).
"""
if install_type == INSTALL_SOURCE:
zipball = release.get("zipball_url")
if not zipball:
return None
tag = release.get("tag_name", "latest")
safe_tag = re.sub(r"[^A-Za-z0-9._-]+", "_", str(tag))
return {
"name": f"source-{safe_tag}.zip",
"browser_download_url": zipball,
"size": 0, # GitHub does not set Content-Length on zipballs reliably
"_is_source_zipball": True,
}
assets = release.get("assets") or []
sysname = platform.system()
if sysname == "Windows":
priority = [r"\.exe$", r"win.*\.zip$", r"\.zip$"]
elif sysname == "Linux":
priority = [r"\.AppImage$", r"linux.*\.tar\.gz$", r"\.tar\.gz$", r"linux.*\.zip$"]
else:
return None
candidates = [
a for a in assets
if isinstance(a, dict)
and a.get("name")
and not a["name"].lower().endswith((".sha256", ".sig", ".asc", ".txt"))
]
for pat in priority:
rx = re.compile(pat, re.IGNORECASE)
for a in candidates:
if rx.search(a["name"]):
return a
return None
# ---------------------------------------------------------------------------
# Tag comparison
# ---------------------------------------------------------------------------
def _parse_tag(tag: str) -> list:
"""Parse a tag like 'v2.6c' into a comparable list of (number, letter)
tuples. Handles all of the variants this project has used historically:
v2.6 -> [(2, ''), (6, '')]
v2.6.0 -> [(2, ''), (6, ''), (0, '')]
v2.6c -> [(2, ''), (6, 'c')]
v2.6.b -> [(2, ''), (6, ''), (0, 'b')]
v2.7 -> [(2, ''), (7, '')]
Empty / unparseable input returns an empty list, which the caller
treats as "I don't know how to compare this".
"""
if not tag:
return []
s = tag.strip().lstrip("vV").strip()
if not s:
return []
parts = []
for component in s.split("."):
m = re.match(r"(\d*)([A-Za-z]*)", component)
if not m:
return []
num_str, letters = m.groups()
if num_str == "" and letters == "":
return []
num = int(num_str) if num_str else 0
parts.append((num, letters.lower()))
return parts
def _all_default(parts: list, start: int) -> bool:
"""Return True if every component from index `start` is (0, '')."""
return all(n == 0 and l == "" for n, l in parts[start:])
def _compare_tags(a: str, b: str) -> Optional[int]:
"""Return -1, 0, +1 like cmp(a, b), or None if either tag can't be parsed.
SaveState versioning convention (per project author):
- X.Y base release
- X.Y[letter] small hotfix on top of X.Y (b, c, d, ...)
- X.Y.Z MAJOR fix that comes AFTER all X.Y[letter]
- X.Y.Z[letter] small hotfix on top of X.Y.Z
Resulting ordering example for the 2.6 family:
2.6 < 2.6b < 2.6c < 2.6.1 < 2.6.1b < 2.6.2 < 2.7
Implementation:
- Numeric components compared as integers, position by position.
- When two components have equal numbers but DIFFERENT letter
suffixes, we look at whether the side without the letter has
deeper components after it. If yes, that side wins (".1 beats c").
If no, the side with the letter wins ("c beats nothing").
- Trailing (0, '') components are treated as absent so 2.6 == 2.6.0.
"""
pa = _parse_tag(a)
pb = _parse_tag(b)
if not pa or not pb:
return None
return _compare_at(pa, pb, 0)
def _compare_at(pa: list, pb: list, i: int) -> int:
if i >= len(pa) and i >= len(pb):
return 0
if i >= len(pa):
return 0 if _all_default(pb, i) else -1
if i >= len(pb):
return 0 if _all_default(pa, i) else 1
na, la = pa[i]
nb, lb = pb[i]
if na != nb:
return -1 if na < nb else 1
if la == lb:
return _compare_at(pa, pb, i + 1)
# Numbers equal at this position, letters differ.
if la == "":
# pa has no letter here. Does it have a meaningful deeper component?
if i + 1 < len(pa) and not _all_default(pa, i + 1):
return 1 # deeper component beats a letter hotfix
return -1 # base release < hotfix release at same level
if lb == "":
if i + 1 < len(pb) and not _all_default(pb, i + 1):
return -1
return 1
# Both have letters but they differ — compare alphabetically.
return -1 if la < lb else 1
def _asset_kind(asset: dict) -> str:
"""Classify an asset: 'source', 'exe', 'zip', 'appimage', 'tar.gz' or 'unknown'."""
if asset.get("_is_source_zipball"):
return "source"
name = (asset.get("name") or "").lower()
if name.endswith(".exe"):
return "exe"
if name.endswith(".appimage"):
return "appimage"
if name.endswith(".tar.gz") or name.endswith(".tgz"):
return "tar.gz"
if name.endswith(".zip"):
return "zip"
return "unknown"
# ---------------------------------------------------------------------------
# Extraction helpers (run in the GUI-thread BEFORE we spawn the restart
# script; keeps the helper scripts simple and lets us respect the
# Desktop-no-readme rule without involving PowerShell).
# ---------------------------------------------------------------------------
_README_PATTERNS = ("readme", "license", "licence", "copying", "notice", "changelog")
def _is_readme_like(name: str) -> bool:
base = os.path.basename(name).lower()
if not base:
return False
# Strip extension for the compare
stem = os.path.splitext(base)[0]
return stem in _README_PATTERNS or base in _README_PATTERNS
def _extract_zip_to_staging(zip_path: str, staging_dir: str,
strip_top_level: bool = False,
drop_readmes: bool = False) -> None:
"""Extract a zip file into `staging_dir`, optionally flattening the top
folder (GitHub zipballs always add one) and filtering README/LICENSE."""
if os.path.isdir(staging_dir):
shutil.rmtree(staging_dir, ignore_errors=True)
os.makedirs(staging_dir, exist_ok=True)
with zipfile.ZipFile(zip_path, "r") as zf:
names = zf.namelist()
top_prefix = ""
if strip_top_level and names:
first = names[0]
slash = first.find("/")
if slash > 0 and all(n.startswith(first[:slash + 1]) or n == first[:slash] for n in names):
top_prefix = first[:slash + 1]
for info in zf.infolist():
raw_name = info.filename
if not raw_name:
continue
rel = raw_name[len(top_prefix):] if top_prefix and raw_name.startswith(top_prefix) else raw_name
if not rel or rel.endswith("/"):
# A directory; create later as part of file writes.
continue
# Security: never honour absolute paths or parent traversal.
if rel.startswith(("/", "\\")) or ".." in rel.replace("\\", "/").split("/"):
logging.warning(f"Skipping unsafe zip entry: {raw_name!r}")
continue
if drop_readmes and _is_readme_like(rel):
logging.debug(f"Desktop rule: skipping {rel!r}")
continue
dest_path = os.path.join(staging_dir, rel)
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
with zf.open(info, "r") as src, open(dest_path, "wb") as dst:
shutil.copyfileobj(src, dst)
# ---------------------------------------------------------------------------
# Workers (QThread)
# ---------------------------------------------------------------------------
class _CheckWorker(QThread):
"""Calls GitHub Releases API once and emits the result."""
finished_ok = Signal(dict)
finished_err = Signal(str)
def __init__(self, repo: str, parent=None):
super().__init__(parent)
self._repo = repo
def run(self):
url = f"https://api.github.com/repos/{self._repo}/releases/latest"
try:
headers = {
"Accept": "application/vnd.github+json",
"User-Agent": f"SaveState-Updater/{config.APP_VERSION}",
}
resp = requests.get(url, headers=headers, timeout=15)
if resp.status_code == 404:
self.finished_err.emit("No releases published yet")
return
resp.raise_for_status()
self.finished_ok.emit(resp.json())
except requests.Timeout:
self.finished_err.emit("Request timed out")
except requests.ConnectionError:
self.finished_err.emit("No internet connection")
except Exception as e:
self.finished_err.emit(f"Check failed: {e}")
class _DownloadWorker(QThread):
"""Streams a release asset to disk, emitting progress.
Persists in memory as a member of UpdateManager so closing the dialog
does NOT cancel the download.
"""
progress = Signal(int, int)
finished_ok = Signal(str)
finished_err = Signal(str)
def __init__(self, url: str, dest_path: str, expected_size: int = 0, parent=None):
super().__init__(parent)
self._url = url
self._dest = dest_path
self._expected = expected_size
self._cancel = False
def cancel(self):
self._cancel = True
def run(self):
tmp_dest = self._dest + ".part"
try:
os.makedirs(os.path.dirname(self._dest), exist_ok=True)
# Use a permissive Accept so the same code path works for both
# release asset URLs (which serve octet-stream) and the
# api.github.com zipball/tarball endpoints (which 415 if you
# ask for octet-stream explicitly and instead want */* or no
# Accept header at all).
headers = {
"Accept": "*/*",
"User-Agent": f"SaveState-Updater/{config.APP_VERSION}",
}
with requests.get(self._url, headers=headers, stream=True, timeout=30, allow_redirects=True) as r:
r.raise_for_status()
total = int(r.headers.get("Content-Length", self._expected or 0))
done = 0
self.progress.emit(0, total)
with open(tmp_dest, "wb") as f:
for chunk in r.iter_content(chunk_size=64 * 1024):
if self._cancel:
raise RuntimeError("cancelled")
if not chunk:
continue
f.write(chunk)
done += len(chunk)
self.progress.emit(done, total)
os.replace(tmp_dest, self._dest)
self.finished_ok.emit(self._dest)
except Exception as e:
try:
if os.path.exists(tmp_dest):
os.remove(tmp_dest)
except Exception:
pass
if str(e) == "cancelled":
self.finished_err.emit("Download cancelled")
else:
self.finished_err.emit(f"Download failed: {e}")
# ---------------------------------------------------------------------------
# UpdateManager (singleton-ish, owned by MainWindow)
# ---------------------------------------------------------------------------
class UpdateManager(QObject):
"""Coordinates update check, download and apply. GUI-thread object."""
state_changed = Signal(str)
update_available = Signal(dict)
progress = Signal(int, int)
error = Signal(str)
download_ready = Signal(str)
def __init__(self, repo: Optional[str] = None, parent=None):
super().__init__(parent)
self._repo = repo or getattr(config, "GITHUB_REPO", "")
self._state: str = STATE_IDLE
self._release: Optional[dict] = None
self._asset: Optional[dict] = None
self._downloaded_path: Optional[str] = None
self._last_error: str = ""
self._bytes_done: int = 0
self._bytes_total: int = 0
self._check_worker: Optional[_CheckWorker] = None
self._dl_worker: Optional[_DownloadWorker] = None
self._install_type, self._install_reason = detect_install_type()
if self._install_type == INSTALL_MANAGED:
logging.info(
f"UpdateManager: auto-install disabled ({self._install_reason}). "
"Update check and download remain available."
)
else:
try:
if self._install_type == INSTALL_FROZEN:
target = _get_executable_path()
else:
target = _get_source_project_root()
logging.info(
f"UpdateManager: install_type={self._install_type}, "
f"target={target}"
)
except Exception:
logging.info(f"UpdateManager: install_type={self._install_type}")
# --- Public getters ---
@property
def state(self) -> str:
return self._state
@property
def release(self) -> Optional[dict]:
return self._release
@property
def asset(self) -> Optional[dict]:
return self._asset
@property
def last_error(self) -> str:
return self._last_error
@property
def progress_tuple(self) -> tuple[int, int]:
return (self._bytes_done, self._bytes_total)
@property
def downloaded_path(self) -> Optional[str]:
return self._downloaded_path
@property
def install_type(self) -> str:
return self._install_type
def is_supported(self) -> bool:
return True
def can_auto_install(self) -> bool:
return self._install_type in (INSTALL_FROZEN, INSTALL_SOURCE)
def swap_unsupported_reason(self) -> str:
return self._install_reason
# --- State helpers ---
def _set_state(self, new_state: str):
if new_state == self._state:
return
self._state = new_state
logging.debug(f"UpdateManager state -> {new_state}")
self.state_changed.emit(new_state)
# --- Check ---
def check_async(self):
if self._state in (STATE_CHECKING, STATE_DOWNLOADING):
return
if not self._repo:
self._last_error = "GitHub repo not configured"
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return
self._set_state(STATE_CHECKING)
self._check_worker = _CheckWorker(self._repo, parent=self)
self._check_worker.finished_ok.connect(self._on_check_ok)
self._check_worker.finished_err.connect(self._on_check_err)
self._check_worker.start()
@Slot(dict)
def _on_check_ok(self, release: dict):
try:
tag = str(release.get("tag_name", "")).strip()
current_tag = str(getattr(config, "APP_RELEASE_TAG", "")).strip()
if not tag:
self._last_error = "Release missing tag_name"
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return
# Decide whether the GitHub release is actually newer.
# Exact match -> up to date, fast path.
# Otherwise try a structured numeric comparison; only consider
# the release "newer" when we can prove it. If parsing fails
# for either tag, fall back to "different = update available"
# so the user is at least notified.
if tag == current_tag:
self._set_state(STATE_UP_TO_DATE)
return
cmp = _compare_tags(current_tag, tag)
if cmp is not None:
if cmp >= 0:
logging.info(
f"UpdateManager: current tag {current_tag!r} >= "
f"latest {tag!r}, treating as up to date."
)
self._set_state(STATE_UP_TO_DATE)
return
else:
logging.warning(
f"UpdateManager: could not parse tags for comparison "
f"(current={current_tag!r}, latest={tag!r}); falling "
"back to 'different means update'."
)
asset = _pick_release_asset(release, self._install_type)
if not asset:
self._last_error = f"No compatible asset in release {tag}"
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return
self._release = release
self._asset = asset
self._set_state(STATE_UPDATE_AVAILABLE)
self.update_available.emit(release)
finally:
self._check_worker = None
@Slot(str)
def _on_check_err(self, msg: str):
self._last_error = msg
self._set_state(STATE_ERROR)
self.error.emit(msg)
self._check_worker = None
# --- Download ---
def start_download(self):
if self._state in (STATE_DOWNLOADING, STATE_DOWNLOADED):
return
if self._state != STATE_UPDATE_AVAILABLE or not self._asset:
logging.warning("UpdateManager.start_download called with no asset")
return
url = self._asset.get("browser_download_url")
name = self._asset.get("name", "update.bin")
size = int(self._asset.get("size", 0) or 0)
if not url:
self._last_error = "Asset has no download URL"
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return
tmp_dir = os.path.join(tempfile.gettempdir(), "savestate_update")
dest = os.path.join(tmp_dir, name)
self._bytes_done = 0
self._bytes_total = size
self._set_state(STATE_DOWNLOADING)
self._dl_worker = _DownloadWorker(url, dest, size, parent=self)
self._dl_worker.progress.connect(self._on_dl_progress)
self._dl_worker.finished_ok.connect(self._on_dl_ok)
self._dl_worker.finished_err.connect(self._on_dl_err)
self._dl_worker.start()
def cancel_download(self):
if self._dl_worker is not None:
self._dl_worker.cancel()
@Slot(int, int)
def _on_dl_progress(self, done: int, total: int):
self._bytes_done = done
self._bytes_total = total
self.progress.emit(done, total)
@Slot(str)
def _on_dl_ok(self, path: str):
self._downloaded_path = path
self._set_state(STATE_DOWNLOADED)
self.download_ready.emit(path)
self._dl_worker = None
@Slot(str)
def _on_dl_err(self, msg: str):
self._last_error = msg
self._set_state(STATE_UPDATE_AVAILABLE)
self.error.emit(msg)
self._dl_worker = None
# --- Apply ---
def apply_and_restart(self) -> bool:
if self._state != STATE_DOWNLOADED or not self._downloaded_path:
logging.warning("apply_and_restart called in wrong state")
return False
if not self.can_auto_install():
self._last_error = (
f"Cannot auto-install on this install: {self._install_reason}. "
"Please install the downloaded file manually."
)
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return False
try:
asset = self._asset or {}
kind = _asset_kind(asset)
downloaded = self._downloaded_path
if self._install_type == INSTALL_SOURCE:
return self._apply_source(downloaded)
# Frozen install
exe_path = _get_executable_path()
logging.info(
f"Update apply: install_type=frozen, kind={kind}, "
f"target_exe={exe_path}, downloaded={downloaded}"
)
if platform.system() == "Windows":
return self._apply_frozen_windows(exe_path, downloaded, kind)
return self._apply_frozen_linux(exe_path, downloaded, kind)
except Exception as e:
logging.exception("apply_and_restart failed")
self._last_error = f"Apply failed: {e}"
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return False
# ------------------------------------------------------------------
# Source apply (runs `python main.py` updater)
# ------------------------------------------------------------------
def _apply_source(self, zip_path: str) -> bool:
project_root = _get_source_project_root()
tmp_root = os.path.join(tempfile.gettempdir(), "savestate_update")
staging = os.path.join(tmp_root, "staging_source")
logging.info(f"Source update: extracting {zip_path} -> {staging}")
_extract_zip_to_staging(zip_path, staging, strip_top_level=True, drop_readmes=False)
return self._spawn_copy_and_restart(
staging=staging,
target_dir=project_root,
restart_cmd=self._build_source_restart_cmd(),
cleanup_paths=[zip_path, staging],
)
def _build_source_restart_cmd(self) -> list[str]:
"""Return argv list used to relaunch `python main.py` after update."""
argv = [_ORIGINAL_PYTHON] + list(_ORIGINAL_ARGV)
# argv[0] may be relative ("main.py"); make it absolute against the
# project root so the helper script can cd anywhere safely.
if len(argv) >= 2:
a0 = argv[1]
if a0 and not os.path.isabs(a0):
argv[1] = os.path.join(_get_source_project_root(), a0)
return argv
# ------------------------------------------------------------------
# Frozen apply: Windows
# ------------------------------------------------------------------
def _apply_frozen_windows(self, exe_path: str, downloaded: str, kind: str) -> bool:
if kind == "exe":
return self._spawn_exe_swap_windows(exe_path, downloaded)
if kind == "zip":
install_dir = os.path.dirname(exe_path)
staging = os.path.join(os.path.dirname(downloaded), "staging_frozen")
drop_readmes = _is_on_desktop(install_dir)
logging.info(
f"Frozen zip update: extract={downloaded} -> {staging} "
f"(drop_readmes={drop_readmes}, install_dir={install_dir})"
)
_extract_zip_to_staging(downloaded, staging,
strip_top_level=False, drop_readmes=drop_readmes)
return self._spawn_copy_and_restart(
staging=staging,
target_dir=install_dir,
restart_cmd=[exe_path],
cleanup_paths=[downloaded, staging],
)
self._last_error = f"Unsupported asset type for Windows: {kind}"
self._set_state(STATE_ERROR)
self.error.emit(self._last_error)
return False
def _spawn_exe_swap_windows(self, exe_path: str, new_file: str) -> bool:
tmp_dir = os.path.dirname(new_file)
bat_path = os.path.join(tmp_dir, "savestate_update.bat")
log_path = os.path.join(tmp_dir, "savestate_update.log")
pid = os.getpid()
script = f"""@echo off
setlocal EnableDelayedExpansion
set "TARGET={exe_path}"
set "SRC={new_file}"
set "LOG={log_path}"
echo [%date% %time%] Update starting > "%LOG%"
echo TARGET=%TARGET% >> "%LOG%"
echo SRC=%SRC% >> "%LOG%"
echo Waiting for PID {pid} to exit... >> "%LOG%"
set /a waits=0
:wait_loop
tasklist /NH /FI "PID eq {pid}" 2>nul | find /I "{pid}" >nul
if not errorlevel 1 (
set /a waits+=1
if !waits! geq 60 (
echo PID {pid} did not exit within 60 seconds, giving up. >> "%LOG%"
goto fail
)
timeout /t 1 /nobreak >nul
goto wait_loop
)
rem PyInstaller bootloader can keep the .exe locked for a moment after the
rem Python process exits. Sleep a couple of seconds before swapping.
timeout /t 2 /nobreak >nul
set /a tries=0
:move_loop
move /y "%SRC%" "%TARGET%" >> "%LOG%" 2>&1
if errorlevel 1 (
set /a tries+=1
if !tries! lss 30 (
timeout /t 1 /nobreak >nul
goto move_loop
)
echo MOVE failed after %tries% retries. >> "%LOG%"
goto fail
)
echo [%date% %time%] Move OK, restarting %TARGET% >> "%LOG%"
start "" "%TARGET%"
del "%~f0"
exit /b 0
:fail
echo [%date% %time%] Update FAILED. >> "%LOG%"
echo SaveState update failed.
echo See log file: %LOG%
echo.
pause
exit /b 1
"""
with open(bat_path, "w", encoding="ascii", errors="replace") as f:
f.write(script)
logging.info(f"Update bat written to {bat_path} (log will be at {log_path})")
_spawn_detached_windows(["cmd.exe", "/c", bat_path])
self._set_state(STATE_IDLE)
return True
def _spawn_copy_and_restart(self, staging: str, target_dir: str,
restart_cmd: list[str],
cleanup_paths: list[str]) -> bool:
"""Write a platform helper script that copies `staging` onto
`target_dir` with overlay semantics, then runs `restart_cmd`.
The script waits for the current PID to exit first."""
import subprocess
tmp_dir = os.path.dirname(staging)
pid = os.getpid()
if platform.system() == "Windows":
bat_path = os.path.join(tmp_dir, "savestate_update.bat")
log_path = os.path.join(tmp_dir, "savestate_update.log")
restart_line = _windows_start_cmd(restart_cmd)
cleanup_lines = "\n".join(
f'rmdir /s /q "{p}" 2>nul\r\ndel /q "{p}" 2>nul' for p in cleanup_paths
)
# robocopy exit codes: 0..7 are success-ish, >=8 are real errors.
# /R:30 /W:1 = up to 30 retries with 1s wait (~30s window) which
# is generous enough to ride out the PyInstaller bootloader keeping
# the .exe locked for a moment after our PID exits.
script = f"""@echo off
setlocal EnableDelayedExpansion
set "STAGING={staging}"
set "TARGET={target_dir}"
set "LOG={log_path}"
echo [%date% %time%] Update starting > "%LOG%"
echo STAGING=%STAGING% >> "%LOG%"
echo TARGET=%TARGET% >> "%LOG%"
echo Waiting for PID {pid} to exit... >> "%LOG%"
set /a waits=0
:wait_loop
tasklist /NH /FI "PID eq {pid}" 2>nul | find /I "{pid}" >nul
if not errorlevel 1 (
set /a waits+=1
if !waits! geq 60 (
echo PID {pid} did not exit within 60 seconds, giving up. >> "%LOG%"
goto fail
)
timeout /t 1 /nobreak >nul
goto wait_loop
)
rem PyInstaller bootloader can keep the .exe locked for a moment after the
rem Python process exits. Sleep a couple of seconds before robocopy.
timeout /t 2 /nobreak >nul
echo [%date% %time%] Running robocopy... >> "%LOG%"
robocopy "%STAGING%" "%TARGET%" /E /R:30 /W:1 >> "%LOG%" 2>&1
set RC=!ERRORLEVEL!
echo robocopy exit code: !RC! >> "%LOG%"
rem robocopy returns 0..7 for success, >=8 for real errors. The parens in
rem the echo MUST be escaped with ^^ otherwise cmd.exe miscounts the IF
rem block's parens and makes goto fail run unconditionally.
if !RC! GEQ 8 (
echo robocopy failed ^(exit code !RC!^) >> "%LOG%"
goto fail
)
echo [%date% %time%] Cleanup... >> "%LOG%"
{cleanup_lines}
echo [%date% %time%] Restarting... >> "%LOG%"
{restart_line}
del "%~f0"
exit /b 0
:fail
echo [%date% %time%] Update FAILED. >> "%LOG%"
echo SaveState update failed.
echo See log file: %LOG%
echo.
pause
exit /b 1
"""
with open(bat_path, "w", encoding="ascii", errors="replace") as f:
f.write(script)
logging.info(f"Update bat written to {bat_path} (log will be at {log_path})")
_spawn_detached_windows(["cmd.exe", "/c", bat_path])
self._set_state(STATE_IDLE)
return True
# Linux / macOS
sh_path = os.path.join(tmp_dir, "savestate_update.sh")
restart_line = _posix_restart_cmd(restart_cmd)
cleanup_lines = "\n".join(f'rm -rf -- "{p}"' for p in cleanup_paths)
script = f"""#!/bin/sh
set -e
STAGING="{staging}"
TARGET="{target_dir}"