snapshot.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import itertools
  2. import pathlib
  3. IS_A_HOOK = False
  4. def get_contained_directories(parent_directory, candidate_contained_directories):
  5. '''
  6. Given a parent directory and a set of candiate directories potentially inside it, get the subset
  7. of contained directories for which the parent directory is actually the parent, a grandparent,
  8. the very same directory, etc. The idea is if, say, /var/log and /var/lib are candidate contained
  9. directories, but there's a parent directory (logical volume, dataset, subvolume, etc.) at /var,
  10. then /var is what we want to snapshot.
  11. Also mutate the given set of candidate contained directories to remove any actually contained
  12. directories from it. That way, this function can be called multiple times, successively
  13. processing candidate directories until none are left—and avoiding assigning any candidate
  14. directory to more than one parent directory.
  15. '''
  16. if not candidate_contained_directories:
  17. return ()
  18. contained = tuple(
  19. candidate
  20. for candidate in candidate_contained_directories
  21. if parent_directory == candidate
  22. or pathlib.PurePosixPath(parent_directory) in pathlib.PurePath(candidate).parents
  23. )
  24. candidate_contained_directories -= set(contained)
  25. return contained