forked from MultiworldGG/MultiworldGG
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleUpdate.py
More file actions
882 lines (726 loc) · 36.3 KB
/
ModuleUpdate.py
File metadata and controls
882 lines (726 loc) · 36.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
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
import sys
import os
import subprocess
import multiprocessing
import warnings
import json
import urllib.request
import shutil
import zipfile
import re
import shutil
import logging
import time
import tempfile
logger = logging.getLogger("Update")
if not logging.getLogger().hasHandlers():
logging.basicConfig(level=logging.DEBUG, format='%(message)s', stream=sys.stdout)
from pathlib import Path
from typing import List, Optional
from importlib import invalidate_caches
from BaseUtils import tuplize_version, Version
from APContainer import APWorldContainer
def is_frozen() -> bool:
return getattr(sys, 'frozen', False)
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:
# Returns the path to the install directory for the python modules
# Frozen builds only
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")
# Version compatibility checks
if (is_windows() or is_macos()) and sys.version_info < (3, 12, 0):
raise RuntimeError(f"Incompatible Python Version found: {sys.version_info}. Official 3.12.+ is supported.")
elif (is_windows() or is_macos()) and sys.version_info < (3, 12, 7):
warnings.warn(f"Python Version {sys.version_info} has security issues. Don't use in production.")
elif sys.version_info < (3, 12, 0):
raise RuntimeError(f"Incompatible Python Version found: {sys.version_info}. 3.12.+ is supported.")
# Skip update if running in splash screen process
# Allow updates in main process and main client process
_skip_update = bool(
multiprocessing.parent_process() and multiprocessing.current_process().name != "MultiWorldGG"
)
local_dir = Path(__file__).parent
update_ran = _skip_update
need_update: List[str] = []
class RequirementsSet(set):
"""Custom set that tracks whether updates have been run."""
def add(self, e):
global update_ran
update_ran &= _skip_update
super().add(e)
def update(self, *s):
global update_ran
update_ran &= _skip_update
super().update(*s)
# Initialize file sets
requirements_files = RequirementsSet({local_dir / "requirements.txt"})
worlds_files = {"wheels": RequirementsSet(), "apworlds": RequirementsSet()}
# Add wheel files if update hasn't run
if not update_ran:
custom_worlds_dir = local_dir / "custom_worlds"
if custom_worlds_dir.exists():
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))
# Only for unfrozen builds, overriding for frozen
python_cmd = sys.executable
if is_frozen():
# For frozen builds, install in a home directory to prevent readonly issues
exe_dir = Path(sys.exec_prefix)
default_libs_dir = Path(exe_dir, "lib")
worlds_install_dir = install_path()
if str(worlds_install_dir) not in sys.path:
sys.path.append(worlds_install_dir)
if str(default_libs_dir) not in sys.path:
sys.path.append(default_libs_dir)
# set up frozen pip command
if is_windows():
# Try to use system Python first, fall back to local if not available
if (install_path() / "Scripts" / "python.exe").exists():
python_cmd = install_path() / "Scripts" / "python.exe"
else:
system_python = shutil.which("python")
if system_python and "WindowsApps" not in system_python:
pass
else:
system_py = shutil.which("py")
py_output = subprocess.run([system_py, "-0p"], capture_output=True, text=True)
system_python = py_output.stdout.strip()
# Priority order: 3.12 → 3.13 → 3.11 → 3.10 → 3.9 → 3.8
# Exclude venv paths and test versions (like python3.13t.exe)
python_versions = []
for line in py_output.stdout.splitlines():
if "venv" in line:
continue
if "python.exe" in line:
# Extract version and path - handle both formats:
# Format 1: "-V:3.12 C:\Program Files\Python312\python.exe"
# Format 2: "-V:3.13 * C:\Users\Lindsay\AppData\Local\Programs\Python\Python313\python.exe"
parts = line.split()
if len(parts) >= 2:
version_part = parts[0]
# Handle the * marker in format 2
path = parts[-1] if parts[-1].endswith('.exe') else parts[1]
# Extract version number (e.g., "3.12" from "-V:3.12")
version_match = re.search(r'3\.(\d+)', version_part)
if version_match:
version_num = int(version_match.group(1))
if 10 <= version_num <= 14: # Valid range
python_versions.append((version_num, path))
# Sort by priority: 3.12 first, then descending order
def version_priority(item):
version_num = item[0]
if version_num == 12:
return 0 # Highest priority
else:
return 20 - version_num # 3.13=7, 3.11=9, 3.10=10, 3.9=11, 3.8=12
python_versions.sort(key=version_priority)
if python_versions:
system_python = python_versions[0][1]
if system_python and "WindowsApps" not in system_python:
pass
else:
raise RuntimeError("No Python found")
# Install windows venv
venv_path = install_path()
venv_path.mkdir(parents=True, exist_ok=True)
subprocess.run([system_python, "-m", "venv", str(venv_path)], check=True)
python_cmd = venv_path / "Scripts" / "python.exe"
elif is_macos() or is_linux():
# Create a venv in cache_path that uses the AppImage's python as base
venv_path = install_path()
if (venv_path / "bin" / "python").exists():
python_cmd = venv_path / "bin" / "python"
else:
logger.info(f"Creating venv in {str(venv_path)}")
system_python = shutil.which("python")
if not system_python:
system_python = shutil.which("python3")
else:
raise RuntimeError("No Python found")
subprocess.run([system_python, "-m", "venv", str(venv_path)], check=True)
python_cmd = venv_path / "bin" / "python"
else:
raise RuntimeError("Unsupported platform")
# Don't import pip directly, we can set/forget this instead.
subprocess.run([python_cmd, "-m", "ensurepip"])
def confirm(msg: str) -> None:
"""Get user confirmation for an action."""
try:
input(f"\n{msg}")
except KeyboardInterrupt:
logger.info("\nAborting")
sys.exit(1)
def parse_requirements_file(file_path: Path) -> List[str]:
"""
Parse a requirements.txt file and return a list of requirement strings.
Handles line continuations, comments, and various requirement formats.
"""
requirements = []
with open(file_path, 'r') as f:
lines = f.readlines()
prev_line = ""
for line in lines:
line = line.rstrip('\r\n')
# Handle line continuations
if line.endswith('\\'):
prev_line += line[:-1] + " "
continue
line = prev_line + line
prev_line = ""
# Skip empty lines and comments
if not line.strip() or line.strip().startswith('#'):
continue
# Remove hash specifications for version checking
line = line.split("--hash=")[0].strip()
# Handle URL-based requirements
if line.startswith(("https://", "git+https://")):
line = _parse_url_requirement(line)
# Handle custom PEP 508 syntax
elif "@" in line and "#" in line:
line = _parse_custom_pep508_requirement(line)
if line.strip():
requirements.append(line.strip())
return requirements
def _parse_url_requirement(line: str) -> str:
"""Parse URL-based requirements and extract package name and version."""
rest = line.split('/')[-1]
# Extract from filename
if "@" in rest:
raise ValueError("Can't deduce version from requirement")
rest = rest.replace(".zip", "-").replace(".tar.gz", "-")
try:
name, version, _ = rest.split("-", 2)
return f'{name}=={version}'
except ValueError:
return ""
def _parse_custom_pep508_requirement(line: str) -> str:
"""Parse custom PEP 508 syntax: name @ url#version ; marker."""
name, rest = line.split("@", 1)
version = rest.split("#", 1)[1].split(";", 1)[0].rstrip()
result = f"{name.rstrip()}=={version}"
if ";" in rest: # keep marker
result += rest[rest.find(";"):]
return result
def check_for_updates(worlds_only: bool = False) -> List[str]:
"""
Check which packages need updates by querying PyPI.
Returns a list of package names that need updating.
"""
if is_frozen() and not worlds_only:
return []
# Ensure packaging is available
try:
import packaging.requirements
except ImportError:
logger.warning("packaging module not available, installing...")
executable_args = [python_cmd, "-m", "pip", "install", "--upgrade", "packaging"]
subprocess.run(executable_args)
import packaging.requirements
try:
if worlds_only:
executable_args = [python_cmd, "-m", "pip", "list", "-o", "--format", "json",
"-i", "https://pypi.multiworld.gg/mwgg/apworlds/+simple"]
else:
executable_args = [python_cmd, "-m", "pip", "list", "-o", "--format", "json",
"-i", "https://pypi.org/simple", "--extra-index-url", "https://pypi.multiworld.gg/mwgg/apworlds/+simple"]
logger.info(f"Executing subprocess command: {executable_args}")
logger.info(f"Working directory: {os.getcwd()}")
response = subprocess.run(executable_args, capture_output=True, text=True, timeout=45)
if response.returncode != 0:
logger.warning(f"Could not check for updates: {response.stderr}")
return []
outdated_packages = json.loads(response.stdout)
logger.info(f"Newer versions of the following packages are available: {outdated_packages}")
if worlds_only:
return [world["name"] for world in outdated_packages]
# Get all requirements to check version constraints
all_requirements = {}
for req_file in requirements_files:
if req_file.exists():
requirements = parse_requirements_file(req_file)
for req_line in requirements:
try:
requirement = packaging.requirements.Requirement(req_line)
all_requirements[requirement.name] = requirement
except packaging.requirements.InvalidRequirement:
continue
# Filter outdated packages based on requirements.txt constraints
packages_to_update = []
for pkg in outdated_packages:
pkg_name = pkg["name"]
latest_version = pkg["latest_version"]
# If package is in requirements.txt, check if update is allowed
if pkg_name in all_requirements:
requirement = all_requirements[pkg_name]
# Check if the latest version satisfies the requirement constraint
try:
# If the requirement has no version specifier, we can update
if not requirement.specifier:
packages_to_update.append(pkg_name)
else:
# Check if the latest version satisfies the current requirement
from packaging.version import parse as parse_version
latest_ver = parse_version(latest_version)
# Test if the latest version satisfies the requirement
if latest_ver in requirement.specifier:
packages_to_update.append(pkg_name)
else:
logger.debug(f"Skipping {pkg_name}: latest version {latest_version} doesn't satisfy requirement {requirement}")
except Exception as e:
# If we can't parse the version, skip it
logger.debug(f"Skipping {pkg_name}: couldn't check version constraint: {e}")
else:
# Package not in requirements.txt, so we can update it
packages_to_update.append(pkg_name)
return packages_to_update
except (subprocess.TimeoutExpired, json.JSONDecodeError, FileNotFoundError) as e:
logger.warning(f"Could not check for updates: {e}")
return []
_WORLD_MODULES_CACHE_TTL = 300 # 5 minutes
_world_modules_cache_path = Path(tempfile.gettempdir()) / "MultiworldGG" / "world_modules_cache.json"
def uninstall_worlds(worlds: List[str]) -> None:
"""Uninstall a list of mwgg packages from the multiworld repository."""
for world in worlds:
executable_args = [python_cmd, "-m", "pip", "uninstall", world, "--yes"]
subprocess.run(executable_args)
try:
_world_modules_cache_path.unlink(missing_ok=True)
except Exception:
pass
def find_world_modules() -> set[str]:
"""Find all world modules in the multiworld repository and currently installed packages."""
# Check cache
try:
with open(_world_modules_cache_path, 'r', encoding='utf-8') as f:
cache_data = json.load(f)
if time.time() - cache_data.get('timestamp', 0) < _WORLD_MODULES_CACHE_TTL:
return set(cache_data.get('modules', []))
except Exception:
pass
world_modules = []
# First, fetch from the repository
try:
# Fetch the simple index page from the multiworld PyPI repository
url = "https://pypi.multiworld.gg/mwgg/apworlds/+simple"
# Set up request with timeout
req = urllib.request.Request(url)
req.add_header('Accept', 'application/vnd.pypi.simple.v1+json')
req.add_header('User-Agent', 'MultiWorldGG/1.0')
with urllib.request.urlopen(req, timeout=15) as response:
json_content = response.read().decode('utf-8')
# Parse the JSON to extract package names
packages = json.loads(json_content)
for package in packages['projects']:
if package['name'].startswith("worlds"):
package_name = package['name'][7:].replace("-", "_")
world_modules.append(package_name)
except (urllib.error.URLError, urllib.error.HTTPError, TimeoutError) as e:
logger.warning(f"Failed to fetch world modules from {url}: {e}")
except Exception as e:
logger.warning(f"Unexpected error while fetching world modules: {e}")
world_modules_set = set(world_modules)
# Also check for currently installed world modules
try:
executable_args = [python_cmd, "-m", "pip", "list", "--format", "json"]
logger.debug(f"Executing subprocess command to find installed worlds: {executable_args}")
response = subprocess.run(executable_args, capture_output=True, text=True, timeout=45)
if response.returncode == 0:
installed_packages = json.loads(response.stdout)
for package in installed_packages:
package_name = package.get("name", "")
if package_name.startswith("worlds"):
world_name = package_name[7:] # Remove "worlds. or worlds-" prefix
if not world_name.startswith("_") and world_name not in world_modules_set:
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}")
# Update cache
try:
_world_modules_cache_path.parent.mkdir(parents=True, exist_ok=True)
with open(_world_modules_cache_path, 'w', encoding='utf-8') as f:
json.dump({'timestamp': time.time(), 'modules': list(world_modules_set)}, f)
except Exception:
pass
return world_modules_set
def install_worlds(worlds: List[str], update: bool = False, no_recurse: bool = False) -> list[str]:
"""
Install worlds from the multiworld repository.
This will install worlds from the multiworld repository. It will also check for additional
updates after installation completes.
If additional updates are found, the restart flag will be set to True.
Args:
worlds: List of world packages to install
update: If True, uninstall old versions first
no_recurse: If True, do not check for additional updates after installation completes.
Returns:
True if additional updates were found, False otherwise.
"""
apworlds = []
if update:
logger.info(f"Uninstalling old versions of: {worlds}")
uninstall_worlds(worlds)
for idx, world in enumerate(worlds):
if update:
logger.info(f"Updating world: {world}")
else:
logger.info(f"Installing world: {world}")
if is_frozen():
# In frozen environments, we need to install to a location that's in the Python path
# and ensure we use the correct target directory
executable_args = [python_cmd, "-m", "pip", "install", "--no-deps", "--index-url", "https://pypi.org/simple",
"--extra-index-url", "https://pypi.multiworld.gg/mwgg/apworlds",
world, "--prefer-binary", "--upgrade", "--no-cache-dir"]
logger.info(f"Executing subprocess command: {executable_args}")
# Use threading instead of multiprocessing to avoid argument contamination
import threading
import queue
result_queue = queue.Queue()
def _pip_install_thread():
try:
result = subprocess.run(executable_args, capture_output=True, text=True)
result_queue.put((result.returncode, result.stdout, result.stderr))
except Exception as e:
result_queue.put((1, "", str(e)))
install_thread = threading.Thread(target=_pip_install_thread, daemon=True)
install_thread.start()
install_thread.join()
# Get the return values from the worker thread
try:
returncode, stdout, stderr = result_queue.get_nowait()
logger.info(stdout)
except:
returncode = 1 # Assume failure if we can't get the result
stdout = ""
stderr = "Failed to get process result"
if returncode != 0:
logger.warning(f"World {world} failed to install")
if stderr:
logger.error(f"{stderr}")
# Add to custom worlds list if it exists there
apworld_file = custom_worlds_dir / f"{world.replace("worlds.", "")}.apworld"
if apworld_file.exists():
logger.info(f"Found apworld file: {apworld_file}")
apworlds.append(world)
else:
logger.warning(f"Custom apworld file not found at {apworld_file}, {world} will be installed from PyPI")
else:
# Before moving files, process all installed packages
logger.debug(f"Processing downloaded packages...")
else:
executable_args = [python_cmd, "-m", "pip", "install",
"--extra-index-url", "https://pypi.multiworld.gg/mwgg/apworlds",
world, "--prefer-binary", "--upgrade", "--no-cache-dir"]
result = subprocess.run(executable_args)
if result.returncode != 0:
logger.warning(f"Failed to install {world} from pypi, checking custom worlds")
# Add to custom worlds list if it exists there
apworld_file = custom_worlds_dir / f"{world.replace("worlds.", "")}.apworld"
if apworld_file.exists():
logger.info(f"Found apworld file: {apworld_file}")
apworlds.append(world)
else:
logger.warning(f"Custom apworld file not found at {apworld_file}, please verify that this world exists.")
else:
logger.info(f"Successfully installed {world}")
invalidate_caches()
try:
_world_modules_cache_path.unlink(missing_ok=True)
except Exception:
pass
if is_frozen():
# Check for any additional updates that might be needed
logger.info("Checking for additional dependencies...")
additional_deps_args = [python_cmd, "-m", "pip", "check"]
additional_deps_result = subprocess.run(additional_deps_args, capture_output=True, text=True)
stdout = additional_deps_result.stdout
no_deps = ("No broken requirements found." in stdout)
if no_deps:
logger.info(f"Updates complete.")
return apworlds
# Parse dependencies from pip check output
# Handles: "pyramid 1.5.2 requires WebOb, which is not installed."
# Handles: "pyramid 1.5.2 has requirement WebOb>=1.3.1, but you have WebOb 0.8."
else:
packages_to_install = []
for line in stdout.splitlines():
match = re.search(r'(?:requires|has requirement)\s+([a-zA-Z0-9_-]+)([><=!.0-9]+)?', line)
if match:
package = match.group(1)
version_req = match.group(2) if match.group(2) else ""
install_spec = f"{package}{version_req}"
packages_to_install.append(install_spec)
update_requirements(packages_to_install)
return apworlds
def update_world_from_package() -> None:
"""Install/update wheel files from custom_worlds directory."""
# Use threading version if frozen, otherwise use subprocess
if is_frozen():
for world in worlds_files["wheels"]:
logger.info(f"Installing wheel: {world}")
executable_args = [python_cmd, "-m", "pip", "install", world, "--upgrade",
"--prefer-binary", "--no-cache-dir"]
# Use threading instead of multiprocessing to avoid argument contamination
import threading
import queue
result_queue = queue.Queue()
def _pip_install_thread():
try:
result = subprocess.run(executable_args, capture_output=True, text=True)
result_queue.put((result.returncode, result.stdout, result.stderr))
except Exception as e:
result_queue.put((1, "", str(e)))
install_thread = threading.Thread(target=_pip_install_thread, daemon=True)
install_thread.start()
install_thread.join()
# Get the return values from the worker thread
try:
returncode, stdout, stderr = result_queue.get_nowait()
logger.info(stdout)
except:
returncode = 1 # Assume failure if we can't get the result
stdout = ""
stderr = "Failed to get process result"
if returncode != 0:
logger.warning(f"Failed to install wheel {wheel}")
if stderr:
logger.error(f"{stderr}")
else:
logger.info(f"Successfully installed wheel {wheel}")
for world in worlds_files["apworlds"]:
logger.info(f"APWorld found, checking versions: {world}")
try:
# Extract module name from apworld filename (e.g., "world_name.apworld" -> "world_name")
world_path = Path(world)
module_name = world_path.stem # Gets filename without extension
# Read version from the apworld zip file using APWorldContainer
new_version: Optional[Version] = None
manifest: dict[str, object] = {}
try:
apworld_container = APWorldContainer(world)
# Set manifest path to expected location
with zipfile.ZipFile(world, 'r') as apworld_zip:
manifest = apworld_container.read_contents(apworld_zip)
if "world_version" in manifest:
new_version = tuplize_version(manifest["world_version"])
logger.info(f"APworld {world} has version {new_version}")
else:
logger.info(f"APworld {world} has no world_version specified")
except Exception as e:
logger.warning(f"Failed to read version from APworld {world}: {e}")
# Check if world is already installed using pip show
package_name = f"worlds.{module_name}"
installed_version: Optional[Version] = None
try:
executable_args = [python_cmd, "-m", "pip", "show", package_name]
result = subprocess.run(executable_args, capture_output=True, text=True, timeout=10)
if result.returncode == 0:
# Package is installed, parse version from output
for line in result.stdout.splitlines():
if line.startswith("Version:"):
version_str = line.split(":", 1)[1].strip()
installed_version = tuplize_version(version_str)
logger.info(f"Installed world {module_name} has version {version_str}")
break
else:
logger.info(f"Installed world {module_name} found but no version in pip show output")
else:
# Package not installed
logger.info(f"World {module_name} is not installed")
except (subprocess.TimeoutExpired, Exception) as e:
logger.warning(f"Failed to check installed version for {module_name} using pip show: {e}")
# Compare versions: install if new_version > installed_version
# According to spec: "An APWorld without a world_version is always treated as older than one with a version"
if new_version is None and installed_version is not None:
logger.info(f"There is a custom apworld file with no world version specified, please remove it from your custom_worlds directory.")
elif installed_version is None or new_version > installed_version:
if installed_version is not None:
uninstall_worlds([package_name])
logger.info(f"New version {new_version.as_simple_string()} > installed {installed_version.as_simple_string()}, uninstalling old version so new version will be picked up.")
else:
logger.info(f"There is a custom apworld file with an older version than what is installed. Please remove it from your custom_worlds directory.")
except Exception as e:
logger.warning(f"Failed to check versions for APworld {world}: {e}")
else:
for wheel in worlds_files["wheels"]:
logger.info(f"Installing wheel: {wheel}")
executable_args = [python_cmd, "-m", "pip", "install", wheel, "--upgrade"]
result = subprocess.run(executable_args)
if result.returncode != 0:
logger.warning(f"Failed to install wheel {wheel}")
else:
logger.info(f"Successfully installed wheel {wheel}")
def update_requirements(needed_packages: List[str]) -> None:
"""
Update packages from requirements.txt files and install worlds.
Args:
needed_packages: List of packages that need updating
"""
# Ensure packaging is available
try:
import packaging.requirements
except ImportError:
logger.warning("packaging module not available, installing...")
subprocess.run([python_cmd, "-m", "pip", "install", "--upgrade", "packaging"])
import packaging.requirements
# If needed_packages is empty, update all requirements (for force mode or missing requirements)
update_all = len(needed_packages) == 0
# Handle regular requirements from files
for req_file in requirements_files:
if not req_file.exists():
logger.warning(f"Requirements file not found: {req_file}")
continue
logger.debug(f"Processing requirements from: {req_file}")
requirements = parse_requirements_file(req_file)
packages_to_update = []
for req_line in requirements:
try:
requirement = packaging.requirements.Requirement(req_line)
# Update if: force mode, package needs update, or package is missing
if update_all or requirement.name in needed_packages:
packages_to_update.append(req_line)
except packaging.requirements.InvalidRequirement:
logger.warning(f"Invalid requirement line: {req_line}")
continue
if packages_to_update:
logger.info(f"Installing/updating packages: {[req.split('==')[0] if '==' in req else req.split('>=')[0] if '>=' in req else req for req in packages_to_update]}")
for package in packages_to_update:
executable_args = [python_cmd, "-m", "pip", "install", "--upgrade", package]
result = subprocess.run(executable_args)
if result.returncode != 0:
logger.warning(f"Failed to install/update {package}")
else:
logger.info("No packages from this requirements file need updating.")
# Handle worlds (these are not in requirements.txt files)
worlds_to_install = [pkg for pkg in needed_packages if pkg.startswith("worlds") or pkg.startswith("mwgg")]
if worlds_to_install:
logger.info(f"Installing/updating worlds: {worlds_to_install}")
install_worlds(worlds_to_install)
def install_packaging(yes: bool = False) -> None:
"""Install packaging module if not available."""
try:
import packaging.requirements # noqa: F401
except ImportError:
if not yes:
confirm("packaging not found, press enter to install it")
executable_args = [python_cmd, "-m", "pip", "install", "--upgrade", "packaging"]
subprocess.run(executable_args)
def check_requirements_satisfied(yes: bool = False) -> bool:
"""
Check if all requirements are satisfied.
Returns True if all requirements are met, False otherwise.
"""
install_packaging(yes=yes)
try:
import packaging.requirements
import importlib.metadata
except ImportError:
return False
all_satisfied = True
for req_file in requirements_files:
if not req_file.exists():
logger.warning(f"Requirements file not found: {req_file}")
continue
requirements = parse_requirements_file(req_file)
for req_line in requirements:
try:
requirement = packaging.requirements.Requirement(req_line)
try:
importlib.metadata.distribution(requirement.name)
except importlib.metadata.PackageNotFoundError:
logger.warning(f"Missing requirement: {requirement.name}")
all_satisfied = False
if not yes:
confirm(f"Requirement {requirement.name} is not satisfied, press enter to install it")
except packaging.requirements.InvalidRequirement:
logger.warning(f"Invalid requirement line: {req_line}")
continue
return all_satisfied
def update(yes: bool = True, force: bool = False, worlds: Optional[List[str]] = None) -> None:
"""
Main update function.
Args:
yes: Answer yes to all prompts
force: Force update without checking
worlds: List of specific worlds to update
Returns:
None
"""
if is_frozen():
if (exe_dir / "custom_wheels").exists():
logger.debug("Custom Worlds found, checking...")
update_world_from_package()
updates = check_for_updates(worlds_only=True)
if updates:
restart_needed = install_worlds(updates)
if restart_needed:
# Library updates were staged, need to restart
from Utils import exit_restart_for_update
exit_restart_for_update()
else:
logger.debug("No updates found.")
global update_ran
if update_ran:
return
update_ran = True
if force:
logger.debug("Force update requested - skipping update checks")
# Force mode updates all requirements and worlds
update_requirements([]) # Empty list means update all
# Check for available updates
logger.debug("Checking for available updates...")
available_updates = check_for_updates()
if available_updates:
logger.debug(f"Found updates for: {available_updates}")
if not yes:
confirm("Updates available. Press enter to continue with updates.")
else:
logger.debug("No updates found.")
# Check if requirements are satisfied
logger.debug("Checking if all requirements are satisfied...")
if not check_requirements_satisfied(yes=yes):
logger.debug("Installing missing requirements...")
update_requirements([]) # Empty list means update all missing requirements
# Update packages that need updates (including worlds)
if available_updates:
logger.debug("Updating packages that need updates...")
update_requirements(available_updates)
logger.debug("Update process completed.")
class RestartException(Exception):
"""Exception raised when a restart is needed."""
pass
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Install archipelago requirements')
parser.add_argument('-y', '--yes', dest='yes', action='store_true',
help='answer "yes" to all questions')
parser.add_argument('-f', '--force', dest='force', action='store_true',
help='force update')
parser.add_argument('-a', '--append', nargs="*", dest='additional_requirements',
help='List paths to additional requirement files.')
parser.add_argument('-w', '--worlds', nargs="*", dest='worlds',
help='List of worlds to update.')
args = parser.parse_args()
if args.additional_requirements:
requirements_files.update([Path(req) for req in args.additional_requirements])
if args.worlds:
update(args.yes, args.force, args.worlds)
else:
update(args.yes, args.force)