feat: remove orphan local files that no longer exist on server in update_mods

This commit is contained in:
Tran G. (Revernomad) Khoa
2026-04-11 09:21:45 +07:00
parent 4fde566cf4
commit 50990cca4e

View File

@@ -102,6 +102,7 @@ def main() -> None:
COL_GROUP = 24 COL_GROUP = 24
total_checked = total_updated = total_bytes = 0 total_checked = total_updated = total_bytes = 0
total_removed = 0
not_on_server = [] not_on_server = []
for group, folder_name, mod_dir in targets: for group, folder_name, mod_dir in targets:
@@ -123,13 +124,21 @@ def main() -> None:
all_files = list_mod_files(folder_url, session) if not args.force else stale all_files = list_mod_files(folder_url, session) if not args.force else stale
checked = len(all_files) if not args.force else len(stale) checked = len(all_files) if not args.force else len(stale)
if not stale: # Find local files that no longer exist on the server (orphans)
server_rel = {rel for rel, _, _ in all_files}
orphans = [
f for f in mod_dir.rglob("*") if f.is_file()
and str(f.relative_to(mod_dir)).replace("\\", "/") not in server_rel
]
if not stale and not orphans:
print(f" [=] {folder_name:<{COL_MOD}} {group:<{COL_GROUP}} {checked} files up-to-date") print(f" [=] {folder_name:<{COL_MOD}} {group:<{COL_GROUP}} {checked} files up-to-date")
total_checked += checked total_checked += checked
continue continue
# Download stale files # Download stale files
mod_bytes = 0 mod_bytes = 0
if stale:
with tqdm( with tqdm(
total=len(stale), unit="file", total=len(stale), unit="file",
desc=f" {folder_name[-COL_MOD:]:<{COL_MOD}}", desc=f" {folder_name[-COL_MOD:]:<{COL_MOD}}",
@@ -148,21 +157,32 @@ def main() -> None:
mod_bytes += n mod_bytes += n
file_bar.update(1) file_bar.update(1)
# Remove orphan files
for orphan in orphans:
tqdm.write(f" [-] orphan removed: {orphan.relative_to(mod_dir)}")
orphan.unlink()
total_checked += checked total_checked += checked
total_updated += len(stale) total_updated += len(stale)
total_bytes += mod_bytes total_bytes += mod_bytes
print(f" [+] {folder_name:<{COL_MOD}} {group:<{COL_GROUP}} " total_removed += len(orphans)
f"{checked} files {len(stale)} updated ({_fmt_bytes(mod_bytes)})") parts = [f"{checked} files"]
if stale:
parts.append(f"{len(stale)} updated ({_fmt_bytes(mod_bytes)})")
if orphans:
parts.append(f"{len(orphans)} orphan(s) removed")
print(f" [+] {folder_name:<{COL_MOD}} {group:<{COL_GROUP}} {' '.join(parts)}")
print(f"\n{'='*56}") print(f"\n{'='*56}")
print(f" Total: {total_checked} files checked, " print(f" Total: {total_checked} files checked, "
f"{total_updated} updated, " f"{total_updated} updated, "
f"{total_removed} orphan(s) removed, "
f"{_fmt_bytes(total_bytes)} downloaded") f"{_fmt_bytes(total_bytes)} downloaded")
if not_on_server: if not_on_server:
print(f" Not found on server ({len(not_on_server)}): {', '.join(not_on_server)}") print(f" Not found on server ({len(not_on_server)}): {', '.join(not_on_server)}")
print(f"{'='*56}\n") print(f"{'='*56}\n")
if total_updated == 0 and not not_on_server: if total_updated == 0 and total_removed == 0 and not not_on_server:
print(" All mods are up-to-date.\n") print(" All mods are up-to-date.\n")