Coverage for src/drive_cleanup.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-16 04:41 +0000

1"""Drive cleanup utilities. 

2 

3This module provides cleanup functionality for removing obsolete files 

4and directories during iCloud Drive sync operations per SRP. 

5""" 

6 

7__author__ = "Mandar Patil (mandarons@pm.me)" 

8 

9from pathlib import Path 

10from shutil import rmtree 

11 

12from src import configure_icloudpy_logging, get_logger 

13 

14# Configure icloudpy logging immediately after import 

15configure_icloudpy_logging() 

16 

17LOGGER = get_logger() 

18 

19 

20def remove_obsolete(destination_path: str, files: set[str]) -> set[str]: 

21 """Remove local files and directories that no longer exist remotely. 

22 

23 Args: 

24 destination_path: Root directory to clean up 

25 files: Set of file paths that should be kept (exist remotely) 

26 

27 Returns: 

28 Set of paths that were removed 

29 """ 

30 removed_paths = set() 

31 if not (destination_path and files is not None): 

32 return removed_paths 

33 

34 for path in Path(destination_path).rglob("*"): 

35 local_file = str(path.absolute()) 

36 if local_file not in files: 

37 LOGGER.info(f"Removing {local_file} ...") 

38 if path.is_file(): 

39 path.unlink(missing_ok=True) 

40 removed_paths.add(local_file) 

41 elif path.is_dir(): 

42 rmtree(local_file) 

43 removed_paths.add(local_file) 

44 return removed_paths