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

fix update on dotfiles for #328

This commit is contained in:
deadc0de6
2021-10-12 19:10:25 +02:00
parent 0bde2ab6b6
commit ab18239a12
5 changed files with 204 additions and 15 deletions

View File

@@ -100,14 +100,17 @@ class CfgAggregator:
path = os.path.join(TILD, path)
return path
def get_dotfile_by_dst(self, dst):
def get_dotfile_by_dst(self, dst, profile_key=None):
"""
get a list of dotfiles by dst
@dst: dotfile dst (on filesystem)
"""
dotfiles = []
dst = self._norm_path(dst)
for dotfile in self.dotfiles:
dfs = self.dotfiles
if profile_key:
dfs = self.get_dotfiles(profile_key=profile_key)
for dotfile in dfs:
left = self._norm_path(dotfile.dst)
if left == dst:
dotfiles.append(dotfile)
@@ -153,10 +156,14 @@ class CfgAggregator:
"""return profiles"""
return self.profiles
def get_profile(self):
def get_profile(self, key=None):
"""return profile object"""
pro = self.profile_key
if key:
pro = key
try:
return next(x for x in self.profiles if x.key == self.profile_key)
return next(x for x in self.profiles if x.key == pro)
except StopIteration:
return None
@@ -169,21 +176,27 @@ class CfgAggregator:
res.append(profile)
return res
def get_dotfiles(self):
"""get all dotfiles for this profile"""
def get_dotfiles(self, profile_key=None):
"""get all dotfiles for this profile or specified profile key"""
dotfiles = []
profile = self.get_profile()
profile = self.get_profile(key=profile_key)
if not profile:
return dotfiles
return profile.dotfiles
def get_dotfile(self, key):
def get_dotfile(self, key, profile_key=None):
"""
return dotfile object by key
@key: the dotfile key to look for
"""
dfs = self.dotfiles
if profile_key:
profile = self.get_profile(key=profile_key)
if not profile:
return None
dfs = profile.dotfiles
try:
return next(x for x in self.dotfiles
return next(x for x in dfs
if x.key == key)
except StopIteration:
return None

View File

@@ -77,7 +77,7 @@ def _dotfile_update(opts, path, key=False):
update a dotfile pointed by path
if key is false or by key (in path)
"""
updater = Updater(opts.dotpath, opts.variables, opts.conf,
updater = Updater(opts.dotpath, opts.variables, opts.conf, opts.profile,
dry=opts.dry, safe=opts.safe, debug=opts.debug,
ignore=opts.update_ignore,
showpatch=opts.update_showpatch,

View File

@@ -224,7 +224,7 @@ class Importer:
test no other dotfile exists with same
dst for this profile but different src
"""
dfs = self.conf.get_dotfile_by_dst(dst)
dfs = self.conf.get_dotfile_by_dst(dst, profile_key=self.profile)
if not dfs:
return False
for dotfile in dfs:

View File

@@ -25,13 +25,14 @@ class Updater:
"""dotfiles updater"""
def __init__(self, dotpath, variables, conf,
dry=False, safe=True, debug=False,
ignore=None, showpatch=False,
profile_key, dry=False, safe=True,
debug=False, ignore=None, showpatch=False,
ignore_missing_in_dotdrop=False):
"""constructor
@dotpath: path where dotfiles are stored
@variables: dictionary of variables for the templates
@conf: configuration manager
@profile_key: the profile key
@dry: simulate
@safe: ask for overwrite if True
@debug: enable debug
@@ -41,6 +42,7 @@ class Updater:
self.dotpath = dotpath
self.variables = variables
self.conf = conf
self.profile_key = profile_key
self.dry = dry
self.safe = safe
self.debug = debug
@@ -61,7 +63,8 @@ class Updater:
if not os.path.lexists(path):
self.log.err('\"{}\" does not exist!'.format(path))
return False
dotfiles = self.conf.get_dotfile_by_dst(path)
dotfiles = self.conf.get_dotfile_by_dst(path,
profile_key=self.profile_key)
if not dotfiles:
return False
for dotfile in dotfiles:
@@ -78,8 +81,11 @@ class Updater:
def update_key(self, key):
"""update the dotfile referenced by key"""
dotfile = self.conf.get_dotfile(key)
dotfile = self.conf.get_dotfile(key, profile_key=self.profile_key)
if not dotfile:
self.log.dbg('no such dotfile: \"{}\"'.format(key))
msg = 'invalid dotfile for update: {}'
self.log.err(msg.format(key))
return False
self.log.dbg('updating {} from key \"{}\"'.format(dotfile, key))
path = self.conf.path_to_dotfile_dst(dotfile.dst)