1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 17:49:01 +00:00
This commit is contained in:
deadc0de6
2021-04-29 15:49:56 +02:00
parent 54c6aa7bb7
commit 4f19dc790c

View File

@@ -22,10 +22,11 @@ TILD = '~'
class Updater: class Updater:
"""dotfiles updater"""
def __init__(self, dotpath, variables, conf, def __init__(self, dotpath, variables, conf,
dry=False, safe=True, debug=False, dry=False, safe=True, debug=False,
ignore=[], showpatch=False, ignore=None, showpatch=False,
ignore_missing_in_dotdrop=False): ignore_missing_in_dotdrop=False):
"""constructor """constructor
@dotpath: path where dotfiles are stored @dotpath: path where dotfiles are stored
@@ -43,7 +44,7 @@ class Updater:
self.dry = dry self.dry = dry
self.safe = safe self.safe = safe
self.debug = debug self.debug = debug
self.ignore = ignore self.ignore = ignore or []
self.ignores = None 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
@@ -125,14 +126,13 @@ class Updater:
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)
else: else:
ret = self._handle_file(new_path, local_path, dotfile) ret = self._handle_file(new_path, local_path)
if deployed_mode != local_mode: if deployed_mode != local_mode:
# mirror rights # mirror rights
m = 'adopt mode {:o} for {}' msg = 'adopt mode {:o} for {}'
self.log.dbg(m.format(deployed_mode, dotfile.key)) self.log.dbg(msg.format(deployed_mode, dotfile.key))
r = self.conf.update_dotfile(dotfile.key, deployed_mode) if self.conf.update_dotfile(dotfile.key, deployed_mode):
if r:
ret = True ret = True
# clean temporary files # clean temporary files
@@ -186,8 +186,8 @@ class Updater:
lefts = get_file_perm(left) lefts = get_file_perm(left)
rights = get_file_perm(right) rights = get_file_perm(right)
return lefts == rights return lefts == rights
except OSError as e: except OSError as exc:
self.log.err(e) self.log.err(exc)
return False return False
def _mirror_rights(self, src, dst): def _mirror_rights(self, src, dst):
@@ -199,10 +199,10 @@ class Updater:
self.log.dbg(msg.format(src, srcr, dst, dstr)) self.log.dbg(msg.format(src, srcr, dst, dstr))
try: try:
mirror_file_rights(src, dst) mirror_file_rights(src, dst)
except OSError as e: except OSError as exc:
self.log.err(e) self.log.err(exc)
def _handle_file(self, deployed_path, local_path, dotfile, compare=True): def _handle_file(self, deployed_path, local_path, 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._ignore([deployed_path, local_path]):
self.log.sub('\"{}\" ignored'.format(local_path)) self.log.sub('\"{}\" ignored'.format(local_path))
@@ -217,10 +217,10 @@ class Updater:
if self.showpatch: if self.showpatch:
try: try:
self._show_patch(deployed_path, local_path) self._show_patch(deployed_path, local_path)
except UndefinedException as e: except UndefinedException as exc:
msg = 'unable to show patch for {}: {}'.format( msg = 'unable to show patch for {}: {}'.format(
deployed_path, deployed_path,
e, exc,
) )
self.log.warn(msg) self.log.warn(msg)
return False return False
@@ -246,10 +246,10 @@ class Updater:
shutil.copyfile(deployed_path, local_path) shutil.copyfile(deployed_path, local_path)
self._mirror_rights(deployed_path, local_path) self._mirror_rights(deployed_path, local_path)
self.log.sub('\"{}\" updated'.format(local_path)) self.log.sub('\"{}\" updated'.format(local_path))
except IOError as e: except IOError as exc:
self.log.warn('{} update failed, do manually: {}'.format( self.log.warn('{} update failed, do manually: {}'.format(
deployed_path, deployed_path,
e exc
)) ))
return False return False
return True return True
@@ -274,17 +274,9 @@ class Updater:
self._mirror_rights(deployed_path, local_path) self._mirror_rights(deployed_path, local_path)
return ret return ret
def _merge_dirs(self, diff, dotfile): def _merge_dirs_create_left_only(self, diff, left, right,
"""Synchronize directories recursively.""" ignore_missing_in_dotdrop):
left, right = diff.left, diff.right """create dirs that don't exist in dotdrop"""
self.log.dbg('sync dir {} to {}'.format(left, right))
if self._ignore([left, right]):
return True
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
dotfile.ignore_missing_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)
if not os.path.isdir(exist): if not os.path.isdir(exist):
@@ -302,7 +294,7 @@ class Updater:
self.log.dbg('cp -r {} {}'.format(exist, new)) self.log.dbg('cp -r {} {}'.format(exist, new))
# Newly created directory should be copied as is (for efficiency). # Newly created directory should be copied as is (for efficiency).
def ig(src, names): def ign(src, names):
whitelist, blacklist = set(), set() whitelist, blacklist = set(), set()
for ignore in self.ignores: for ignore in self.ignores:
for name in names: for name in names:
@@ -317,14 +309,16 @@ class Updater:
return blacklist - whitelist return blacklist - whitelist
try: try:
shutil.copytree(exist, new, ignore=ig) shutil.copytree(exist, new, ignore=ign)
except OSError as e: except OSError as exc:
msg = 'error copying dir {}'.format(exist) msg = 'error copying dir {}'.format(exist)
self.log.err('{}: {}'.format(msg, e)) self.log.err('{}: {}'.format(msg, exc))
continue continue
self.log.sub('\"{}\" dir added'.format(new)) self.log.sub('\"{}\" dir added'.format(new))
# remove dirs that don't exist in deployed version def _merge_dirs_remove_right_only(self, diff, left, right,
ignore_missing_in_dotdrop):
"""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):
@@ -346,9 +340,9 @@ class Updater:
fdiff = diff.diff_files fdiff = diff.diff_files
fdiff.extend(diff.funny_files) fdiff.extend(diff.funny_files)
fdiff.extend(diff.common_funny) fdiff.extend(diff.common_funny)
for f in fdiff: for file in fdiff:
fleft = os.path.join(left, f) fleft = os.path.join(left, file)
fright = os.path.join(right, f) 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._ignore([fleft, fright]):
continue continue
@@ -356,9 +350,11 @@ class Updater:
self.log.dry('would cp {} {}'.format(fleft, fright)) self.log.dry('would cp {} {}'.format(fleft, fright))
continue continue
self.log.dbg('cp {} {}'.format(fleft, fright)) self.log.dbg('cp {} {}'.format(fleft, fright))
self._handle_file(fleft, fright, dotfile, compare=False) self._handle_file(fleft, fright, compare=False)
# copy files that don't exist in dotdrop def _merge_dirs_copy_left_only(self, diff, left, right,
ignore_missing_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)
if os.path.isdir(exist): if os.path.isdir(exist):
@@ -374,15 +370,16 @@ class Updater:
self.log.dbg('cp {} {}'.format(exist, new)) self.log.dbg('cp {} {}'.format(exist, new))
try: try:
shutil.copyfile(exist, new) shutil.copyfile(exist, new)
except OSError as e: except OSError as exc:
msg = 'error copying file {}'.format(exist) msg = 'error copying file {}'.format(exist)
self.log.err('{}: {}'.format(msg, e)) self.log.err('{}: {}'.format(msg, exc))
continue continue
self._mirror_rights(exist, new) self._mirror_rights(exist, new)
self.log.sub('\"{}\" added'.format(new)) self.log.sub('\"{}\" added'.format(new))
# remove files that don't exist in deployed version def _merge_dirs_remove_right_only_2(self, diff, right):
"""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)
if not os.path.exists(new): if not os.path.exists(new):
@@ -399,6 +396,24 @@ class Updater:
removepath(new, logger=self.log) removepath(new, logger=self.log)
self.log.sub('\"{}\" removed'.format(new)) self.log.sub('\"{}\" removed'.format(new))
def _merge_dirs(self, diff, dotfile):
"""Synchronize directories recursively."""
left, right = diff.left, diff.right
self.log.dbg('sync dir {} to {}'.format(left, right))
if self._ignore([left, right]):
return True
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
dotfile.ignore_missing_in_dotdrop
self._merge_dirs_create_left_only(diff, left, right,
ignore_missing_in_dotdrop)
self._merge_dirs_remove_right_only(diff, left, right,
ignore_missing_in_dotdrop)
self._merge_dirs_copy_left_only(diff, left, right,
ignore_missing_in_dotdrop)
self._merge_dirs_remove_right_only_2(diff, right)
# 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)