1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 13:09:18 +00:00

allow transformation to be templated (#177)

This commit is contained in:
deadc0de6
2019-07-10 20:47:16 +02:00
parent a4a831195e
commit 3a4552da55
5 changed files with 170 additions and 12 deletions

View File

@@ -101,8 +101,8 @@ Note:
* profile `(dyn)variables` > any other `(dyn)variables` * profile `(dyn)variables` > any other `(dyn)variables`
* profile `(dyn)variables` > profile's included `(dyn)variables` * profile `(dyn)variables` > profile's included `(dyn)variables`
* imported `variables`/`dynvariables` > `(dyn)variables` * imported `variables`/`dynvariables` > `(dyn)variables`
* actions using variables are resolved at runtime (when action is executed) * actions/transformations using variables are resolved at runtime
and not when loading the config (when action/transformation is executed) and not when loading the config
# Testing # Testing

View File

@@ -121,14 +121,20 @@ class Action(Cmd):
class Transform(Cmd): class Transform(Cmd):
def transform(self, arg0, arg1): def transform(self, arg0, arg1, templater=None, debug=False):
""" """
execute transformation with {0} and {1} execute transformation with {0} and {1}
where {0} is the file to transform where {0} is the file to transform
and {1} is the result file and {1} is the result file
""" """
ret = 1 ret = 1
cmd = self.action.format(arg0, arg1) action = self.action
if templater:
action = templater.generate_string(action)
if debug:
self.log.dbg('trans \"{}\" -> \"{}\"'.format(self.action,
action))
cmd = action.format(arg0, arg1)
if os.path.exists(arg1): if os.path.exists(arg1):
msg = 'transformation \"{}\": destination exists: {}' msg = 'transformation \"{}\": destination exists: {}'
self.log.warn(msg.format(cmd, arg1)) self.log.warn(msg.format(cmd, arg1))

View File

@@ -133,7 +133,7 @@ def cmd_install(o):
src = dotfile.src src = dotfile.src
tmp = None tmp = None
if dotfile.trans_r: if dotfile.trans_r:
tmp = apply_trans(o.dotpath, dotfile, debug=o.debug) tmp = apply_trans(o.dotpath, dotfile, t, debug=o.debug)
if not tmp: if not tmp:
continue continue
src = tmp src = tmp
@@ -224,7 +224,7 @@ def cmd_compare(o, tmp):
# apply transformation # apply transformation
if o.debug: if o.debug:
LOG.dbg('applying transformation before comparing') LOG.dbg('applying transformation before comparing')
tmpsrc = apply_trans(o.dotpath, dotfile, debug=o.debug) tmpsrc = apply_trans(o.dotpath, dotfile, t, debug=o.debug)
if not tmpsrc: if not tmpsrc:
# could not apply trans # could not apply trans
same = False same = False
@@ -561,7 +561,7 @@ def _select(selections, dotfiles):
return selected return selected
def apply_trans(dotpath, dotfile, debug=False): def apply_trans(dotpath, dotfile, templater, debug=False):
""" """
apply the read transformation to the dotfile apply the read transformation to the dotfile
return None if fails and new source if succeed return None if fails and new source if succeed
@@ -573,7 +573,7 @@ def apply_trans(dotpath, dotfile, debug=False):
LOG.dbg('executing transformation {}'.format(trans)) LOG.dbg('executing transformation {}'.format(trans))
s = os.path.join(dotpath, src) s = os.path.join(dotpath, src)
temp = os.path.join(dotpath, new_src) temp = os.path.join(dotpath, new_src)
if not trans.transform(s, temp): if not trans.transform(s, temp, templater=templater, debug=debug):
msg = 'transformation \"{}\" failed for {}' msg = 'transformation \"{}\" failed for {}'
LOG.err(msg.format(trans.key, dotfile.key)) LOG.err(msg.format(trans.key, dotfile.key))
if new_src and os.path.exists(new_src): if new_src and os.path.exists(new_src):

View File

@@ -48,6 +48,11 @@ class Updater:
self.debug = debug self.debug = debug
self.ignore = ignore self.ignore = ignore
self.showpatch = showpatch self.showpatch = showpatch
self.templater = Templategen(variables=self.variables,
base=self.dotpath,
debug=self.debug)
# save template vars
self.tvars = self.templater.add_tmp_vars()
self.log = Logger() self.log = Logger()
def update_path(self, path): def update_path(self, path):
@@ -110,7 +115,11 @@ class Updater:
if self.debug: if self.debug:
self.log.dbg('executing write transformation {}'.format(trans)) self.log.dbg('executing write transformation {}'.format(trans))
tmp = get_unique_tmp_name() tmp = get_unique_tmp_name()
if not trans.transform(path, tmp): self.templater.restore_vars(self.tvars)
newvars = dotfile.get_dotfile_variables()
self.templater.add_tmp_vars(newvars=newvars)
if not trans.transform(path, tmp, templater=self.templater,
debug=self.debug):
msg = 'transformation \"{}\" failed for {}' msg = 'transformation \"{}\" failed for {}'
self.log.err(msg.format(trans.key, dotfile.key)) self.log.err(msg.format(trans.key, dotfile.key))
if os.path.exists(tmp): if os.path.exists(tmp):
@@ -136,9 +145,8 @@ class Updater:
def _resolve_template(self, tpath): def _resolve_template(self, tpath):
"""resolve the template to a temporary file""" """resolve the template to a temporary file"""
t = Templategen(variables=self.variables, base=self.dotpath, self.templater.restore_vars(self.tvars)
debug=self.debug) return self.templater.generate(tpath)
return t.generate(tpath)
def _handle_file(self, path, dtpath, compare=True): def _handle_file(self, path, dtpath, compare=True):
"""sync path (deployed file) and dtpath (dotdrop dotfile path)""" """sync path (deployed file) and dtpath (dotdrop dotfile path)"""

View File

@@ -0,0 +1,144 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2019, deadc0de6
#
# test transformations using templates
#
# exit on first error
set -e
#set -v
# 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 "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)"
################################################################
# this is the test
################################################################
# the dotfile source
tmps=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
mkdir -p ${tmps}/dotfiles
echo "dotfiles source (dotpath): ${tmps}"
# the dotfile destination
tmpd=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
echo "dotfiles destination: ${tmpd}"
# create the config file
cfg="${tmps}/config.yaml"
# token
token="test-base64"
tokend="compressed archive"
touched="touched"
cat > ${cfg} << _EOF
trans_read:
r_echo_abs_src: echo "\$(cat {0}); {{@@ _dotfile_abs_src @@}}" > {1}
r_echo_var: echo "\$(cat {0}); {{@@ r_var @@}}" > {1}
trans_write:
w_echo_key: echo "\$(cat {0}); {{@@ _dotfile_key @@}}" > {1}
w_echo_var: echo "\$(cat {0}); {{@@ w_var @@}}" > {1}
variables:
r_var: readvar
w_var: writevar
config:
backup: true
create: true
dotpath: dotfiles
dotfiles:
f_def:
dst: ${tmpd}/def
src: def
f_abc:
dst: ${tmpd}/abc
src: abc
trans_read: r_echo_abs_src
trans_write: w_echo_key
d_ghi:
dst: ${tmpd}/ghi
src: ghi
trans_read: r_echo_var
trans_write: w_echo_var
profiles:
p1:
dotfiles:
- f_abc
- f_def
- d_ghi
_EOF
#cat ${cfg}
# create the dotfiles
echo 'abc' > ${tmps}/dotfiles/abc
echo 'marker' > ${tmps}/dotfiles/def
echo 'ghi' > ${tmps}/dotfiles/ghi
###########################
# test install and compare
###########################
# install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -b -V
# check dotfile
[ ! -e ${tmpd}/def ] && exit 1
[ ! -e ${tmpd}/abc ] && exit 1
[ ! -e ${tmpd}/ghi ] && exit 1
grep marker ${tmpd}/def
cat ${tmpd}/abc
grep "abc; ${tmps}/dotfiles/abc" ${tmpd}/abc
cat ${tmpd}/ghi
grep "ghi; readvar" ${tmpd}/ghi
###########################
# test update
###########################
# update single file
cd ${ddpath} | ${bin} update -f -k -c ${cfg} -p p1 -b -V
# checks
[ ! -e ${tmps}/dotfiles/def ] && exit 1
[ ! -e ${tmps}/dotfiles/abc ] && exit 1
[ ! -e ${tmps}/dotfiles/ghi ] && exit 1
grep marker ${tmps}/dotfiles/def
cat ${tmps}/dotfiles/abc
grep "abc; ${tmps}/dotfiles/abc; f_abc" ${tmps}/dotfiles/abc
cat ${tmps}/dotfiles/ghi
grep "ghi; readvar; writevar" ${tmps}/dotfiles/ghi
## CLEANING
rm -rf ${tmps} ${tmpd}
echo "OK"
exit 0