Don't overwrite files if the contents match

This commit is contained in:
Joscha 2018-11-26 13:37:01 +00:00
parent 2034c9d426
commit 529c4a7dda

View File

@ -50,26 +50,30 @@ class Organizer:
# check if sync_dir/to_path is inside sync_dir? # check if sync_dir/to_path is inside sync_dir?
to_path = pathlib.Path(self._sync_dir, to_path) 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 to_path.exists():
if not filecmp.cmp(from_path, to_path, shallow=False): if filecmp.cmp(from_path, to_path, shallow=False):
logger.info(f"Changed file at {to_path}.") 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: 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 # copy the file from from_path to sync_dir/to_path
to_path.parent.mkdir(parents=True, exist_ok=True) to_path.parent.mkdir(parents=True, exist_ok=True)
from_path.replace(to_path) from_path.replace(to_path)
logger.debug(f"Moved {from_path} to {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): def clean_sync_dir(self):
self._clean_dir(self._sync_dir, remove_parent=False) self._clean_dir(self._sync_dir, remove_parent=False)
logger.debug(f"Cleaned sync dir: {self._sync_dir}") logger.debug(f"Cleaned sync dir: {self._sync_dir}")
def _clean_dir(self, path, remove_parent=True): def _clean_dir(self, path, remove_parent=True):
for child in path.iterdir(): for child in sorted(path.iterdir()):
if child.is_dir(): if child.is_dir():
self._clean_dir(child, remove_parent=True) self._clean_dir(child, remove_parent=True)
elif child.resolve() not in self._added_files: elif child.resolve() not in self._added_files: