diff --git a/dotdrop/action.py b/dotdrop/action.py index e383d0a..617bf72 100644 --- a/dotdrop/action.py +++ b/dotdrop/action.py @@ -68,12 +68,12 @@ class Action(Cmd): def __repr__(self): return 'action({})'.format(self.__str__()) - def execute(self, templater=None, newvars={}): + def execute(self, templater=None): """execute the action in the shell""" ret = 1 action = self.action if templater: - action = templater.generate_string(self.action, tmpvars=newvars) + action = templater.generate_string(self.action) try: cmd = action.format(*self.args) except IndexError: diff --git a/dotdrop/config.py b/dotdrop/config.py index 155349e..caaab31 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -175,21 +175,23 @@ class Cfg: """resolve dotfiles src/dst/actions templating for this profile""" t = Templategen(variables=variables) dotfiles = self._get_dotfiles(profile) + tvars = t.add_tmp_vars() for d in dotfiles: + # add dotfile variables + t.restore_vars(tvars) + newvar = d.get_vars() + t.add_tmp_vars(newvars=newvar) # src and dst path d.src = t.generate_string(d.src) d.dst = t.generate_string(d.dst) # pre actions - var = d.get_vars() if self.key_actions_pre in d.actions: for action in d.actions[self.key_actions_pre]: - action.action = t.generate_string(action.action, - tmpvars=var) + action.action = t.generate_string(action.action) # post actions if self.key_actions_post in d.actions: for action in d.actions[self.key_actions_post]: - action.action = t.generate_string(action.action, - tmpvars=var) + action.action = t.generate_string(action.action) return dotfiles def _load_config(self, profile=None): diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index d8b1dca..c020188 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -45,8 +45,7 @@ def action_executor(o, dotfile, actions, defactions, templater, post=False): continue if o.debug: LOG.dbg('executing def-{}-action {}'.format(s, action)) - newvars = dotfile.get_vars() - ret = action.execute(templater=templater, newvars=newvars) + ret = action.execute(templater=templater) if not ret: err = 'def-{}-action \"{}\" failed'.format(s, action.key) LOG.err(err) @@ -59,8 +58,7 @@ def action_executor(o, dotfile, actions, defactions, templater, post=False): continue if o.debug: LOG.dbg('executing {}-action {}'.format(s, action)) - newvars = dotfile.get_vars() - ret = action.execute(templater=templater, newvars=newvars) + ret = action.execute(templater=templater) if not ret: err = '{}-action \"{}\" failed'.format(s, action.key) LOG.err(err) @@ -93,7 +91,13 @@ def cmd_install(o): showdiff=o.install_showdiff, backup_suffix=o.install_backup_suffix) installed = 0 + tvars = t.add_tmp_vars() for dotfile in dotfiles: + # add dotfile variables + t.restore_vars(tvars) + newvars = dotfile.get_vars() + t.add_tmp_vars(newvars=newvars) + preactions = [] if not o.install_temporary and dotfile.actions \ and Cfg.key_actions_pre in dotfile.actions: diff --git a/dotdrop/dotfile.py b/dotdrop/dotfile.py index ce1cac3..6b9b939 100644 --- a/dotdrop/dotfile.py +++ b/dotdrop/dotfile.py @@ -43,8 +43,8 @@ class Dotfile: def get_vars(self): """return this dotfile templating vars""" _vars = {} - _vars['_dotfile_src'] = self.src - _vars['_dotfile_dst'] = self.dst + _vars['_dotfile_abs_src'] = self.src + _vars['_dotfile_abs_dst'] = self.dst return _vars def __str__(self): diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index d5b7a1f..2ae2aad 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -51,36 +51,28 @@ class Templategen: self.env.globals['exists'] = jhelpers.exists self.env.globals['exists_in_path'] = jhelpers.exists_in_path - def generate(self, src, tmpvars={}): + def generate(self, src): """render template from path""" if not os.path.exists(src): return '' - saved = self._patch_globals(tmpvars) - ret = self._handle_file(src) - self._restore_globals(saved) - return ret + return self._handle_file(src) - def generate_string(self, string, tmpvars={}): + def generate_string(self, string): """render template from string""" if not string: return '' - saved = self._patch_globals(tmpvars) - if self.debug: - self.log.dbg('new vars: {}'.format(tmpvars)) - ret = self.env.from_string(string).render() - self._restore_globals(saved) - return ret + return self.env.from_string(string).render() - def _patch_globals(self, newvars={}): - """add vars to the globals, make sure to call _restore_globals""" + def add_tmp_vars(self, newvars={}): + """add vars to the globals, make sure to call restore_vars""" saved_globals = self.env.globals.copy() if not newvars: return saved_globals self.env.globals.update(newvars) return saved_globals - def _restore_globals(self, saved_globals): - """restore globals from _patch_globals""" + def restore_vars(self, saved_globals): + """restore globals from add_tmp_vars""" self.env.globals = saved_globals.copy() def update_variables(self, variables): diff --git a/tests-ng/actions-template.sh b/tests-ng/actions-template.sh index 88a8d0f..17db3d1 100755 --- a/tests-ng/actions-template.sh +++ b/tests-ng/actions-template.sh @@ -59,10 +59,10 @@ cfg="${tmps}/config.yaml" cat > ${cfg} << _EOF actions: pre: - preaction: "echo {{@@ _dotfile_src @@}} > {0}" + preaction: "echo {{@@ _dotfile_abs_src @@}} > {0}" post: - postaction: "echo {{@@ _dotfile_src @@}} > ${tmpa}/post" - nakedaction: "echo {{@@ _dotfile_src @@}} > ${tmpa}/naked" + postaction: "echo {{@@ _dotfile_abs_src @@}} > ${tmpa}/post" + nakedaction: "echo {{@@ _dotfile_abs_src @@}} > ${tmpa}/naked" config: backup: true create: true @@ -102,10 +102,10 @@ rm -f ${tmpa}/naked* ${tmpa}/pre* ${tmpa}/post* ${tmpd}/abc cat > ${cfg} << _EOF actions: pre: - preaction: "echo {{@@ _dotfile_dst @@}} > ${tmpa}/pre" + preaction: "echo {{@@ _dotfile_abs_dst @@}} > ${tmpa}/pre" post: - postaction: "echo {{@@ _dotfile_dst @@}} > ${tmpa}/post" - nakedaction: "echo {{@@ _dotfile_dst @@}} > ${tmpa}/naked" + postaction: "echo {{@@ _dotfile_abs_dst @@}} > ${tmpa}/post" + nakedaction: "echo {{@@ _dotfile_abs_dst @@}} > ${tmpa}/naked" config: backup: true create: true diff --git a/tests-ng/dotfile-variables.sh b/tests-ng/dotfile-variables.sh new file mode 100755 index 0000000..0d48d64 --- /dev/null +++ b/tests-ng/dotfile-variables.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test dotfile specific variables +# 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 --suffix='-dotdrop-tests'` +mkdir -p ${tmps}/dotfiles +# the dotfile destination +tmpd=`mktemp -d --suffix='-dotdrop-tests'` + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF +#cat ${cfg} + +# create the dotfile +echo 'src:{{@@ _dotfile_abs_src @@}}' > ${tmps}/dotfiles/abc +echo 'dst:{{@@ _dotfile_abs_dst @@}}' >> ${tmps}/dotfiles/abc + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V + +# checks +[ ! -e ${tmpd}/abc ] && echo 'dotfile not installed' && exit 1 +grep "src:${tmps}/dotfiles/abc" ${tmpd}/abc >/dev/null +grep "dst:${tmpd}/abc" ${tmpd}/abc >/dev/null + +## CLEANING +rm -rf ${tmps} ${tmpd} + +echo "OK" +exit 0