Add offline fallbacks for remote components #9

Merged
Dusk-92 merged 101 commits from test/remote-fallbacks into main 2026-09-04 15:03:01 +00:00
Showing only changes of commit 9ff415ef3f - Show all commits
+20 -187
View File
@@ -453,29 +453,6 @@ def _current_recorded_package_revision(target_dir, package_id):
return None return None
def _cache_current_managed_mpq(target_dir, mod_id, revision):
"""Seed cache from a managed MPQ and report whether the install is current."""
if not managed_mpq_is_current(target_dir, mod_id, revision):
return False
files = _load_managed_manifest(target_dir, mod_id)
if len(files) != 1:
return False
source_path = os.path.join(target_dir, files[0])
try:
_verify_mpq(source_path)
except RemotePackageError:
return False
_seed_cached_file_from_installed(
target_dir,
mod_id,
source_path,
"payload.mpq",
revision=revision,
validator=_verify_mpq,
)
return True
def _runtime_base_path(): def _runtime_base_path():
return getattr( return getattr(
sys, sys,
@@ -2140,7 +2117,7 @@ def _install_remote_mpq(
timeout=300, timeout=300,
revision=None, revision=None,
): ):
if revision is not None and _cache_current_managed_mpq( if revision is not None and managed_mpq_is_current(
target_dir, target_dir,
mod_id, mod_id,
revision, revision,
@@ -2153,80 +2130,34 @@ def _install_remote_mpq(
) )
return return
downloaded_path = None temp_path = _download(
source_path = None
try:
downloaded_path = _download(
url, url,
suffix=".mpq", suffix=".mpq",
progress=progress, progress=progress,
label=label, label=label,
timeout=timeout, timeout=timeout,
) )
_verify_mpq(downloaded_path)
source_path = downloaded_path
except Exception as download_error:
cached = _load_cached_file(
target_dir,
mod_id,
"payload.mpq",
expected_revision=revision,
)
if cached is not None:
source_path, _ = cached
_verify_mpq(source_path)
_emit_progress(
progress,
f"{label.replace('Downloading ', '')}: using cached offline fallback.",
None,
None,
)
else:
bundled = _load_bundled_remote_fallback(
mod_id,
"payload.mpq",
expected_revision=revision,
)
if bundled is None:
raise download_error
source_path, _ = bundled
_verify_mpq(source_path)
_emit_progress(
progress,
f"{label.replace('Downloading ', '')}: using bundled offline fallback.",
None,
None,
)
try: try:
_emit_progress(progress, f"Installing {os.path.basename(destination)}...", None, None) _verify_mpq(temp_path)
_emit_progress(
progress,
f"Installing {os.path.basename(destination)}...",
None,
None,
)
_install_managed_files( _install_managed_files(
target_dir, target_dir,
mod_id, mod_id,
[(source_path, destination)], [(temp_path, destination)],
revision=revision, revision=revision,
) )
if downloaded_path is not None and _store_cached_file_safely(
target_dir,
mod_id,
downloaded_path,
"payload.mpq",
revision=revision,
) is None:
_emit_progress(
progress,
f"{label.replace('Downloading ', '')} installed; offline cache could not be refreshed.",
None,
None,
)
finally: finally:
if downloaded_path and os.path.exists(downloaded_path):
try: try:
os.remove(downloaded_path) os.remove(temp_path)
except OSError: except OSError:
pass pass
def _google_drive_download_url(file_id): def _google_drive_download_url(file_id):
return ( return (
"https://drive.usercontent.google.com/download" "https://drive.usercontent.google.com/download"
@@ -2293,8 +2224,6 @@ def install_pink_herbs(target_dir, progress=None):
mod_id = "visual_pink_herbs" mod_id = "visual_pink_herbs"
destination = os.path.join("Data", "patch-V.mpq") destination = os.path.join("Data", "patch-V.mpq")
target_path = os.path.join(target_dir, destination) target_path = os.path.join(target_dir, destination)
downloaded_path = None
source_path = None
installed_revision = _current_recorded_package_revision(target_dir, mod_id) installed_revision = _current_recorded_package_revision(target_dir, mod_id)
try: try:
@@ -2306,14 +2235,6 @@ def install_pink_herbs(target_dir, progress=None):
except RemotePackageError: except RemotePackageError:
installed_revision = None installed_revision = None
else: else:
_seed_cached_file_from_installed(
target_dir,
mod_id,
target_path,
"payload.mpq",
revision=installed_revision,
validator=_verify_mpq,
)
_emit_progress( _emit_progress(
progress, progress,
f"Pink Herbs {installed_revision[:7]} kept; update source unavailable.", f"Pink Herbs {installed_revision[:7]} kept; update source unavailable.",
@@ -2321,49 +2242,14 @@ def install_pink_herbs(target_dir, progress=None):
None, None,
) )
return f"seacrabsam/patch-herb main@{installed_revision[:7]}" return f"seacrabsam/patch-herb main@{installed_revision[:7]}"
cached = _load_cached_file(target_dir, mod_id, "payload.mpq")
if cached is not None:
source_path, metadata = cached
revision = str(metadata.get("revision") or "cached")
_verify_mpq(source_path)
_emit_progress(
progress,
"Pink Herbs: using cached offline fallback.",
None,
None,
)
else:
bundled = _load_bundled_remote_fallback(
mod_id,
"payload.mpq",
)
if bundled is None:
raise revision_error raise revision_error
source_path, metadata = bundled
revision = str(metadata.get("revision") or "bundled")
_verify_mpq(source_path)
_emit_progress(
progress,
"Pink Herbs: using bundled offline fallback.",
None,
None,
)
else:
if installed_revision == revision: if installed_revision == revision:
try: try:
_verify_mpq(target_path) _verify_mpq(target_path)
except RemotePackageError: except RemotePackageError:
installed_revision = None installed_revision = None
else: else:
_seed_cached_file_from_installed(
target_dir,
mod_id,
target_path,
"payload.mpq",
revision=revision,
validator=_verify_mpq,
)
_emit_progress( _emit_progress(
progress, progress,
f"Pink Herbs {revision[:7]} is already current.", f"Pink Herbs {revision[:7]} is already current.",
@@ -2373,32 +2259,20 @@ def install_pink_herbs(target_dir, progress=None):
return f"seacrabsam/patch-herb main@{revision[:7]}" return f"seacrabsam/patch-herb main@{revision[:7]}"
try: try:
downloaded_path = _download( temp_path = _download(
f"https://raw.githubusercontent.com/seacrabsam/patch-herb/{revision}/patch-H.mpq", f"https://raw.githubusercontent.com/seacrabsam/patch-herb/{revision}/patch-H.mpq",
suffix=".mpq", suffix=".mpq",
progress=progress, progress=progress,
label="Downloading Pink Herbs", label="Downloading Pink Herbs",
timeout=300, timeout=300,
) )
_verify_mpq(downloaded_path)
source_path = downloaded_path
except Exception as download_error: except Exception as download_error:
# Never replace a valid installed revision with an older cache just
# because the newest remote payload is temporarily unavailable.
if installed_revision is not None: if installed_revision is not None:
try: try:
_verify_mpq(target_path) _verify_mpq(target_path)
except RemotePackageError: except RemotePackageError:
installed_revision = None installed_revision = None
else: else:
_seed_cached_file_from_installed(
target_dir,
mod_id,
target_path,
"payload.mpq",
revision=installed_revision,
validator=_verify_mpq,
)
_emit_progress( _emit_progress(
progress, progress,
f"Pink Herbs {installed_revision[:7]} kept; latest download unavailable.", f"Pink Herbs {installed_revision[:7]} kept; latest download unavailable.",
@@ -2406,37 +2280,11 @@ def install_pink_herbs(target_dir, progress=None):
None, None,
) )
return f"seacrabsam/patch-herb main@{installed_revision[:7]}" return f"seacrabsam/patch-herb main@{installed_revision[:7]}"
cached = _load_cached_file(target_dir, mod_id, "payload.mpq")
if cached is not None:
source_path, metadata = cached
revision = str(metadata.get("revision") or revision)
_verify_mpq(source_path)
_emit_progress(
progress,
"Pink Herbs: using cached offline fallback.",
None,
None,
)
else:
bundled = _load_bundled_remote_fallback(
mod_id,
"payload.mpq",
)
if bundled is None:
raise download_error raise download_error
source_path, metadata = bundled
revision = str(metadata.get("revision") or revision)
_verify_mpq(source_path)
_emit_progress(
progress,
"Pink Herbs: using bundled offline fallback.",
None,
None,
)
try: try:
_migrate_legacy_pink_herbs_patch(target_dir, source_path, progress=progress) _verify_mpq(temp_path)
_migrate_legacy_pink_herbs_patch(target_dir, temp_path, progress=progress)
_emit_progress( _emit_progress(
progress, progress,
f"Installing {os.path.basename(destination)}...", f"Installing {os.path.basename(destination)}...",
@@ -2446,7 +2294,7 @@ def install_pink_herbs(target_dir, progress=None):
_install_managed_files( _install_managed_files(
target_dir, target_dir,
mod_id, mod_id,
[(source_path, destination)], [(temp_path, destination)],
revision=VISUAL_MOD_REVISIONS[mod_id], revision=VISUAL_MOD_REVISIONS[mod_id],
) )
_record_package_state_safely( _record_package_state_safely(
@@ -2455,29 +2303,14 @@ def install_pink_herbs(target_dir, progress=None):
revision, revision,
[destination], [destination],
) )
if downloaded_path is not None and _store_cached_file_safely(
target_dir,
mod_id,
downloaded_path,
"payload.mpq",
revision=revision,
) is None:
_emit_progress(
progress,
"Pink Herbs installed; offline cache could not be refreshed.",
None,
None,
)
finally: finally:
if downloaded_path and os.path.exists(downloaded_path):
try: try:
os.remove(downloaded_path) os.remove(temp_path)
except OSError: except OSError:
pass pass
suffix = revision[:7] if len(revision) >= 7 else revision return f"seacrabsam/patch-herb main@{revision[:7]}"
return f"seacrabsam/patch-herb main@{suffix}"
def _download_github_branch_archive( def _download_github_branch_archive(
repo, repo,