Coverage for src/photo_cleanup_utils.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 17:25 +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( 

17 destination_path: str | None, 

18 tracked_files: set[str] | None, 

19 exclude_filenames: set[str] | None = None, 

20) -> set[str]: 

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

22 

23 Args: 

24 destination_path: Path to search for obsolete files 

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

26 exclude_filenames: Set of filenames (basename only) that must never 

27 be removed even if they are not in ``tracked_files``. Used to 

28 protect the mount-marker sentinel file from cleanup. 

29 

30 Returns: 

31 Set of paths that were removed 

32 """ 

33 removed_paths = set() 

34 

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

36 return removed_paths 

37 

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

39 local_file = str(path.absolute()) 

40 if local_file not in tracked_files: 

41 if path.is_file(): 

42 if exclude_filenames and path.name in exclude_filenames: 

43 continue 

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

45 path.unlink(missing_ok=True) 

46 removed_paths.add(local_file) 

47 

48 return removed_paths