Coverage for src/drive_cleanup.py: 100%
28 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-08 21:05 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-08 21:05 +0000
1"""Drive cleanup utilities.
3This module provides cleanup functionality for removing obsolete files
4and directories during iCloud Drive sync operations per SRP.
5"""
7__author__ = "Mandar Patil (mandarons@pm.me)"
9from pathlib import Path
10from shutil import rmtree
12from src import configure_icloudpy_logging, get_logger
14# Configure icloudpy logging immediately after import
15configure_icloudpy_logging()
17LOGGER = get_logger()
20def remove_obsolete(destination_path: str, files: set[str], recycle_bin=None) -> set[str]:
21 """Remove local files and directories that no longer exist remotely.
23 Args:
24 destination_path: Root directory to clean up
25 files: Set of file paths that should be kept (exist remotely)
26 recycle_bin: ``RecycleBin`` to move them to instead of deleting
28 Returns:
29 Set of paths that were removed
30 """
31 removed_paths = set()
32 if not (destination_path and files is not None):
33 return removed_paths
34 if recycle_bin:
35 recycle_bin.purge()
37 for path in Path(destination_path).rglob("*"):
38 local_file = str(path.absolute())
39 if recycle_bin and recycle_bin.contains(local_file):
40 continue
41 if local_file not in files:
42 if not (path.is_file() or path.is_dir()):
43 continue # already went with a folder removed above
44 if recycle_bin:
45 LOGGER.info(f"Moving {local_file} to the recycle bin ...")
46 recycle_bin.discard(local_file, destination_path)
47 else:
48 LOGGER.info(f"Removing {local_file} ...")
49 if path.is_file():
50 path.unlink(missing_ok=True)
51 else:
52 rmtree(local_file)
53 removed_paths.add(local_file)
54 return removed_paths