mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-06 18:55:38 +00:00
add ability to not deploy empty template
This commit is contained in:
@@ -29,6 +29,7 @@ class Cfg:
|
||||
key_banner = 'banner'
|
||||
key_long = 'longkey'
|
||||
key_keepdot = 'keepdot'
|
||||
key_ignoreempty = 'ignoreempty'
|
||||
key_showdiff = 'showdiff'
|
||||
key_deflink = 'link_by_default'
|
||||
key_workdir = 'workdir'
|
||||
@@ -49,6 +50,7 @@ class Cfg:
|
||||
key_dotfiles_src = 'src'
|
||||
key_dotfiles_dst = 'dst'
|
||||
key_dotfiles_link = 'link'
|
||||
key_dotfiles_noempty = 'ignoreempty'
|
||||
key_dotfiles_cmpignore = 'cmpignore'
|
||||
key_dotfiles_actions = 'actions'
|
||||
key_dotfiles_trans = 'trans'
|
||||
@@ -66,6 +68,7 @@ class Cfg:
|
||||
default_longkey = False
|
||||
default_keepdot = False
|
||||
default_showdiff = False
|
||||
default_ignoreempty = False
|
||||
default_link_by_default = False
|
||||
default_workdir = '~/.config/dotdrop'
|
||||
|
||||
@@ -191,6 +194,9 @@ class Cfg:
|
||||
dst = v[self.key_dotfiles_dst]
|
||||
link = v[self.key_dotfiles_link] if self.key_dotfiles_link \
|
||||
in v else self.default_link
|
||||
noempty = v[self.key_dotfiles_noempty] if \
|
||||
self.key_dotfiles_noempty \
|
||||
in v else self.lnk_settings[self.key_ignoreempty]
|
||||
itsactions = v[self.key_dotfiles_actions] if \
|
||||
self.key_dotfiles_actions in v else []
|
||||
actions = self._parse_actions(itsactions)
|
||||
@@ -206,7 +212,8 @@ class Cfg:
|
||||
self.key_dotfiles_cmpignore in v else []
|
||||
self.dotfiles[k] = Dotfile(k, dst, src,
|
||||
link=link, actions=actions,
|
||||
trans=trans, cmpignore=ignores)
|
||||
trans=trans, cmpignore=ignores,
|
||||
noempty=noempty)
|
||||
|
||||
# assign dotfiles to each profile
|
||||
for k, v in self.lnk_profiles.items():
|
||||
@@ -222,7 +229,12 @@ class Cfg:
|
||||
self.prodots[k] = list(self.dotfiles.values())
|
||||
else:
|
||||
# add the dotfiles
|
||||
self.prodots[k].extend([self.dotfiles[d] for d in dots])
|
||||
for d in dots:
|
||||
if d not in self.dotfiles:
|
||||
msg = 'unknown dotfile \"{}\" for {}'.format(d, k)
|
||||
self.log.err(msg)
|
||||
continue
|
||||
self.prodots[k].append(self.dotfiles[d])
|
||||
|
||||
# handle "include" for each profile
|
||||
for k in self.lnk_profiles.keys():
|
||||
@@ -319,6 +331,8 @@ class Cfg:
|
||||
self.lnk_settings[self.key_workdir] = self.default_workdir
|
||||
if self.key_showdiff not in self.lnk_settings:
|
||||
self.lnk_settings[self.key_showdiff] = self.default_showdiff
|
||||
if self.key_ignoreempty not in self.lnk_settings:
|
||||
self.lnk_settings[self.key_ignoreempty] = self.default_ignoreempty
|
||||
|
||||
def abs_dotpath(self, path):
|
||||
"""transform path to an absolute path based on config path"""
|
||||
|
||||
@@ -86,8 +86,8 @@ def cmd_install(opts, conf, temporary=False, keys=[]):
|
||||
# filtered dotfiles to install
|
||||
dotfiles = [d for d in dotfiles if d.key in set(keys)]
|
||||
if not dotfiles:
|
||||
msg = 'no dotfiles to install for this profile (\"{}\")'
|
||||
LOG.err(msg.format(opts['profile']))
|
||||
msg = 'no dotfile to install for this profile (\"{}\")'
|
||||
LOG.warn(msg.format(opts['profile']))
|
||||
return False
|
||||
|
||||
t = Templategen(opts['profile'], base=opts['dotpath'],
|
||||
@@ -118,7 +118,8 @@ def cmd_install(opts, conf, temporary=False, keys=[]):
|
||||
if not tmp:
|
||||
continue
|
||||
src = tmp
|
||||
r = inst.install(t, src, dotfile.dst, actions=preactions)
|
||||
r = inst.install(t, src, dotfile.dst, actions=preactions,
|
||||
noempty=dotfile.noempty)
|
||||
if tmp:
|
||||
tmp = os.path.join(opts['dotpath'], tmp)
|
||||
if os.path.exists(tmp):
|
||||
@@ -145,8 +146,8 @@ def cmd_compare(opts, conf, tmp, focus=[], ignore=[]):
|
||||
"""compare dotfiles and return True if all identical"""
|
||||
dotfiles = conf.get_dotfiles(opts['profile'])
|
||||
if dotfiles == []:
|
||||
msg = 'no dotfiles defined for this profile (\"{}\")'
|
||||
LOG.err(msg.format(opts['profile']))
|
||||
msg = 'no dotfile defined for this profile (\"{}\")'
|
||||
LOG.warn(msg.format(opts['profile']))
|
||||
return True
|
||||
# compare only specific files
|
||||
same = True
|
||||
|
||||
@@ -10,7 +10,8 @@ class Dotfile:
|
||||
|
||||
def __init__(self, key, dst, src,
|
||||
actions={}, trans=[],
|
||||
link=False, cmpignore=[]):
|
||||
link=False, cmpignore=[],
|
||||
noempty=False):
|
||||
# key of dotfile in the config
|
||||
self.key = key
|
||||
# path where to install this dotfile
|
||||
@@ -25,6 +26,8 @@ class Dotfile:
|
||||
self.trans = trans
|
||||
# pattern to ignore when comparing
|
||||
self.cmpignore = cmpignore
|
||||
# do not deploy empty file
|
||||
self.noempty = noempty
|
||||
|
||||
def __str__(self):
|
||||
msg = 'key:\"{}\", src:\"{}\", dst:\"{}\", link:\"{}\"'
|
||||
|
||||
@@ -35,7 +35,7 @@ class Installer:
|
||||
self.action_executed = False
|
||||
self.log = Logger()
|
||||
|
||||
def install(self, templater, src, dst, actions=[]):
|
||||
def install(self, templater, src, dst, actions=[], noempty=False):
|
||||
"""install the src to dst using a template"""
|
||||
self.action_executed = False
|
||||
src = os.path.join(self.base, os.path.expanduser(src))
|
||||
@@ -52,8 +52,10 @@ class Installer:
|
||||
if self.debug:
|
||||
self.log.dbg('install {} to {}'.format(src, dst))
|
||||
if os.path.isdir(src):
|
||||
return self._handle_dir(templater, src, dst, actions=actions)
|
||||
return self._handle_file(templater, src, dst, actions=actions)
|
||||
return self._handle_dir(templater, src, dst, actions=actions,
|
||||
noempty=noempty)
|
||||
return self._handle_file(templater, src, dst,
|
||||
actions=actions, noempty=noempty)
|
||||
|
||||
def link(self, templater, src, dst, actions=[]):
|
||||
"""set src as the link target of dst"""
|
||||
@@ -110,15 +112,19 @@ class Installer:
|
||||
self.log.sub('linked {} to {}'.format(dst, src))
|
||||
return [(src, dst)]
|
||||
|
||||
def _handle_file(self, templater, src, dst, actions=[]):
|
||||
def _handle_file(self, templater, src, dst, actions=[], noempty=False):
|
||||
"""install src to dst when is a file"""
|
||||
if self.debug:
|
||||
self.log.dbg('generate template for {}'.format(src))
|
||||
self.log.dbg('ignore empty: {}'.format(noempty))
|
||||
if utils.samefile(src, dst):
|
||||
# symlink loop
|
||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
||||
return []
|
||||
content = templater.generate(src)
|
||||
if noempty and utils.content_empty(content):
|
||||
self.log.warn('ignoring empty template: {}'.format(src))
|
||||
return []
|
||||
if content is None:
|
||||
self.log.err('generate from template {}'.format(src))
|
||||
return []
|
||||
@@ -140,8 +146,11 @@ class Installer:
|
||||
return [(src, dst)]
|
||||
return []
|
||||
|
||||
def _handle_dir(self, templater, src, dst, actions=[]):
|
||||
def _handle_dir(self, templater, src, dst, actions=[], noempty=False):
|
||||
"""install src to dst when is a directory"""
|
||||
if self.debug:
|
||||
self.log.dbg('install dir {}'.format(src))
|
||||
self.log.dbg('ignore empty: {}'.format(noempty))
|
||||
ret = []
|
||||
if not self._create_dirs(dst):
|
||||
return []
|
||||
@@ -150,11 +159,11 @@ class Installer:
|
||||
f = os.path.join(src, entry)
|
||||
if not os.path.isdir(f):
|
||||
res = self._handle_file(templater, f, os.path.join(dst, entry),
|
||||
actions=actions)
|
||||
actions=actions, noempty=noempty)
|
||||
ret.extend(res)
|
||||
else:
|
||||
res = self._handle_dir(templater, f, os.path.join(dst, entry),
|
||||
actions=actions)
|
||||
actions=actions, noempty=noempty)
|
||||
ret.extend(res)
|
||||
return ret
|
||||
|
||||
|
||||
@@ -78,4 +78,14 @@ def samefile(path1, path2):
|
||||
|
||||
|
||||
def header():
|
||||
"""return dotdrop header"""
|
||||
return 'This dotfile is managed using dotdrop'
|
||||
|
||||
|
||||
def content_empty(string):
|
||||
"""return True if is empty or only one CRLF"""
|
||||
if not string:
|
||||
return True
|
||||
if string == b'\n':
|
||||
return True
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user