From 529c4a7ddaff58f6d73a6390e9bbd369cdf38f54 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 26 Nov 2018 13:37:01 +0000 Subject: [PATCH] Don't overwrite files if the contents match --- PFERD/organizer.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/PFERD/organizer.py b/PFERD/organizer.py index 95482b4..0a0f2b5 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -50,26 +50,30 @@ class Organizer: # check if sync_dir/to_path is inside sync_dir? to_path = pathlib.Path(self._sync_dir, to_path) + # remember path for later reference + self._added_files.add(to_path.resolve()) + if to_path.exists(): - if not filecmp.cmp(from_path, to_path, shallow=False): - logger.info(f"Changed file at {to_path}.") + if filecmp.cmp(from_path, to_path, shallow=False): + logger.info(f"Done nothing at {to_path}") + # No further action needed, especially not overwriting symlinks... + return + else: + logger.info(f"Changed file at {to_path}") else: - logger.info(f"New file at {to_path}.") + logger.info(f"New file at {to_path}") # copy the file from from_path to sync_dir/to_path to_path.parent.mkdir(parents=True, exist_ok=True) from_path.replace(to_path) logger.debug(f"Moved {from_path} to {to_path}") - # remember path for later reference - self._added_files.add(to_path.resolve()) - def clean_sync_dir(self): self._clean_dir(self._sync_dir, remove_parent=False) logger.debug(f"Cleaned sync dir: {self._sync_dir}") def _clean_dir(self, path, remove_parent=True): - for child in path.iterdir(): + for child in sorted(path.iterdir()): if child.is_dir(): self._clean_dir(child, remove_parent=True) elif child.resolve() not in self._added_files: