mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 21:14:00 +00:00
add ability to not deploy empty template
This commit is contained in:
17
README.md
17
README.md
@@ -570,12 +570,14 @@ the following entries:
|
|||||||
* `link_by_default`: when importing a dotfile set `link` to that value per default (default *false*)
|
* `link_by_default`: when importing a dotfile set `link` to that value per default (default *false*)
|
||||||
* `workdir`: directory where templates are installed before being symlink when using `link` (default *~/.config/dotdrop*)
|
* `workdir`: directory where templates are installed before being symlink when using `link` (default *~/.config/dotdrop*)
|
||||||
* `showdiff`: on install show a diff before asking to overwrite (see `--showdiff`) (default *false*)
|
* `showdiff`: on install show a diff before asking to overwrite (see `--showdiff`) (default *false*)
|
||||||
|
* `ignoreempty`: do not deploy template if empty (default *false*)
|
||||||
|
|
||||||
* **dotfiles** entry: a list of dotfiles
|
* **dotfiles** entry: a list of dotfiles
|
||||||
* When `link` is true, dotdrop will create a symlink instead of copying (default *false*).
|
* `link`: if true dotdrop will create a symlink instead of copying (default *false*).
|
||||||
* `cmpignore` contains a list of pattern to ignore when comparing (enclose in quotes when using wildcards).
|
* `cmpignore`: list of pattern to ignore when comparing (enclose in quotes when using wildcards).
|
||||||
* `actions` contains a list of action keys that need to be defined in the **actions** entry below.
|
* `actions`: list of action keys that need to be defined in the **actions** entry below.
|
||||||
* `trans` contains a list of transformation keys that need to be defined in the **trans** entry below.
|
* `trans`: list of transformation keys that need to be defined in the **trans** entry below.
|
||||||
|
* `ignoreempty`: if true empty template will not be deployed (default global value of `ignoreempty` above)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
<dotfile-key-name>:
|
<dotfile-key-name>:
|
||||||
@@ -583,6 +585,7 @@ the following entries:
|
|||||||
src: <filename-within-the-dotpath>
|
src: <filename-within-the-dotpath>
|
||||||
# Optional
|
# Optional
|
||||||
link: <true|false>
|
link: <true|false>
|
||||||
|
ignoreempty: <true|false>
|
||||||
cmpignore:
|
cmpignore:
|
||||||
- "<ignore-pattern>"
|
- "<ignore-pattern>"
|
||||||
actions:
|
actions:
|
||||||
@@ -668,6 +671,12 @@ profiles:
|
|||||||
```
|
```
|
||||||
Here profile *host1* contains all the dotfiles defined for *host2* plus `f_xinitrc`.
|
Here profile *host1* contains all the dotfiles defined for *host2* plus `f_xinitrc`.
|
||||||
|
|
||||||
|
## Ignore empty template
|
||||||
|
|
||||||
|
It is possible not to deploy template file if their rendered content
|
||||||
|
is empty. Simply set the global setting `ignoreempty` to true for this
|
||||||
|
behavior for all dotfiles or specifically to one or more dotfile entries.
|
||||||
|
|
||||||
# Templating
|
# Templating
|
||||||
|
|
||||||
Dotdrop leverage the power of [jinja2](http://jinja.pocoo.org/) to handle the
|
Dotdrop leverage the power of [jinja2](http://jinja.pocoo.org/) to handle the
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class Cfg:
|
|||||||
key_banner = 'banner'
|
key_banner = 'banner'
|
||||||
key_long = 'longkey'
|
key_long = 'longkey'
|
||||||
key_keepdot = 'keepdot'
|
key_keepdot = 'keepdot'
|
||||||
|
key_ignoreempty = 'ignoreempty'
|
||||||
key_showdiff = 'showdiff'
|
key_showdiff = 'showdiff'
|
||||||
key_deflink = 'link_by_default'
|
key_deflink = 'link_by_default'
|
||||||
key_workdir = 'workdir'
|
key_workdir = 'workdir'
|
||||||
@@ -49,6 +50,7 @@ class Cfg:
|
|||||||
key_dotfiles_src = 'src'
|
key_dotfiles_src = 'src'
|
||||||
key_dotfiles_dst = 'dst'
|
key_dotfiles_dst = 'dst'
|
||||||
key_dotfiles_link = 'link'
|
key_dotfiles_link = 'link'
|
||||||
|
key_dotfiles_noempty = 'ignoreempty'
|
||||||
key_dotfiles_cmpignore = 'cmpignore'
|
key_dotfiles_cmpignore = 'cmpignore'
|
||||||
key_dotfiles_actions = 'actions'
|
key_dotfiles_actions = 'actions'
|
||||||
key_dotfiles_trans = 'trans'
|
key_dotfiles_trans = 'trans'
|
||||||
@@ -66,6 +68,7 @@ class Cfg:
|
|||||||
default_longkey = False
|
default_longkey = False
|
||||||
default_keepdot = False
|
default_keepdot = False
|
||||||
default_showdiff = False
|
default_showdiff = False
|
||||||
|
default_ignoreempty = False
|
||||||
default_link_by_default = False
|
default_link_by_default = False
|
||||||
default_workdir = '~/.config/dotdrop'
|
default_workdir = '~/.config/dotdrop'
|
||||||
|
|
||||||
@@ -191,6 +194,9 @@ class Cfg:
|
|||||||
dst = v[self.key_dotfiles_dst]
|
dst = v[self.key_dotfiles_dst]
|
||||||
link = v[self.key_dotfiles_link] if self.key_dotfiles_link \
|
link = v[self.key_dotfiles_link] if self.key_dotfiles_link \
|
||||||
in v else self.default_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 \
|
itsactions = v[self.key_dotfiles_actions] if \
|
||||||
self.key_dotfiles_actions in v else []
|
self.key_dotfiles_actions in v else []
|
||||||
actions = self._parse_actions(itsactions)
|
actions = self._parse_actions(itsactions)
|
||||||
@@ -206,7 +212,8 @@ class Cfg:
|
|||||||
self.key_dotfiles_cmpignore in v else []
|
self.key_dotfiles_cmpignore in v else []
|
||||||
self.dotfiles[k] = Dotfile(k, dst, src,
|
self.dotfiles[k] = Dotfile(k, dst, src,
|
||||||
link=link, actions=actions,
|
link=link, actions=actions,
|
||||||
trans=trans, cmpignore=ignores)
|
trans=trans, cmpignore=ignores,
|
||||||
|
noempty=noempty)
|
||||||
|
|
||||||
# assign dotfiles to each profile
|
# assign dotfiles to each profile
|
||||||
for k, v in self.lnk_profiles.items():
|
for k, v in self.lnk_profiles.items():
|
||||||
@@ -222,7 +229,12 @@ class Cfg:
|
|||||||
self.prodots[k] = list(self.dotfiles.values())
|
self.prodots[k] = list(self.dotfiles.values())
|
||||||
else:
|
else:
|
||||||
# add the dotfiles
|
# 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
|
# handle "include" for each profile
|
||||||
for k in self.lnk_profiles.keys():
|
for k in self.lnk_profiles.keys():
|
||||||
@@ -319,6 +331,8 @@ class Cfg:
|
|||||||
self.lnk_settings[self.key_workdir] = self.default_workdir
|
self.lnk_settings[self.key_workdir] = self.default_workdir
|
||||||
if self.key_showdiff not in self.lnk_settings:
|
if self.key_showdiff not in self.lnk_settings:
|
||||||
self.lnk_settings[self.key_showdiff] = self.default_showdiff
|
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):
|
def abs_dotpath(self, path):
|
||||||
"""transform path to an absolute path based on config 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
|
# filtered dotfiles to install
|
||||||
dotfiles = [d for d in dotfiles if d.key in set(keys)]
|
dotfiles = [d for d in dotfiles if d.key in set(keys)]
|
||||||
if not dotfiles:
|
if not dotfiles:
|
||||||
msg = 'no dotfiles to install for this profile (\"{}\")'
|
msg = 'no dotfile to install for this profile (\"{}\")'
|
||||||
LOG.err(msg.format(opts['profile']))
|
LOG.warn(msg.format(opts['profile']))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
t = Templategen(opts['profile'], base=opts['dotpath'],
|
t = Templategen(opts['profile'], base=opts['dotpath'],
|
||||||
@@ -118,7 +118,8 @@ def cmd_install(opts, conf, temporary=False, keys=[]):
|
|||||||
if not tmp:
|
if not tmp:
|
||||||
continue
|
continue
|
||||||
src = tmp
|
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:
|
if tmp:
|
||||||
tmp = os.path.join(opts['dotpath'], tmp)
|
tmp = os.path.join(opts['dotpath'], tmp)
|
||||||
if os.path.exists(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"""
|
"""compare dotfiles and return True if all identical"""
|
||||||
dotfiles = conf.get_dotfiles(opts['profile'])
|
dotfiles = conf.get_dotfiles(opts['profile'])
|
||||||
if dotfiles == []:
|
if dotfiles == []:
|
||||||
msg = 'no dotfiles defined for this profile (\"{}\")'
|
msg = 'no dotfile defined for this profile (\"{}\")'
|
||||||
LOG.err(msg.format(opts['profile']))
|
LOG.warn(msg.format(opts['profile']))
|
||||||
return True
|
return True
|
||||||
# compare only specific files
|
# compare only specific files
|
||||||
same = True
|
same = True
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ class Dotfile:
|
|||||||
|
|
||||||
def __init__(self, key, dst, src,
|
def __init__(self, key, dst, src,
|
||||||
actions={}, trans=[],
|
actions={}, trans=[],
|
||||||
link=False, cmpignore=[]):
|
link=False, cmpignore=[],
|
||||||
|
noempty=False):
|
||||||
# key of dotfile in the config
|
# key of dotfile in the config
|
||||||
self.key = key
|
self.key = key
|
||||||
# path where to install this dotfile
|
# path where to install this dotfile
|
||||||
@@ -25,6 +26,8 @@ class Dotfile:
|
|||||||
self.trans = trans
|
self.trans = trans
|
||||||
# pattern to ignore when comparing
|
# pattern to ignore when comparing
|
||||||
self.cmpignore = cmpignore
|
self.cmpignore = cmpignore
|
||||||
|
# do not deploy empty file
|
||||||
|
self.noempty = noempty
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
msg = 'key:\"{}\", src:\"{}\", dst:\"{}\", link:\"{}\"'
|
msg = 'key:\"{}\", src:\"{}\", dst:\"{}\", link:\"{}\"'
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class Installer:
|
|||||||
self.action_executed = False
|
self.action_executed = False
|
||||||
self.log = Logger()
|
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"""
|
"""install the src to dst using a template"""
|
||||||
self.action_executed = False
|
self.action_executed = False
|
||||||
src = os.path.join(self.base, os.path.expanduser(src))
|
src = os.path.join(self.base, os.path.expanduser(src))
|
||||||
@@ -52,8 +52,10 @@ class Installer:
|
|||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('install {} to {}'.format(src, dst))
|
self.log.dbg('install {} to {}'.format(src, dst))
|
||||||
if os.path.isdir(src):
|
if os.path.isdir(src):
|
||||||
return self._handle_dir(templater, src, dst, actions=actions)
|
return self._handle_dir(templater, src, dst, actions=actions,
|
||||||
return self._handle_file(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=[]):
|
def link(self, templater, src, dst, actions=[]):
|
||||||
"""set src as the link target of dst"""
|
"""set src as the link target of dst"""
|
||||||
@@ -110,15 +112,19 @@ class Installer:
|
|||||||
self.log.sub('linked {} to {}'.format(dst, src))
|
self.log.sub('linked {} to {}'.format(dst, src))
|
||||||
return [(src, dst)]
|
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"""
|
"""install src to dst when is a file"""
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('generate template for {}'.format(src))
|
self.log.dbg('generate template for {}'.format(src))
|
||||||
|
self.log.dbg('ignore empty: {}'.format(noempty))
|
||||||
if utils.samefile(src, dst):
|
if utils.samefile(src, dst):
|
||||||
# symlink loop
|
# symlink loop
|
||||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
self.log.err('dotfile points to itself: {}'.format(dst))
|
||||||
return []
|
return []
|
||||||
content = templater.generate(src)
|
content = templater.generate(src)
|
||||||
|
if noempty and utils.content_empty(content):
|
||||||
|
self.log.warn('ignoring empty template: {}'.format(src))
|
||||||
|
return []
|
||||||
if content is None:
|
if content is None:
|
||||||
self.log.err('generate from template {}'.format(src))
|
self.log.err('generate from template {}'.format(src))
|
||||||
return []
|
return []
|
||||||
@@ -140,8 +146,11 @@ class Installer:
|
|||||||
return [(src, dst)]
|
return [(src, dst)]
|
||||||
return []
|
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"""
|
"""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 = []
|
ret = []
|
||||||
if not self._create_dirs(dst):
|
if not self._create_dirs(dst):
|
||||||
return []
|
return []
|
||||||
@@ -150,11 +159,11 @@ class Installer:
|
|||||||
f = os.path.join(src, entry)
|
f = os.path.join(src, entry)
|
||||||
if not os.path.isdir(f):
|
if not os.path.isdir(f):
|
||||||
res = self._handle_file(templater, f, os.path.join(dst, entry),
|
res = self._handle_file(templater, f, os.path.join(dst, entry),
|
||||||
actions=actions)
|
actions=actions, noempty=noempty)
|
||||||
ret.extend(res)
|
ret.extend(res)
|
||||||
else:
|
else:
|
||||||
res = self._handle_dir(templater, f, os.path.join(dst, entry),
|
res = self._handle_dir(templater, f, os.path.join(dst, entry),
|
||||||
actions=actions)
|
actions=actions, noempty=noempty)
|
||||||
ret.extend(res)
|
ret.extend(res)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|||||||
@@ -78,4 +78,14 @@ def samefile(path1, path2):
|
|||||||
|
|
||||||
|
|
||||||
def header():
|
def header():
|
||||||
|
"""return dotdrop header"""
|
||||||
return 'This dotfile is managed using dotdrop'
|
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
|
||||||
|
|||||||
122
tests-ng/ignore-empty.sh
Executable file
122
tests-ng/ignore-empty.sh
Executable file
@@ -0,0 +1,122 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
# Copyright (c) 2017, deadc0de6
|
||||||
|
#
|
||||||
|
# test empty template generation
|
||||||
|
# returns 1 in case of error
|
||||||
|
#
|
||||||
|
|
||||||
|
# exit on first error
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# all this crap to get current path
|
||||||
|
rl="readlink -f"
|
||||||
|
if ! ${rl} "${0}" >/dev/null 2>&1; then
|
||||||
|
rl="realpath"
|
||||||
|
|
||||||
|
if ! hash ${rl}; then
|
||||||
|
echo "\"${rl}\" not found !" && exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
cur=$(dirname "$(${rl} "${0}")")
|
||||||
|
|
||||||
|
#hash dotdrop >/dev/null 2>&1
|
||||||
|
#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1
|
||||||
|
|
||||||
|
#echo "called with ${1}"
|
||||||
|
|
||||||
|
# dotdrop path can be pass as argument
|
||||||
|
ddpath="${cur}/../"
|
||||||
|
[ "${1}" != "" ] && ddpath="${1}"
|
||||||
|
[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1
|
||||||
|
|
||||||
|
export PYTHONPATH="${ddpath}:${PYTHONPATH}"
|
||||||
|
bin="python3 -m dotdrop.dotdrop"
|
||||||
|
|
||||||
|
echo "dotdrop path: ${ddpath}"
|
||||||
|
echo "pythonpath: ${PYTHONPATH}"
|
||||||
|
|
||||||
|
# get the helpers
|
||||||
|
source ${cur}/helpers
|
||||||
|
|
||||||
|
echo -e "\e[96m\e[1m==> RUNNING $(basename $BASH_SOURCE) <==\e[0m"
|
||||||
|
|
||||||
|
################################################################
|
||||||
|
# this is the test
|
||||||
|
################################################################
|
||||||
|
|
||||||
|
# the dotfile source
|
||||||
|
tmps=`mktemp -d`
|
||||||
|
mkdir -p ${tmps}/dotfiles
|
||||||
|
#echo "dotfile source: ${tmps}"
|
||||||
|
# the dotfile destination
|
||||||
|
tmpd=`mktemp -d`
|
||||||
|
#echo "dotfile destination: ${tmpd}"
|
||||||
|
|
||||||
|
# create the config file
|
||||||
|
cfg="${tmps}/config.yaml"
|
||||||
|
|
||||||
|
# globally
|
||||||
|
cat > ${cfg} << _EOF
|
||||||
|
config:
|
||||||
|
backup: true
|
||||||
|
create: true
|
||||||
|
dotpath: dotfiles
|
||||||
|
ignoreempty: true
|
||||||
|
dotfiles:
|
||||||
|
d_d1:
|
||||||
|
dst: ${tmpd}/d1
|
||||||
|
src: d1
|
||||||
|
profiles:
|
||||||
|
p1:
|
||||||
|
dotfiles:
|
||||||
|
- d_d1
|
||||||
|
_EOF
|
||||||
|
#cat ${cfg}
|
||||||
|
|
||||||
|
# create the dotfile
|
||||||
|
mkdir -p ${tmps}/dotfiles/d1
|
||||||
|
echo "{{@@ var1 @@}}" > ${tmps}/dotfiles/d1/empty
|
||||||
|
echo "not empty" >> ${tmps}/dotfiles/d1/notempty
|
||||||
|
|
||||||
|
# install
|
||||||
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
||||||
|
|
||||||
|
# test existence
|
||||||
|
[ -e ${tmpd}/d1/empty ] && exit 1
|
||||||
|
[ ! -e ${tmpd}/d1/notempty ] && exit 1
|
||||||
|
|
||||||
|
# through the dotfile
|
||||||
|
cat > ${cfg} << _EOF
|
||||||
|
config:
|
||||||
|
backup: true
|
||||||
|
create: true
|
||||||
|
dotpath: dotfiles
|
||||||
|
ignoreempty: false
|
||||||
|
dotfiles:
|
||||||
|
d_d1:
|
||||||
|
dst: ${tmpd}/d1
|
||||||
|
src: d1
|
||||||
|
ignoreempty: true
|
||||||
|
profiles:
|
||||||
|
p1:
|
||||||
|
dotfiles:
|
||||||
|
- d_d1
|
||||||
|
_EOF
|
||||||
|
#cat ${cfg}
|
||||||
|
|
||||||
|
# clean destination
|
||||||
|
rm -rf ${tmpd}/*
|
||||||
|
|
||||||
|
# install
|
||||||
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
||||||
|
|
||||||
|
# test existence
|
||||||
|
[ -e ${tmpd}/d1/empty ] && exit 1
|
||||||
|
[ ! -e ${tmpd}/d1/notempty ] && exit 1
|
||||||
|
|
||||||
|
## CLEANING
|
||||||
|
rm -rf ${tmps} ${tmpd}
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user