Coverage for src/photo_cleanup_utils.py: 100%

16 statements  

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

1"""Photo file cleanup utilities module. 

2 

3This module contains utilities for cleaning up obsolete photo files 

4that are no longer on the server. 

5""" 

6 

7___author___ = "Mandar Patil <mandarons@pm.me>" 

8 

9from pathlib import Path 

10 

11from src import get_logger 

12 

13LOGGER = get_logger() 

14 

15 

16def remove_obsolete_files(destination_path: str | None, tracked_files: set[str] | None) -> set[str]: 

17 """Remove local obsolete files that are no longer on server. 

18 

19 Args: 

20 destination_path: Path to search for obsolete files 

21 tracked_files: Set of files that should be kept (files on server) 

22 

23 Returns: 

24 Set of paths that were removed 

25 """ 

26 removed_paths = set() 

27 

28 if not (destination_path and tracked_files is not None): 

29 return removed_paths 

30 

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

32 local_file = str(path.absolute()) 

33 if local_file not in tracked_files: 

34 if path.is_file(): 

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

36 path.unlink(missing_ok=True) 

37 removed_paths.add(local_file) 

38 

39 return removed_paths