1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-07 17:24:17 +00:00

adding ability to update by key or on all keys of a specific profile

This commit is contained in:
deadc0de6
2018-10-08 21:36:12 +02:00
parent b71cbefa10
commit 2b901d5608
5 changed files with 211 additions and 30 deletions

View File

@@ -19,32 +19,45 @@ TILD = '~'
class Updater:
def __init__(self, conf, dotpath, dry, safe, debug):
def __init__(self, conf, dotpath, dry, safe,
iskey=False, debug=False):
self.home = os.path.expanduser(TILD)
self.conf = conf
self.dotpath = dotpath
self.dry = dry
self.safe = safe
self.iskey = iskey
self.debug = debug
self.log = Logger()
def update(self, path, profile):
def update_path(self, path, profile):
"""update the dotfile installed on path"""
if not os.path.lexists(path):
self.log.err('\"{}\" does not exist!'.format(path))
return False
left = self._normalize(path)
dotfile = self._get_dotfile(left, profile)
path = self._normalize(path)
dotfile = self._get_dotfile_by_path(path, profile)
if not dotfile:
return False
if self.debug:
self.log.dbg('updating {} from {}'.format(dotfile, path))
self.log.dbg('updating {} from path \"{}\"'.format(dotfile, path))
return self._update(path, dotfile)
def update_key(self, key, profile):
"""update the dotfile referenced by key"""
dotfile = self._get_dotfile_by_key(key, profile)
if not dotfile:
return False
if self.debug:
self.log.dbg('updating {} from key \"{}\"'.format(dotfile, key))
path = self._normalize(dotfile.dst)
return self._update(path, dotfile)
def _update(self, path, dotfile):
"""update dotfile from file pointed by path"""
left = os.path.expanduser(path)
right = os.path.join(self.conf.abs_dotpath(self.dotpath), dotfile.src)
# expands user
left = os.path.expanduser(left)
right = os.path.expanduser(right)
# go through all files and update
if os.path.isdir(path):
return self._handle_dir(left, right)
return self._handle_file(left, right)
@@ -60,7 +73,20 @@ class Updater:
path = os.path.join(TILD, path)
return path
def _get_dotfile(self, path, profile):
def _get_dotfile_by_key(self, key, profile):
"""get the dotfile matching this key"""
dotfiles = self.conf.get_dotfiles(profile)
subs = [d for d in dotfiles if d.key == key]
if not subs:
self.log.err('key \"{}\" not found!'.format(path))
return None
if len(subs) > 1:
found = ','.join([d.src for d in dotfiles])
self.log.err('multiple dotfiles found: {}'.format(found))
return None
return subs[0]
def _get_dotfile_by_path(self, path, profile):
"""get the dotfile matching this path"""
dotfiles = self.conf.get_dotfiles(profile)
subs = [d for d in dotfiles if d.dst == path]
@@ -114,7 +140,7 @@ class Updater:
# find the differences
diff = filecmp.dircmp(left, right, ignore=None)
# handle directories diff
self._merge_dirs(diff)
return self._merge_dirs(diff)
def _merge_dirs(self, diff):
"""Synchronize directories recursively."""
@@ -123,8 +149,6 @@ class Updater:
self.log.dbg('sync dir {} to {}'.format(left, right))
# create dirs that don't exist in dotdrop
if self.debug:
self.log.dbg('handle dirs that do not exist in dotdrop')
for toadd in diff.left_only:
exist = os.path.join(left, toadd)
if not os.path.isdir(exist):
@@ -141,8 +165,6 @@ class Updater:
shutil.copytree(exist, new)
# remove dirs that don't exist in deployed version
if self.debug:
self.log.dbg('remove dirs that do not exist in deployed version')
for toremove in diff.right_only:
old = os.path.join(right, toremove)
if not os.path.isdir(old):
@@ -159,8 +181,6 @@ class Updater:
# handle files diff
# sync files that exist in both but are different
if self.debug:
self.log.dbg('sync files that exist in both but are different')
fdiff = diff.diff_files
fdiff.extend(diff.funny_files)
fdiff.extend(diff.common_funny)
@@ -175,8 +195,6 @@ class Updater:
self._handle_file(fleft, fright, compare=False)
# copy files that don't exist in dotdrop
if self.debug:
self.log.dbg('copy files not existing in dotdrop')
for toadd in diff.left_only:
exist = os.path.join(left, toadd)
if os.path.isdir(exist):
@@ -191,8 +209,6 @@ class Updater:
shutil.copyfile(exist, new)
# remove files that don't exist in deployed version
if self.debug:
self.log.dbg('remove files that do not exist in deployed version')
for toremove in diff.right_only:
new = os.path.join(right, toremove)
if not os.path.exists(new):