mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-09 22:14:17 +00:00
refactoring
This commit is contained in:
@@ -13,8 +13,9 @@ import fnmatch
|
|||||||
# local imports
|
# local imports
|
||||||
from dotdrop.logger import Logger
|
from dotdrop.logger import Logger
|
||||||
from dotdrop.templategen import Templategen
|
from dotdrop.templategen import Templategen
|
||||||
from dotdrop.utils import patch_ignores, removepath, get_unique_tmp_name, \
|
from dotdrop.utils import ignores_to_absolute, removepath, \
|
||||||
write_to_tmpfile, must_ignore, mirror_file_rights, get_file_perm
|
get_unique_tmp_name, write_to_tmpfile, must_ignore, \
|
||||||
|
mirror_file_rights, get_file_perm
|
||||||
from dotdrop.exceptions import UndefinedException
|
from dotdrop.exceptions import UndefinedException
|
||||||
|
|
||||||
|
|
||||||
@@ -47,7 +48,6 @@ class Updater:
|
|||||||
self.safe = safe
|
self.safe = safe
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self.ignore = ignore or []
|
self.ignore = ignore or []
|
||||||
self.ignores = None
|
|
||||||
self.showpatch = showpatch
|
self.showpatch = showpatch
|
||||||
self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop
|
self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop
|
||||||
self.templater = Templategen(variables=self.variables,
|
self.templater = Templategen(variables=self.variables,
|
||||||
@@ -96,8 +96,9 @@ class Updater:
|
|||||||
ret = False
|
ret = False
|
||||||
new_path = None
|
new_path = None
|
||||||
ignores = list(set(self.ignore + dotfile.upignore))
|
ignores = list(set(self.ignore + dotfile.upignore))
|
||||||
self.ignores = patch_ignores(ignores, dotfile.dst, debug=self.debug)
|
ignores = ignores_to_absolute(ignores, dotfile.dst,
|
||||||
self.log.dbg(f'ignore pattern(s): {self.ignores}')
|
debug=self.debug)
|
||||||
|
self.log.dbg(f'ignore pattern(s) for {path}: {ignores}')
|
||||||
|
|
||||||
deployed_path = os.path.expanduser(path)
|
deployed_path = os.path.expanduser(path)
|
||||||
local_path = os.path.join(self.dotpath, dotfile.src)
|
local_path = os.path.join(self.dotpath, dotfile.src)
|
||||||
@@ -115,8 +116,9 @@ class Updater:
|
|||||||
|
|
||||||
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
|
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
|
||||||
dotfile.ignore_missing_in_dotdrop
|
dotfile.ignore_missing_in_dotdrop
|
||||||
|
|
||||||
if (ignore_missing_in_dotdrop and not os.path.exists(local_path)) or \
|
if (ignore_missing_in_dotdrop and not os.path.exists(local_path)) or \
|
||||||
self._ignore([deployed_path, local_path]):
|
self._must_ignore([deployed_path, local_path], ignores):
|
||||||
self.log.sub(f'\"{dotfile.key}\" ignored')
|
self.log.sub(f'\"{dotfile.key}\" ignored')
|
||||||
return True
|
return True
|
||||||
# apply write transformation if any
|
# apply write transformation if any
|
||||||
@@ -130,12 +132,14 @@ class Updater:
|
|||||||
|
|
||||||
# handle the pointed file
|
# handle the pointed file
|
||||||
if os.path.isdir(new_path):
|
if os.path.isdir(new_path):
|
||||||
ret = self._handle_dir(new_path, local_path, dotfile)
|
ret = self._handle_dir(new_path, local_path,
|
||||||
|
dotfile, ignores)
|
||||||
else:
|
else:
|
||||||
ret = self._handle_file(new_path, local_path)
|
ret = self._handle_file(new_path, local_path,
|
||||||
|
ignores)
|
||||||
|
|
||||||
|
# mirror rights
|
||||||
if deployed_mode != local_mode:
|
if deployed_mode != local_mode:
|
||||||
# mirror rights
|
|
||||||
msg = f'adopt mode {deployed_mode:o} for {dotfile.key}'
|
msg = f'adopt mode {deployed_mode:o} for {dotfile.key}'
|
||||||
self.log.dbg(msg)
|
self.log.dbg(msg)
|
||||||
if self.conf.update_dotfile(dotfile.key, deployed_mode):
|
if self.conf.update_dotfile(dotfile.key, deployed_mode):
|
||||||
@@ -165,9 +169,9 @@ class Updater:
|
|||||||
return None
|
return None
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
def _is_template(self, path):
|
def _is_template(self, path, ignores):
|
||||||
if not Templategen.path_is_template(path,
|
if not Templategen.path_is_template(path,
|
||||||
ignore=self.ignores,
|
ignore=ignores,
|
||||||
debug=self.debug):
|
debug=self.debug):
|
||||||
self.log.dbg(f'{path} is NO template')
|
self.log.dbg(f'{path} is NO template')
|
||||||
return False
|
return False
|
||||||
@@ -199,7 +203,7 @@ class Updater:
|
|||||||
self.log.err(exc)
|
self.log.err(exc)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _mirror_rights(self, src, dst):
|
def _mirror_file_perms(self, src, dst):
|
||||||
srcr = get_file_perm(src)
|
srcr = get_file_perm(src)
|
||||||
dstr = get_file_perm(dst)
|
dstr = get_file_perm(dst)
|
||||||
if srcr == dstr:
|
if srcr == dstr:
|
||||||
@@ -211,13 +215,14 @@ class Updater:
|
|||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
self.log.err(exc)
|
self.log.err(exc)
|
||||||
|
|
||||||
def _handle_file(self, deployed_path, local_path, compare=True):
|
def _handle_file(self, deployed_path, local_path,
|
||||||
|
ignores, compare=True):
|
||||||
"""sync path (deployed file) and local_path (dotdrop dotfile path)"""
|
"""sync path (deployed file) and local_path (dotdrop dotfile path)"""
|
||||||
if self._ignore([deployed_path, local_path]):
|
if self._must_ignore([deployed_path, local_path], ignores):
|
||||||
self.log.sub(f'\"{local_path}\" ignored')
|
self.log.sub(f'\"{local_path}\" ignored')
|
||||||
return True
|
return True
|
||||||
self.log.dbg(f'update for file {deployed_path} and {local_path}')
|
self.log.dbg(f'update for file {deployed_path} and {local_path}')
|
||||||
if self._is_template(local_path):
|
if self._is_template(local_path, ignores):
|
||||||
# dotfile is a template
|
# dotfile is a template
|
||||||
self.log.dbg(f'{local_path} is a template')
|
self.log.dbg(f'{local_path} is a template')
|
||||||
if self.showpatch:
|
if self.showpatch:
|
||||||
@@ -241,32 +246,35 @@ class Updater:
|
|||||||
else:
|
else:
|
||||||
self.log.dbg(f'cp {deployed_path} {local_path}')
|
self.log.dbg(f'cp {deployed_path} {local_path}')
|
||||||
shutil.copyfile(deployed_path, local_path)
|
shutil.copyfile(deployed_path, local_path)
|
||||||
self._mirror_rights(deployed_path, local_path)
|
self._mirror_file_perms(deployed_path, local_path)
|
||||||
self.log.sub(f'\"{local_path}\" updated')
|
self.log.sub(f'\"{local_path}\" updated')
|
||||||
except IOError as exc:
|
except IOError as exc:
|
||||||
self.log.warn(f'{deployed_path} update failed, do manually: {exc}')
|
self.log.warn(f'{deployed_path} update failed, do manually: {exc}')
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _handle_dir(self, deployed_path, local_path, dotfile):
|
def _handle_dir(self, deployed_path, local_path,
|
||||||
|
dotfile, ignores):
|
||||||
"""sync path (local dir) and local_path (dotdrop dir path)"""
|
"""sync path (local dir) and local_path (dotdrop dir path)"""
|
||||||
self.log.dbg(f'handle update for dir {deployed_path} to {local_path}')
|
self.log.dbg(f'handle update for dir {deployed_path} to {local_path}')
|
||||||
# paths must be absolute (no tildes)
|
# paths must be absolute (no tildes)
|
||||||
deployed_path = os.path.expanduser(deployed_path)
|
deployed_path = os.path.expanduser(deployed_path)
|
||||||
local_path = os.path.expanduser(local_path)
|
local_path = os.path.expanduser(local_path)
|
||||||
|
|
||||||
if self._ignore([deployed_path, local_path]):
|
if self._must_ignore([deployed_path, local_path], ignores):
|
||||||
self.log.sub(f'\"{local_path}\" ignored')
|
self.log.sub(f'\"{local_path}\" ignored')
|
||||||
return True
|
return True
|
||||||
# find the differences
|
# find the differences
|
||||||
diff = filecmp.dircmp(deployed_path, local_path, ignore=None)
|
diff = filecmp.dircmp(deployed_path, local_path, ignore=None)
|
||||||
# handle directories diff
|
# handle directories diff
|
||||||
ret = self._merge_dirs(diff, dotfile)
|
ret = self._merge_dirs(diff, dotfile,
|
||||||
self._mirror_rights(deployed_path, local_path)
|
ignores)
|
||||||
|
self._mirror_file_perms(deployed_path, local_path)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _merge_dirs_create_left_only(self, diff, left, right,
|
def _merge_dirs_create_left_only(self, diff, left, right,
|
||||||
ignore_missing_in_dotdrop):
|
ignore_missing_in_dotdrop,
|
||||||
|
ignores):
|
||||||
"""create dirs that don't exist in dotdrop"""
|
"""create dirs that don't exist in dotdrop"""
|
||||||
for toadd in diff.left_only:
|
for toadd in diff.left_only:
|
||||||
exist = os.path.join(left, toadd)
|
exist = os.path.join(left, toadd)
|
||||||
@@ -276,7 +284,7 @@ class Updater:
|
|||||||
# match to dotdrop dotpath
|
# match to dotdrop dotpath
|
||||||
new = os.path.join(right, toadd)
|
new = os.path.join(right, toadd)
|
||||||
if (ignore_missing_in_dotdrop and not os.path.exists(new)) or \
|
if (ignore_missing_in_dotdrop and not os.path.exists(new)) or \
|
||||||
self._ignore([exist, new]):
|
self._must_ignore([exist, new], ignores):
|
||||||
self.log.sub(f'\"{exist}\" ignored')
|
self.log.sub(f'\"{exist}\" ignored')
|
||||||
continue
|
continue
|
||||||
if self.dry:
|
if self.dry:
|
||||||
@@ -287,7 +295,7 @@ class Updater:
|
|||||||
# Newly created directory should be copied as is (for efficiency).
|
# Newly created directory should be copied as is (for efficiency).
|
||||||
def ign(src, names):
|
def ign(src, names):
|
||||||
whitelist, blacklist = set(), set()
|
whitelist, blacklist = set(), set()
|
||||||
for ignore in self.ignores:
|
for ignore in ignores:
|
||||||
for name in names:
|
for name in names:
|
||||||
path = os.path.join(src, name)
|
path = os.path.join(src, name)
|
||||||
if ignore.startswith('!') and \
|
if ignore.startswith('!') and \
|
||||||
@@ -308,14 +316,15 @@ class Updater:
|
|||||||
self.log.sub(f'\"{new}\" dir added')
|
self.log.sub(f'\"{new}\" dir added')
|
||||||
|
|
||||||
def _merge_dirs_remove_right_only(self, diff, left, right,
|
def _merge_dirs_remove_right_only(self, diff, left, right,
|
||||||
ignore_missing_in_dotdrop):
|
ignore_missing_in_dotdrop,
|
||||||
|
ignores):
|
||||||
"""remove dirs that don't exist in deployed version"""
|
"""remove dirs that don't exist in deployed version"""
|
||||||
for toremove in diff.right_only:
|
for toremove in diff.right_only:
|
||||||
old = os.path.join(right, toremove)
|
old = os.path.join(right, toremove)
|
||||||
if not os.path.isdir(old):
|
if not os.path.isdir(old):
|
||||||
# ignore files for now
|
# ignore files for now
|
||||||
continue
|
continue
|
||||||
if self._ignore([old]):
|
if self._must_ignore([old], ignores):
|
||||||
continue
|
continue
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry(f'would rm -r {old}')
|
self.log.dry(f'would rm -r {old}')
|
||||||
@@ -335,16 +344,19 @@ class Updater:
|
|||||||
fleft = os.path.join(left, file)
|
fleft = os.path.join(left, file)
|
||||||
fright = os.path.join(right, file)
|
fright = os.path.join(right, file)
|
||||||
if (ignore_missing_in_dotdrop and not os.path.exists(fright)) or \
|
if (ignore_missing_in_dotdrop and not os.path.exists(fright)) or \
|
||||||
self._ignore([fleft, fright]):
|
self._must_ignore([fleft, fright], ignores):
|
||||||
continue
|
continue
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry(f'would cp {fleft} {fright}')
|
self.log.dry(f'would cp {fleft} {fright}')
|
||||||
continue
|
continue
|
||||||
self.log.dbg(f'cp {fleft} {fright}')
|
self.log.dbg(f'cp {fleft} {fright}')
|
||||||
self._handle_file(fleft, fright, compare=False)
|
self._handle_file(fleft, fright,
|
||||||
|
ignores,
|
||||||
|
compare=False)
|
||||||
|
|
||||||
def _merge_dirs_copy_left_only(self, diff, left, right,
|
def _merge_dirs_copy_left_only(self, diff, left, right,
|
||||||
ignore_missing_in_dotdrop):
|
ignore_missing_in_dotdrop,
|
||||||
|
ignores):
|
||||||
"""copy files that don't exist in dotdrop"""
|
"""copy files that don't exist in dotdrop"""
|
||||||
for toadd in diff.left_only:
|
for toadd in diff.left_only:
|
||||||
exist = os.path.join(left, toadd)
|
exist = os.path.join(left, toadd)
|
||||||
@@ -353,7 +365,7 @@ class Updater:
|
|||||||
continue
|
continue
|
||||||
new = os.path.join(right, toadd)
|
new = os.path.join(right, toadd)
|
||||||
if (ignore_missing_in_dotdrop and not os.path.exists(new)) or \
|
if (ignore_missing_in_dotdrop and not os.path.exists(new)) or \
|
||||||
self._ignore([exist, new]):
|
self._must_ignore([exist, new], ignores):
|
||||||
continue
|
continue
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry(f'would cp {exist} {new}')
|
self.log.dry(f'would cp {exist} {new}')
|
||||||
@@ -366,10 +378,10 @@ class Updater:
|
|||||||
self.log.err(f'{msg}: {exc}')
|
self.log.err(f'{msg}: {exc}')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self._mirror_rights(exist, new)
|
self._mirror_file_perms(exist, new)
|
||||||
self.log.sub(f'\"{new}\" added')
|
self.log.sub(f'\"{new}\" added')
|
||||||
|
|
||||||
def _merge_dirs_remove_right_only_2(self, diff, right):
|
def _merge_dirs_remove_right_only_2(self, diff, right, ignores):
|
||||||
"""remove files that don't exist in deployed version"""
|
"""remove files that don't exist in deployed version"""
|
||||||
for toremove in diff.right_only:
|
for toremove in diff.right_only:
|
||||||
new = os.path.join(right, toremove)
|
new = os.path.join(right, toremove)
|
||||||
@@ -378,7 +390,7 @@ class Updater:
|
|||||||
if os.path.isdir(new):
|
if os.path.isdir(new):
|
||||||
# ignore dirs, done above
|
# ignore dirs, done above
|
||||||
continue
|
continue
|
||||||
if self._ignore([new]):
|
if self._must_ignore([new], ignores):
|
||||||
continue
|
continue
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry(f'would rm {new}')
|
self.log.dry(f'would rm {new}')
|
||||||
@@ -387,34 +399,38 @@ class Updater:
|
|||||||
removepath(new, logger=self.log)
|
removepath(new, logger=self.log)
|
||||||
self.log.sub(f'\"{new}\" removed')
|
self.log.sub(f'\"{new}\" removed')
|
||||||
|
|
||||||
def _merge_dirs(self, diff, dotfile):
|
def _merge_dirs(self, diff, dotfile, ignores):
|
||||||
"""Synchronize directories recursively."""
|
"""Synchronize directories recursively."""
|
||||||
left, right = diff.left, diff.right
|
left, right = diff.left, diff.right
|
||||||
self.log.dbg(f'sync dir {left} to {right}')
|
self.log.dbg(f'sync dir {left} to {right}')
|
||||||
if self._ignore([left, right]):
|
if self._must_ignore([left, right], ignores):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
|
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
|
||||||
dotfile.ignore_missing_in_dotdrop
|
dotfile.ignore_missing_in_dotdrop
|
||||||
|
|
||||||
self._merge_dirs_create_left_only(diff, left, right,
|
self._merge_dirs_create_left_only(diff, left, right,
|
||||||
ignore_missing_in_dotdrop)
|
ignore_missing_in_dotdrop,
|
||||||
|
ignores)
|
||||||
self._merge_dirs_remove_right_only(diff, left, right,
|
self._merge_dirs_remove_right_only(diff, left, right,
|
||||||
ignore_missing_in_dotdrop)
|
ignore_missing_in_dotdrop,
|
||||||
|
ignores)
|
||||||
self._merge_dirs_copy_left_only(diff, left, right,
|
self._merge_dirs_copy_left_only(diff, left, right,
|
||||||
ignore_missing_in_dotdrop)
|
ignore_missing_in_dotdrop,
|
||||||
self._merge_dirs_remove_right_only_2(diff, right)
|
ignores)
|
||||||
|
self._merge_dirs_remove_right_only_2(diff, right, ignores)
|
||||||
|
|
||||||
# compare rights
|
# compare rights
|
||||||
for common in diff.common_files:
|
for common in diff.common_files:
|
||||||
leftf = os.path.join(left, common)
|
leftf = os.path.join(left, common)
|
||||||
rightf = os.path.join(right, common)
|
rightf = os.path.join(right, common)
|
||||||
if not self._same_rights(leftf, rightf):
|
if not self._same_rights(leftf, rightf):
|
||||||
self._mirror_rights(leftf, rightf)
|
self._mirror_file_perms(leftf, rightf)
|
||||||
|
|
||||||
# Recursively decent into common subdirectories.
|
# Recursively decent into common subdirectories.
|
||||||
for subdir in diff.subdirs.values():
|
for subdir in diff.subdirs.values():
|
||||||
self._merge_dirs(subdir, dotfile)
|
self._merge_dirs(subdir, dotfile,
|
||||||
|
ignores)
|
||||||
|
|
||||||
# Nothing more to do here.
|
# Nothing more to do here.
|
||||||
return True
|
return True
|
||||||
@@ -433,8 +449,8 @@ class Updater:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _ignore(self, paths):
|
def _must_ignore(self, paths, ignores):
|
||||||
if must_ignore(paths, self.ignores, debug=self.debug):
|
if must_ignore(paths, ignores, debug=self.debug):
|
||||||
self.log.dbg(f'ignoring update for {paths}')
|
self.log.dbg(f'ignoring update for {paths}')
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user