Repository navigation
Expand file tree
/
Copy pathModuleUpdate.py
More file actions
1659 lines (1369 loc) · 67.2 KB
/
Copy pathModuleUpdate.py
File metadata and controls
1659 lines (1369 loc) · 67.2 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
"""Worlds venv and world install management.
Owns the per-user uv-managed worlds venv, the mwgg_igdb index package, and
per-world wheel/apworld installs. Importing this module has side effects, in
this order: read-only venv probe -> skip-flag computation -> custom_worlds
scan -> venv create/repair + sys.path insert -> installer wheel_cache
(variant marker, mwgg_igdb bootstrap, cached-wheel install).
Mutable module globals (update_ran, requirements_files, worlds_files,
custom_worlds_dir, python_cmd, worlds_venv_error, the MWGG_IGDB_* trio) are
public API: callers and tests read and write them through the module namespace.
"""
import sys
import os
import subprocess
import multiprocessing
import json
import re
import shutil
import time
import datetime
import zipfile
import tarfile
import logging
import tempfile
import contextlib
import errno
import importlib.metadata
import importlib.util
import urllib.request
logger = logging.getLogger("Update")
# Fallback for entry points that call update() before their own init_logging.
if not logging.getLogger().hasHandlers() and sys.stdout:
logging.basicConfig(level=logging.INFO, format='%(message)s', stream=sys.stdout)
from pathlib import Path
from collections.abc import Iterable
from typing import Any, List, NamedTuple, Optional, TypeVar, cast, override
from importlib import invalidate_caches
from BaseUtils import local_path, user_path, mwgg_venv_site_packages, use_worlds_venv, is_frozen, tuplize_version, Version
# ── Platform & paths ─────────────────────────────────────────────────────────
def is_windows() -> bool:
return sys.platform in ("win32", "cygwin", "msys")
def is_macos() -> bool:
return sys.platform == "darwin"
def is_linux() -> bool:
return sys.platform.startswith("linux")
def install_path() -> Path:
"""Per-user worlds venv location (frozen builds)."""
if is_windows():
return Path.home() / "AppData" / "Local" / "MultiworldGG" / "mwgg_venv"
elif is_macos():
return Path.home() / "Library" / "Application Support" / "MultiworldGG" / "mwgg_venv"
elif is_linux():
return Path.home() / ".local" / "share" / "MultiworldGG" / "mwgg_venv"
else:
raise RuntimeError("Unsupported platform")
if (is_windows() or is_macos()) and sys.version_info < (3, 13, 0):
raise RuntimeError(f"Incompatible Python Version found: {sys.version_info}. Official 3.13.+ is supported.")
elif sys.version_info < (3, 13, 0):
raise RuntimeError(f"Incompatible Python Version found: {sys.version_info}. 3.13.+ is supported.")
# ── Install gating ───────────────────────────────────────────────────────────
def _worlds_venv_is_readonly() -> bool:
"""True when the worlds venv lives on a read-only mount (e.g. a Docker `:ro`
bind mount). Such consumers must never attempt installs; the mwgg_upgrader
service is the sole writer of the venv."""
if not use_worlds_venv():
return False
venv_dir = install_path()
try:
venv_dir.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(dir=venv_dir):
pass
except OSError as e:
logger.warning(f"Worlds venv at {venv_dir} not writable ({e!r}); "
"treating as read-only (installs disabled).")
return True
return False
# Detected once at import: a read-only worlds venv disables every install path
# below (like SKIP_ALL_INSTALLS), so consumers can't crash writing the upgrader-owned venv.
_VENV_READONLY = _worlds_venv_is_readonly()
if _VENV_READONLY:
logger.info("Worlds venv is read-only; installs disabled (mwgg_upgrader owns writes).")
# uv's error when _bootstrap_worlds_venv() could not create the venv; disables installs
# like _VENV_READONLY, and the launcher shows it to the user.
worlds_venv_error: Optional[str] = None
def _skip_all_installs() -> bool:
"""Installs are off via explicit env opt-out, a read-only worlds venv, or a
worlds venv that could not be created."""
return bool(os.environ.get("SKIP_ALL_INSTALLS")) or _VENV_READONLY or worlds_venv_error is not None
# True in spawned children (any multiprocessing child not named "MultiworldGG",
# splash included) and under the env opt-outs. Gates the custom_worlds scan and
# the dev pipeline in _update_locked; update_worlds() ignores it on purpose so
# the splash child can run the world update.
_skip_update = bool(
(multiprocessing.parent_process() and multiprocessing.current_process().name != "MultiworldGG")
or os.environ.get("SKIP_REQUIREMENTS_UPDATE", "")
or _skip_all_installs()
)
update_ran = _skip_update
_T = TypeVar("_T")
# ── Mutable public state ─────────────────────────────────────────────────────
class RequirementsSet(set[_T]):
"""Set that re-arms the updater (update_ran) whenever a file is added."""
@override
def add(self, e: _T) -> None:
global update_ran
update_ran &= _skip_update
super().add(e)
@override
def update(self, *s: Iterable[_T]) -> None:
global update_ran
update_ran &= _skip_update
super().update(*s)
# Core requirements.txt doubles as a constraint file for every requirements
# install below, so additional files can't upgrade core deps past its pins.
core_constraints: Path = Path(local_path("requirements.txt"))
requirements_files: RequirementsSet[Path] = RequirementsSet({core_constraints})
worlds_files: dict[str, RequirementsSet[str]] = {"wheels": RequirementsSet(), "apworlds": RequirementsSet()}
# Single source of truth for the launch scan, install and Generate. user_path is the
# install dir when writable, else the user data dir (AppImage mount, macOS .app).
def _resolve_custom_worlds_dir() -> Path:
return Path(user_path("custom_worlds"))
custom_worlds_dir = _resolve_custom_worlds_dir()
try:
custom_worlds_dir.mkdir(parents=True, exist_ok=True)
except OSError:
pass # best-effort; read-only filesystems tolerated
def _scan_custom_worlds() -> None:
"""Register .whl/.apworld files in custom_worlds_dir into worlds_files.
Skipped once a full update has run. Tests monkeypatch the module globals
and call this directly.
"""
if update_ran or not custom_worlds_dir.exists():
return
for world_file in custom_worlds_dir.glob("*.whl"):
worlds_files["wheels"].add(str(world_file))
for world_file in custom_worlds_dir.glob("*.apworld"):
worlds_files["apworlds"].add(str(world_file))
_scan_custom_worlds()
# ── uv runner ────────────────────────────────────────────────────────────────
# Default for dev mode (not frozen): use the running interpreter and let uv install into its venv.
python_cmd = sys.executable
_uv_resolved_path: Optional[Path] = None
_uv_unavailable: bool = False
def _uv_candidate_paths() -> list[Path]:
"""uv lookup order; fixed list, no filesystem hunting."""
candidates: list[Path] = []
# Frozen builds on Linux/macOS ship uv next to the executable
if is_frozen():
exe_dir = Path(sys.executable).parent
if is_macos():
import platform
arch = platform.machine() # "arm64" on Apple Silicon, "x86_64" on Intel
candidates.append(exe_dir / f"uv-{arch}")
elif not is_windows():
candidates.append(exe_dir / "uv")
candidates.append(Path("uv")) # PATH lookup
if is_windows():
candidates += [
Path.home() / "AppData" / "Local" / "Microsoft" / "WinGet" / "Links" / "uv.exe", # winget shim
Path.home() / ".local" / "bin" / "uv.exe", # astral PS installer
]
try:
# The Links shim is an AppExecLink some tokens can't stat/exec (WinError 448);
# the actual winget-installed PE is a plain file that works in any token.
packages_dir = Path.home() / "AppData" / "Local" / "Microsoft" / "WinGet" / "Packages"
candidates += sorted(packages_dir.glob("astral-sh.uv_*/**/uv.exe"))
except OSError:
pass
else:
candidates += [
Path.home() / ".local" / "bin" / "uv", # astral installer / pipx
Path("/opt/homebrew/bin/uv"), # Homebrew (Apple Silicon)
Path("/usr/local/bin/uv"), # Homebrew (Intel) / generic
]
return candidates
def _uv_pip(*args: str) -> list[str]:
return ["pip", *args, "--python", str(python_cmd)]
def _uv_run(args: list[str], timeout: float = 120, check: bool = False) -> subprocess.CompletedProcess[str]:
"""Run `uv <args>` against the first reachable uv binary."""
global _uv_resolved_path, _uv_unavailable
# Windows-only: new process group + no console window so uv can't flash a
# window or steal the parent's Ctrl-C; 0 is the no-op default elsewhere.
creationflags = (
subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NO_WINDOW
if is_windows() else 0
)
if _uv_unavailable:
return subprocess.CompletedProcess(args, 127, "", "uv not found at any known path")
candidates = [_uv_resolved_path] if _uv_resolved_path else _uv_candidate_paths()
for cand in candidates:
cmd = [cand] + args
logger.info(f"Executing subprocess command: {[str(c) for c in cmd]}")
try:
result = subprocess.run(
cmd,
check=check,
capture_output=True,
# uv emits UTF-8 (box-drawing error art); the locale default
# (cp1252 on Windows) mis-decodes it into undecodable bytes
# for anything re-reading our log stream.
encoding="utf-8",
errors="replace",
stdin=subprocess.DEVNULL,
timeout=timeout,
creationflags=creationflags,
)
except OSError as e:
# Candidate unusable; try next.
logger.debug(f"uv not usable at {cand} ({e!r}); trying next candidate")
continue
if _uv_resolved_path is None:
_uv_resolved_path = cand
logger.debug(f"Using uv at {cand}")
return result
_uv_unavailable = True
if is_windows():
install_hint = (
"install uv via `winget install astral-sh.uv`, "
"`irm https://astral.sh/uv/install.ps1 | iex`, or `choco install uv`"
)
else:
install_hint = "install uv via `curl -LsSf https://astral.sh/uv/install.sh | sh`"
logger.warning(
"uv not found at any known install path. Worlds cannot be pre-installed; "
f"they will be installed on demand when needed. To pre-install, {install_hint}."
)
return subprocess.CompletedProcess(args, 127, "", "uv not found at any known path")
# ── Worlds-venv bootstrap ────────────────────────────────────────────────────
def venv_is_healthy(venv_path: Path) -> bool:
"""True if the venv's interpreter actually runs.
No pre-probes: stat()-ing the venv's `home =` (a uv-managed python) raises
OSError on untraversable mounts (WinError 448); just run the interpreter and
treat any failure as unhealthy so the caller recreates.
"""
venv_python = venv_path / ("Scripts" if is_windows() else "bin") / ("python.exe" if is_windows() else "python")
try:
return subprocess.run([str(venv_python), "--version"], capture_output=True, timeout=10).returncode == 0
except (subprocess.TimeoutExpired, OSError):
return False
_venv_just_created = False
def _bootstrap_worlds_venv() -> None:
"""Route worlds + mwgg_igdb into a dedicated venv under user data, creating or
repairing it via uv. Tests monkeypatch the module globals and call this directly."""
global python_cmd, _venv_just_created, worlds_venv_error
if not use_worlds_venv():
return
if is_frozen():
exe_dir = Path(sys.exec_prefix)
default_libs_dir = Path(exe_dir, "lib")
if str(default_libs_dir) not in sys.path:
sys.path.append(str(default_libs_dir))
venv_path = install_path()
# Never sys.executable: frozen, that is the launcher exe, which uv cannot query.
python_cmd = venv_path / ("Scripts" if is_windows() else "bin") / ("python.exe" if is_windows() else "python")
venv_ready = True
if not venv_is_healthy(venv_path):
# Any failure must degrade to "installs disabled", never crash
# the import: this runs at module load for every consumer.
try:
venv_path.mkdir(parents=True, exist_ok=True)
if any(venv_path.iterdir()):
logger.info(f"Repairing stale venv at {venv_path} via uv (site-packages preserved).")
else:
logger.info(f"Creating venv at {venv_path} via uv.")
# uv reuses an existing system Python 3.13 if one is present; otherwise it
# downloads python-build-standalone.
venv_result = _uv_run(
["venv", str(venv_path), "--allow-existing", "--python", "3.13"],
timeout=600,
)
venv_ready = venv_result.returncode == 0
_venv_just_created = venv_ready
if not venv_ready:
worlds_venv_error = (venv_result.stderr.strip()
or f"uv venv exited with code {venv_result.returncode}")
except Exception as e:
worlds_venv_error = repr(e)
venv_ready = False
if not venv_ready:
logger.warning(f"Could not create the worlds venv at {venv_path}; "
f"world installs are disabled until it is repaired:\n{worlds_venv_error}")
if venv_ready:
# Make worlds-venv packages (mwgg_igdb etc.) importable from this process.
site_packages = mwgg_venv_site_packages()
if site_packages not in sys.path:
sys.path.insert(0, site_packages)
_bootstrap_worlds_venv()
# ── mwgg_igdb index & variants ───────────────────────────────────────────────
# mwgg_igdb package source: orphan branch on the Index repo, fetched as GitHub's source
# tarball -- a `git+` URL makes uv shell out to a git executable that end users lack.
# See MultiworldGG-Index/scripts/build_variants.py for variant definitions.
DEFAULT_MWGG_IGDB_VARIANT = "sixteen" # ultimate fallback when nothing is installed
_VARIANTS = ("nr", "ao", "twelve", "sixteen")
_EXPLICIT_VARIANT: Optional[str] = None # set via set_variant(); wins over detection
MWGG_INDEX_REPO = "MultiworldGG/MultiworldGG-Index"
def _index_archive_url(ref: str) -> str:
"""Source tarball URL for `ref` (`heads/<branch>` or `tags/<tag>`) on the Index repo."""
return f"https://github.com/{MWGG_INDEX_REPO}/archive/refs/{ref}.tar.gz"
# The three globals below mirror the currently *resolved* variant. _resolve_variant()
# keeps them consistent; callers should treat them as read-only.
MWGG_IGDB_VARIANT = DEFAULT_MWGG_IGDB_VARIANT
MWGG_IGDB_BRANCH = f"game_index_{MWGG_IGDB_VARIANT}"
MWGG_IGDB_URL = _index_archive_url(f"heads/{MWGG_IGDB_BRANCH}")
def _detect_installed_variant() -> Optional[str]:
"""Return the variant currently installed locally, or None if undetectable.
Reads the `__variant__` constant the Index build bakes into the generated
`mwgg_igdb` module. Absent (pre-`__variant__` build) or unimportable → None,
and callers fall back to DEFAULT_MWGG_IGDB_VARIANT.
"""
if importlib.util.find_spec("mwgg_igdb") is None:
return None
try:
import mwgg_igdb
except ImportError:
return None
variant = getattr(mwgg_igdb, "__variant__", None)
if isinstance(variant, str) and variant in _VARIANTS:
return variant
return None
def _resolve_variant() -> str:
"""Pick the variant to act on, refresh the derived globals, and return it.
Precedence: explicit `set_variant()` > detected install > default fallback.
"""
global MWGG_IGDB_VARIANT, MWGG_IGDB_BRANCH, MWGG_IGDB_URL
if _EXPLICIT_VARIANT is not None:
variant = _EXPLICIT_VARIANT
else:
variant = _detect_installed_variant() or DEFAULT_MWGG_IGDB_VARIANT
# Public globals, intentionally reassigned (callers and tests read them).
MWGG_IGDB_VARIANT = variant # pyright: ignore[reportConstantRedefinition]
MWGG_IGDB_BRANCH = f"game_index_{variant}" # pyright: ignore[reportConstantRedefinition]
MWGG_IGDB_URL = _index_archive_url(f"heads/{MWGG_IGDB_BRANCH}") # pyright: ignore[reportConstantRedefinition]
return variant
def set_variant(variant: str) -> None:
"""Switch the runtime mwgg_igdb variant; takes effect on next install_mwgg_igdb call."""
global _EXPLICIT_VARIANT
_EXPLICIT_VARIANT = variant # pyright: ignore[reportConstantRedefinition] # override sentinel
_resolve_variant()
def _parse_variant_token(token: str) -> Optional[str]:
"""Return the variant name if `token` is `mwgg_igdb` or `mwgg_igdb_<variant>`, else None.
Bare `mwgg_igdb` maps to the canonical default `sixteen`. Inno Setup passes
one of these tokens in the `--worlds` list to select the parental-rating gate.
"""
if token == "mwgg_igdb":
return "sixteen"
prefix = "mwgg_igdb_"
if token.startswith(prefix):
variant = token[len(prefix):]
if variant in _VARIANTS:
return variant
return None
def _igdb_install_date() -> Optional[datetime.date]:
"""Local date `mwgg_igdb` was last written to disk, or None if not installed.
The module file's mtime is the package's own install datestamp (set fresh
every time uv (re)installs it), so no separate stamp file is needed.
"""
spec = importlib.util.find_spec("mwgg_igdb")
if spec is None or not spec.origin or not os.path.exists(spec.origin):
return None
return datetime.date.fromtimestamp(os.path.getmtime(spec.origin))
def _igdb_upgraded_recently() -> bool:
"""True when an upgrade pull would be a no-op: mwgg_igdb was installed today."""
# force=True callers (variant switches, CLI) bypass this throttle entirely.
install_date = _igdb_install_date()
return install_date is not None and install_date == datetime.date.today()
def installed_mwgg_index_tag() -> Optional[str]:
"""Return the Index release tag the installed mwgg_igdb corresponds to, or None.
Stamped into a generated seed so a client can reconstruct the exact world set
from that index snapshot (the Index branches are force-pushed, but every release
is preserved as a `<variant>-<date>` tag). Prefers a `__tag__` baked by the Index
build; otherwise derives `<variant>-<zero-padded CalVer>` from the package version
(e.g. variant 'sixteen' + version '2026.6.10' -> 'sixteen-2026.06.10').
"""
try:
import mwgg_igdb
except ImportError:
return None
baked = getattr(mwgg_igdb, "__tag__", None)
if isinstance(baked, str) and baked:
return baked
variant = _detect_installed_variant() or DEFAULT_MWGG_IGDB_VARIANT
try:
version = importlib.metadata.version("mwgg_igdb")
except importlib.metadata.PackageNotFoundError:
return None
parts = version.split(".")
if len(parts) >= 3 and all(p.isdigit() for p in parts[:3]):
year, month, day, *rest = parts
date = ".".join([year, f"{int(month):02d}", f"{int(day):02d}", *rest])
return f"{variant}-{date}"
return f"{variant}-{version}"
def install_mwgg_igdb(upgrade: bool = False, force: bool = False) -> bool:
"""Install or refresh the mwgg_igdb package from the Index repo orphan branch.
Called before any code path that imports `mwgg_igdb`: the package is the
runtime source-of-truth for which worlds exist and where to fetch them.
Args:
upgrade: Run pip with --upgrade.
force: With upgrade=True, bypass the once-daily throttle.
Returns True if the install succeeded (or was throttled).
"""
# Racing installs from two processes are benign: uv writes temp-then-rename.
if _skip_all_installs():
return True
_resolve_variant()
if upgrade and not force and _igdb_upgraded_recently():
logger.debug("mwgg_igdb already installed today; skipping upgrade pull")
return True
args = _uv_pip("install", MWGG_IGDB_URL, "--no-cache")
if upgrade:
# --reinstall rewrites the package even when the branch HEAD is unchanged,
# advancing its mtime so the once-daily throttle stays satisfied until tomorrow.
args.append("--reinstall")
logger.info(f"Installing mwgg_igdb ({MWGG_IGDB_VARIANT}) from {MWGG_IGDB_BRANCH}")
try:
result = _uv_run(args, timeout=300)
except subprocess.TimeoutExpired:
logger.warning("uv install of mwgg_igdb timed out.")
return False
if result.returncode != 0:
logger.warning(f"Failed to install mwgg_igdb: {result.stderr}")
return False
return True
def _bootstrap_fresh_venv_mwgg_igdb() -> None:
"""The venv creator installs mwgg_igdb synchronously: a sys.path entry probed
before it exists is None-cached in sys.path_importer_cache and never rechecked."""
if _venv_just_created:
install_mwgg_igdb()
invalidate_caches()
def _get_game_index():
"""Lazy-import GameIndex; install mwgg_igdb if missing. Returns None on failure."""
try:
from mwgg_igdb import GameIndex
return GameIndex
except ImportError:
if install_mwgg_igdb():
invalidate_caches()
try:
from mwgg_igdb import GameIndex
return GameIndex
except ImportError as e:
logger.warning(f"mwgg_igdb still unimportable after install: {e}")
return None
# ── Installer wheel_cache (Inno) ─────────────────────────────────────────────
# Inno's native [Files] download step stages selected worlds' wheels here, next to
# the exe, before first launch (replaces the broken de-elevated runasoriginaluser exec).
def _wheel_cache_dir() -> Path:
return Path(sys.executable).parent / "wheel_cache"
def _wheel_cache_variant_token(cache_dir: Optional[Path] = None) -> Optional[str]:
"""Read wheel_cache's variant marker; pass a dir explicitly to read a claimed copy."""
marker = (cache_dir or _wheel_cache_dir()) / "mwgg_igdb_variant.txt"
try:
token = marker.read_text(encoding="utf-8").strip()
except OSError:
return None
return token if token in _VARIANTS else None
def _apply_wheel_cache_variant() -> None:
"""Apply the installer's chosen variant; must run before
_bootstrap_fresh_venv_mwgg_igdb() or the first mwgg_igdb install misses it."""
if not is_frozen() or _skip_all_installs():
return
token = _wheel_cache_variant_token()
if token is not None:
set_variant(token)
def _install_wheel_cache_wheels(wheel_paths: list[str]) -> None:
"""Install every cached wheel in one `uv pip install`, deps resolved normally."""
args = _uv_pip("install", *wheel_paths)
try:
result = _uv_run(args, timeout=300)
except subprocess.TimeoutExpired:
result = None
if result is None or result.returncode != 0:
stderr = (result.stderr if result else "timed out").strip()
for wheel in wheel_paths:
logger.warning(f"Failed to install cached wheel {wheel}: {stderr}")
return
for wheel in wheel_paths:
logger.info(f"Installed cached wheel {wheel}")
invalidate_caches()
# Handed to the GUI launcher, which seeds its favorites bar from it; env rather
# than a module global so an update restart (exit_restart_for_update) still
# delivers it to the launcher process that finally builds the UI.
INSTALLER_WORLDS_ENV = "MWGG_INSTALLER_WORLDS"
def _wheel_cache_slugs(wheel_paths: Iterable[str]) -> list[str]:
"""World slugs from staged wheel filenames (worlds_<slug>-<version>-...whl)."""
slugs: list[str] = []
for wheel in wheel_paths:
dist = Path(wheel).name.split("-", 1)[0]
slug = dist.removeprefix("worlds_").removeprefix("worlds.")
if slug and slug not in slugs:
slugs.append(slug)
return slugs
def _consume_wheel_cache() -> None:
"""Claim wheel_cache/ via atomic rename (loser gets OSError, skips) and install
it into the worlds venv; best effort, failures degrade to on-demand install."""
if not is_frozen() or _skip_all_installs():
return
cache_dir = _wheel_cache_dir()
consuming_dir = cache_dir.with_name(cache_dir.name + ".consuming")
# A stale claim from a crashed prior run must not wedge every future launch.
if consuming_dir.exists():
shutil.rmtree(consuming_dir, ignore_errors=True)
try:
os.rename(cache_dir, consuming_dir)
except OSError:
return # absent, or another process just claimed it
try:
token = _wheel_cache_variant_token(consuming_dir)
if token is not None:
set_variant(token)
wheels = sorted(str(p) for p in consuming_dir.glob("*.whl"))
if wheels:
_install_wheel_cache_wheels(wheels)
os.environ[INSTALLER_WORLDS_ENV] = ",".join(_wheel_cache_slugs(wheels))
except Exception as e:
# Never crash module import; worlds are installed on demand instead.
logger.warning(f"wheel_cache processing failed: {e!r}")
finally:
shutil.rmtree(consuming_dir, ignore_errors=True)
# Marker peeked before the first mwgg_igdb install; claim/install runs after.
_apply_wheel_cache_variant()
_bootstrap_fresh_venv_mwgg_igdb()
_consume_wheel_cache()
# ── World install primitives ─────────────────────────────────────────────────
def _world_slug(world: str) -> str:
return world.removeprefix("worlds.")
def _module_location_tag(url: str) -> Optional[str]:
"""Extract the version from a release-asset wheel URL.
Expects ``https://.../<dist>-<ver>-py3-none-any.whl``, optionally with a
``#sha256=<hex>`` fragment. Returns None for anything that isn't a
recognizable wheel URL; the caller then skips the comparison.
"""
if not url:
return None
name = url.rsplit("/", 1)[-1]
name = name.split("#", 1)[0].split("?", 1)[0]
if not name.endswith(".whl"):
return None
parts = name[:-len(".whl")].split("-")
# PEP 427: dist, version, [build,] python, abi, platform; version is index 1.
if len(parts) < 5:
return None
return parts[1]
def _venv_worlds_dir() -> Path:
"""Return the venv worlds dir from which worlds/__init__.py extends __path__.
Apworlds get extracted here so they're importable via the normal file loader
(multiprocessing.spawn in child processes needs disk-based modules; zipimport-
only modules can't be re-imported in the spawned child).
"""
if use_worlds_venv():
return Path(mwgg_venv_site_packages("worlds"))
# Dev: matches the hardcoded path in src/worlds/__init__.py
from sysconfig import get_path
return Path(get_path("purelib")) / "worlds"
# Consumed by the upgrader tools (tools/mwgg_upgrade.py, tools/mcp_mwgg_upgrader.py),
# so it is unused within this module, hence the targeted ignore.
def _venv_has_worlds() -> bool: # pyright: ignore[reportUnusedFunction]
try:
worlds_dir = _venv_worlds_dir()
return worlds_dir.exists() and any(worlds_dir.iterdir())
except OSError:
return False
def apworld_module_name(zf: zipfile.ZipFile) -> str:
"""An apworld's module is its single top-level directory (LauncherComponents._install_apworld's
rule), else the file stem."""
directories = [f.name for f in zipfile.Path(zf).iterdir() if f.is_dir()]
return directories[0] if len(directories) == 1 else Path(cast(str, zf.filename)).stem
def find_custom_apworld(slug: str) -> Path:
"""custom_worlds/<slug>.apworld, else a hand-copied apworld (e.g. a versioned filename) whose
module is slug. Returns the canonical path when neither exists."""
apworld_file = custom_worlds_dir / f"{slug}.apworld"
if apworld_file.exists():
return apworld_file
for candidate in custom_worlds_dir.glob("*.apworld"):
try:
with zipfile.ZipFile(candidate) as zf:
if apworld_module_name(zf) == slug:
return candidate
except (OSError, zipfile.BadZipFile):
continue
return apworld_file
# Written into an extracted apworld: identifies the file it was extracted from.
_CUSTOM_SOURCE_MARKER = ".mwgg-custom-source"
def _custom_source_id(apworld_file: Path) -> str:
source = apworld_file.stat()
return f"{source.st_size}:{source.st_mtime_ns}"
def _install_apworld_to_venv(apworld_file: Path, slug: str) -> bool:
"""Extract the `<slug>/` directory from apworld_file into the venv worlds dir.
Returns True on success. Overwrites existing files in place rather than
rmtree'ing (rmtree fails on Windows if the module is currently loaded).
"""
venv_worlds = _venv_worlds_dir()
try:
venv_worlds.mkdir(parents=True, exist_ok=True)
prefix = f"{slug}/"
with zipfile.ZipFile(apworld_file, "r") as zf:
members = [m for m in zf.namelist() if m == prefix or m.startswith(prefix)]
if not members:
logger.warning(f"Apworld {apworld_file} contains no '{slug}/' directory")
return False
for member in members:
zf.extract(member, str(venv_worlds))
# Refresh the dir's mtime so the stale-extraction pruner sees this
# extraction as "last used" even if zipfile restored archive timestamps.
target_dir = venv_worlds / slug
try:
os.utime(target_dir, None)
except OSError:
pass
(target_dir / _CUSTOM_SOURCE_MARKER).write_text(_custom_source_id(apworld_file))
logger.info(f"Extracted apworld {apworld_file} to {target_dir}")
# Extend an already-imported worlds package's __path__ so the new module is
# discoverable without a restart (worlds/__init__.py handles the startup case).
worlds_pkg = sys.modules.get("worlds")
if worlds_pkg is not None and hasattr(worlds_pkg, "__path__"):
venv_str = str(venv_worlds)
if venv_str not in worlds_pkg.__path__:
worlds_pkg.__path__.append(venv_str)
return True
except Exception as e:
logger.error(f"Failed to extract apworld {apworld_file} to {venv_worlds}: {e}")
return False
def _prune_stale_apworld_extractions(max_age_days: int = 30) -> None:
"""Remove extracted-apworld dirs in the venv worlds dir whose mtime is older
than max_age_days. Skips any dir backed by a real importlib.metadata
Distribution (pip-installed wheels), so this only ever touches our own
extraction output.
"""
venv_worlds = _venv_worlds_dir()
if not venv_worlds.exists():
return
cutoff = time.time() - max_age_days * 86400
for entry in venv_worlds.iterdir():
if not entry.is_dir() or entry.name.startswith("_"):
continue
try:
importlib.metadata.distribution(f"worlds.{entry.name}")
continue # pip-installed; leave alone
except importlib.metadata.PackageNotFoundError:
pass
try:
if entry.stat().st_mtime >= cutoff:
continue
except OSError:
continue
try:
shutil.rmtree(entry)
logger.info(f"Pruned stale extracted apworld {entry} (mtime > {max_age_days}d)")
except OSError as e:
logger.warning(f"Could not prune stale apworld {entry}: {e}")
def uninstall_worlds(worlds: List[str]) -> None:
"""Uninstall a list of `worlds.<slug>` packages from the venv."""
for world in worlds:
try:
_uv_run(_uv_pip("uninstall", world), timeout=60)
except subprocess.TimeoutExpired:
logger.warning(f"uv uninstall of {world} timed out.")
def find_world_modules() -> set[str]:
"""Return all known world slugs: union of mwgg_igdb entries and currently installed `worlds.<slug>` dists."""
world_modules_set: set[str] = set()
index = _get_game_index()
if index is not None:
world_modules_set.update(index.get_all_games().keys())
try:
executable_args = _uv_pip("list", "--format", "json")
response = _uv_run(executable_args, timeout=45)
if response.returncode == 0:
for package in json.loads(response.stdout):
package_name = package.get("name", "")
if package_name.startswith("worlds") and len(package_name) > 7:
# uv hyphenates dist names (worlds.dark_souls_3 -> worlds-dark-souls-3); restore the slug.
world_name = package_name[7:].replace("-", "_")
if not world_name.startswith("_"):
world_modules_set.add(world_name)
else:
logger.warning(f"Could not list installed packages: {response.stderr}")
except (subprocess.TimeoutExpired, json.JSONDecodeError, FileNotFoundError) as e:
logger.warning(f"Could not check installed world modules: {e}")
except Exception as e:
logger.warning(f"Unexpected error while checking installed world modules: {e}")
return world_modules_set
def _format_manual_install_hint(module_location: str) -> str:
"""Render a copy-pasteable `uv pip install` command for the host shell."""
return (
"To install manually on the host (where build tools are available), run:\n"
f" bash: source {mwgg_venv_site_packages()}/bin/activate\n"
f" win PS: & {mwgg_venv_site_packages()}/Scripts/Activate.ps1\n"
f" docker: source ~/<your-local-venv>/bin/activate\n"
f" then run:\n"
f" uv pip install '{module_location}' --upgrade --no-cache\n"
f" docker admins will need to copy the site packages to /var/lib/mwgg/mwgg_venv"
)
# ── Dependency healing ───────────────────────────────────────────────────────
def _canonical_name(name: str) -> str:
"""PEP 503 name normalization."""
return re.sub(r"[-_.]+", "-", name).lower()
def _installed_dist_names() -> set[str]:
"""Canonical names of every dist visible to this process (one venv pass)."""
names: set[str] = set()
for dist in importlib.metadata.distributions():
try:
name = dist.metadata["Name"]
except Exception:
continue
if name: # email.Message returns None for a missing key despite the str stub
names.add(_canonical_name(name))
return names
def _missing_requirements(requires: Optional[list[str]],
installed_names: Optional[set[str]] = None) -> list[str]:
"""Names a dist's declared requirements need that are absent from the venv.
Presence-only: version specifiers are ignored (uv reconciles versions on
reinstall). Extras-gated and false-marker requirements don't apply;
unparseable requirement strings are skipped (unhealable, never loop on them).
"""
if not requires:
return []
try:
from packaging.requirements import InvalidRequirement, Requirement
except ImportError:
# Stale env without `packaging` yet; healing resumes once requirements install.
return []
if installed_names is None:
installed_names = _installed_dist_names()
missing: list[str] = []
for raw in requires:
try:
req = Requirement(raw)
except InvalidRequirement:
continue
if req.marker is not None and not req.marker.evaluate({"extra": ""}):
continue
if _canonical_name(req.name) not in installed_names:
missing.append(req.name)
return missing
def _world_dist(slug: str) -> Optional[importlib.metadata.Distribution]:
try:
return importlib.metadata.distribution(f"worlds.{slug}")
except importlib.metadata.PackageNotFoundError:
return None
def _state_dir() -> Path:
"""Shared writable dir for cross-process state (install lock, heal markers)."""
state_dir = install_path().parent if use_worlds_venv() else Path(tempfile.gettempdir())
state_dir.mkdir(parents=True, exist_ok=True)
return state_dir
def _heal_state_path() -> Path:
return _state_dir() / ".mwgg-heal-attempts.json"
def _load_heal_attempts() -> dict[str, str]:
"""slug -> module_location of that world's last failed install.
Suppresses re-healing a wheel that already failed; a new release URL in the
index mismatches the stored value and re-arms exactly one retry. The store
is an optimization, never authority: any read failure degrades to {}.
"""
try:
data = json.loads(_heal_state_path().read_text(encoding="utf-8"))
except (OSError, ValueError):
return {}
if not isinstance(data, dict):
return {}
attempts: dict[Any, Any] = data
return {key: value for key, value in attempts.items()
if isinstance(key, str) and isinstance(value, str)}
def _save_heal_attempts(attempts: dict[str, str]) -> None:
try:
_heal_state_path().write_text(json.dumps(attempts), encoding="utf-8")
except OSError:
pass # worst case: one extra heal attempt next cold start
def _record_heal_attempt(slug: str, module_location: str) -> None:
attempts = _load_heal_attempts()
if attempts.get(slug) != module_location:
attempts[slug] = module_location
_save_heal_attempts(attempts)
def _clear_heal_attempt(slug: str) -> None:
attempts = _load_heal_attempts()
if slug in attempts:
del attempts[slug]
_save_heal_attempts(attempts)
# ── Install decisions ────────────────────────────────────────────────────────
def _world_requires_install(slug: str, games: dict[str, dict[str, object]],
installed_names: Optional[set[str]] = None,
heal_attempts: Optional[dict[str, str]] = None) -> bool:
"""True when the world's wheel should be (re)installed: dist missing,
version behind the index tag, or installed with missing dependencies.
The dependency branch is the healing path: the caller reinstalls WITH deps
from the current module_location. A wheel that already failed (recorded
heal attempt for this exact URL) is left alone until the index serves a
new URL, bounding retries to one per (slug, wheel URL).
"""
dist = _world_dist(slug)
if dist is None:
return True
module_location = games.get(slug, {}).get("module_location")
if not isinstance(module_location, str):
return False
tag = _module_location_tag(module_location)
if tag and dist.version != tag:
return True
missing = _missing_requirements(dist.requires, installed_names)
if not missing:
return False
if heal_attempts is None:
heal_attempts = _load_heal_attempts()
if heal_attempts.get(slug) == module_location:
return False
logger.info(f"World {slug} is missing dependencies {missing}; reinstalling with them")
return True
def _worlds_requiring_install(worlds: list[str], games: dict[str, dict[str, object]],
installed_names: Optional[set[str]] = None,
heal_attempts: Optional[dict[str, str]] = None) -> list[str]:
return [world for world in worlds
if _world_requires_install(_world_slug(world), games, installed_names, heal_attempts)]