feat: remove orphan local files that no longer exist on server in update_mods
This commit is contained in:
@@ -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,46 +124,65 @@ 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
|
||||||
with tqdm(
|
if stale:
|
||||||
total=len(stale), unit="file",
|
with tqdm(
|
||||||
desc=f" {folder_name[-COL_MOD:]:<{COL_MOD}}",
|
total=len(stale), unit="file",
|
||||||
position=0, leave=True, dynamic_ncols=True,
|
desc=f" {folder_name[-COL_MOD:]:<{COL_MOD}}",
|
||||||
) as file_bar:
|
position=0, leave=True, dynamic_ncols=True,
|
||||||
for rel, url, size in stale:
|
) as file_bar:
|
||||||
dest_file = mod_dir / rel
|
for rel, url, size in stale:
|
||||||
with tqdm(
|
dest_file = mod_dir / rel
|
||||||
total=size if size else None,
|
with tqdm(
|
||||||
unit="B", unit_scale=True, unit_divisor=1024,
|
total=size if size else None,
|
||||||
desc=f" {rel[-40:]:40s}",
|
unit="B", unit_scale=True, unit_divisor=1024,
|
||||||
position=1, leave=False, dynamic_ncols=True,
|
desc=f" {rel[-40:]:40s}",
|
||||||
) as chunk_bar:
|
position=1, leave=False, dynamic_ncols=True,
|
||||||
n = download_file(url, dest_file, session,
|
) as chunk_bar:
|
||||||
on_chunk=lambda b: chunk_bar.update(b))
|
n = download_file(url, dest_file, session,
|
||||||
mod_bytes += n
|
on_chunk=lambda b: chunk_bar.update(b))
|
||||||
file_bar.update(1)
|
mod_bytes += n
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user