1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 14:31:46 +00:00
This commit is contained in:
deadc0de6
2024-01-31 22:19:13 +01:00
committed by deadc0de
parent cee443d518
commit d91282ecb9

View File

@@ -15,8 +15,7 @@ from dotdrop.ftree import FTreeDir
from dotdrop.templategen import Templategen
from dotdrop.utils import ignores_to_absolute, removepath, \
get_unique_tmp_name, write_to_tmpfile, must_ignore, \
mirror_file_rights, get_file_perm, copytree_with_ign, \
diff
mirror_file_rights, get_file_perm, diff
from dotdrop.exceptions import UndefinedException
@@ -337,186 +336,6 @@ class Updater:
return True
def _handle_dir2(self, deployed_path, local_path,
dotfile, ignores):
"""sync path (local dir) and local_path (dotdrop dir path)"""
self.log.dbg(f'handle update for dir {deployed_path} to {local_path}')
# paths must be absolute (no tildes)
deployed_path = os.path.expanduser(deployed_path)
local_path = os.path.expanduser(local_path)
# find the differences
cmp = filecmp.dircmp(deployed_path, local_path)
# handle directories diff
ret = self._merge_dirs(cmp, dotfile,
ignores)
self._mirror_file_perms(deployed_path, local_path)
return ret
def _merge_dirs_create_left_only(self, cmp, left, right,
ignore_missing_in_dotdrop,
ignores):
"""create dirs that don't exist in dotdrop"""
self.log.dbg(f'_merge_dirs_create_left_only: {cmp.left_only}')
for toadd in cmp.left_only:
exist = os.path.join(left, toadd)
if not os.path.isdir(exist):
# ignore files for now
continue
exist += os.path.sep
# match to dotdrop dotpath
new = os.path.join(right, toadd)
if ignore_missing_in_dotdrop and not os.path.exists(new):
self.log.sub(f'\"{exist}\" ignored')
continue
if self.dry:
self.log.dry(f'would cp -r {exist} {new}')
continue
self.log.dbg(f'cp -r {exist} {new}')
cpied_cnt = 0
try:
ign_func = self._ignore(ignores)
cpied_cnt = copytree_with_ign(exist, new,
ignore_func=ign_func,
debug=self.debug)
except OSError as exc:
msg = f'error copying dir {exist}'
self.log.err(f'{msg}: {exc}')
continue
if cpied_cnt:
self.log.sub(f'\"{new}\" dir added')
def _ignore(self, ignores):
def ignore_func(path):
return must_ignore([path], ignores,
debug=self.debug)
return ignore_func
def _merge_dirs_remove_right_only(self, cmp, left, right,
ignore_missing_in_dotdrop,
ignores):
"""remove dirs that don't exist in deployed version"""
self.log.dbg(f'_merge_dirs_remove_right_only: {cmp.right_only}')
for toremove in cmp.right_only:
old = os.path.join(right, toremove)
if not os.path.isdir(old):
# ignore files for now
continue
old += os.path.sep
if self._must_ignore([old], ignores):
continue
if self.dry:
self.log.dry(f'would rm -r {old}')
continue
self.log.dbg(f'rm -r {old}')
if not self._confirm_rm_r(old):
continue
removepath(old, logger=self.log)
self.log.sub(f'\"{old}\" dir removed')
# handle files diff
# sync files that exist in both but are different
self.log.dbg(f'_merge_dirs_remove_right_only: {cmp.diff_files}')
fdiff = cmp.diff_files
fdiff.extend(cmp.funny_files)
fdiff.extend(cmp.common_funny)
for file in fdiff:
fleft = os.path.join(left, file)
fright = os.path.join(right, file)
if (ignore_missing_in_dotdrop and not os.path.exists(fright)) or \
self._must_ignore([fleft, fright], ignores):
continue
if self.dry:
self.log.dry(f'would cp {fleft} {fright}')
continue
self.log.dbg(f'cp {fleft} {fright}')
self._handle_file(fleft, fright,
ignores,
compare=False)
def _merge_files_copy_left_only(self, cmp, left, right,
ignore_missing_in_dotdrop,
ignores):
"""copy files that don't exist in dotdrop"""
self.log.dbg(f'_merge_dirs_copy_left_only: {cmp.left_only}')
for toadd in cmp.left_only:
exist = os.path.join(left, toadd)
if os.path.isdir(exist):
# ignore dirs, done above
continue
new = os.path.join(right, toadd)
if (ignore_missing_in_dotdrop and not os.path.exists(new)) or \
self._must_ignore([exist, new], ignores):
continue
if self.dry:
self.log.dry(f'would cp {exist} {new}')
continue
self.log.dbg(f'cp {exist} {new}')
try:
shutil.copyfile(exist, new)
except OSError as exc:
msg = f'error copying file {exist}'
self.log.err(f'{msg}: {exc}')
continue
self._mirror_file_perms(exist, new)
self.log.sub(f'\"{new}\" added')
def _merge_files_remove_right_only(self, cmp, right, ignores):
"""remove files that don't exist in deployed version"""
self.log.dbg(f'_merge_dirs_remove_right_only_2: {cmp.right_only}')
for toremove in cmp.right_only:
new = os.path.join(right, toremove)
if not os.path.exists(new):
continue
if os.path.isdir(new):
# ignore dirs, done above
continue
if self._must_ignore([new], ignores):
continue
if self.dry:
self.log.dry(f'would rm {new}')
continue
self.log.dbg(f'rm {new}')
removepath(new, logger=self.log)
self.log.sub(f'\"{new}\" removed')
def _merge_dirs(self, cmp, dotfile, ignores):
"""Synchronize directories recursively."""
left, right = cmp.left, cmp.right
self.log.dbg(f'sync dir {left} to {right}')
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
dotfile.ignore_missing_in_dotdrop
# directories
self._merge_dirs_create_left_only(cmp, left, right,
ignore_missing_in_dotdrop,
ignores)
self._merge_dirs_remove_right_only(cmp, left, right,
ignore_missing_in_dotdrop,
ignores)
# files
self._merge_files_copy_left_only(cmp, left, right,
ignore_missing_in_dotdrop,
ignores)
self._merge_files_remove_right_only(cmp, right, ignores)
# compare rights
for common in cmp.common_files:
leftf = os.path.join(left, common)
rightf = os.path.join(right, common)
if not self._same_rights(leftf, rightf):
self._mirror_file_perms(leftf, rightf)
# Recursively decent into common subdirectories.
for subdir in cmp.subdirs.values():
self._merge_dirs(subdir, dotfile,
ignores)
# Nothing more to do here.
return True
def _overwrite(self, src, dst):
"""ask for overwritting"""
msg = f'Overwrite \"{dst}\" with \"{src}\"?'