From 228524bdf3881219021c7d46347d2af5dd7bc24f Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 29 Mar 2019 22:57:58 +0100 Subject: [PATCH 1/2] implement import external actions for #113 --- dotdrop/config.py | 82 ++++++++++++++++------------ tests-ng/ext-actions.sh | 115 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 34 deletions(-) create mode 100755 tests-ng/ext-actions.sh diff --git a/dotdrop/config.py b/dotdrop/config.py index d3a8ae5..da8167c 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -33,7 +33,8 @@ class Cfg: key_showdiff = 'showdiff' key_deflink = 'link_by_default' key_workdir = 'workdir' - key_include_vars = 'import_variables' + key_import_vars = 'import_variables' + key_import_actions = 'import_actions' # actions keys key_actions = 'actions' @@ -216,45 +217,36 @@ class Cfg: self._abs_path(self.curworkdir) # load external variables/dynvariables - if self.key_include_vars in self.lnk_settings: - paths = self.lnk_settings[self.key_include_vars] + if self.key_import_vars in self.lnk_settings: + paths = self.lnk_settings[self.key_import_vars] self._load_ext_variables(paths, profile=profile) - # parse all actions - if self.key_actions in self.content: - if self.content[self.key_actions] is not None: - for k, v in self.content[self.key_actions].items(): - # loop through all actions - if k in [self.key_actions_pre, self.key_actions_post]: - # parse pre/post actions - items = self.content[self.key_actions][k].items() - for k2, v2 in items: - if k not in self.actions: - self.actions[k] = {} - a = Action(k2, k, v2) - self.actions[k][k2] = a - if self.debug: - self.log.dbg('new action: {}'.format(a)) - else: - # parse naked actions as post actions - if self.key_actions_post not in self.actions: - self.actions[self.key_actions_post] = {} - a = Action(k, '', v) - self.actions[self.key_actions_post][k] = a - if self.debug: - self.log.dbg('new action: {}'.format(a)) + # parse external actions + if self.key_import_actions in self.lnk_settings: + for path in self.lnk_settings[self.key_import_actions]: + if self.debug: + self.log.dbg('loading actions from {}'.format(path)) + content = self._load_yaml(path) + if self.key_actions in content and \ + content[self.key_actions] is not None: + self._load_actions(content[self.key_actions]) + + # parse local actions + if self.key_actions in self.content and \ + self.content[self.key_actions] is not None: + self._load_actions(self.content[self.key_actions]) # parse read transformations - if self.key_trans_r in self.content: - if self.content[self.key_trans_r] is not None: - for k, v in self.content[self.key_trans_r].items(): - self.trans_r[k] = Transform(k, v) + if self.key_trans_r in self.content and \ + self.content[self.key_trans_r] is not None: + for k, v in self.content[self.key_trans_r].items(): + self.trans_r[k] = Transform(k, v) # parse write transformations - if self.key_trans_w in self.content: - if self.content[self.key_trans_w] is not None: - for k, v in self.content[self.key_trans_w].items(): - self.trans_w[k] = Transform(k, v) + if self.key_trans_w in self.content and \ + self.content[self.key_trans_w] is not None: + for k, v in self.content[self.key_trans_w].items(): + self.trans_w[k] = Transform(k, v) # parse the dotfiles # and construct the dict of objects per dotfile key @@ -425,6 +417,28 @@ class Cfg: if self.debug: self.log.dbg('loaded ext dynvariables: {}'.format(dvariables)) + def _load_actions(self, dic): + for k, v in dic.items(): + # loop through all actions + if k in [self.key_actions_pre, self.key_actions_post]: + # parse pre/post actions + items = dic[k].items() + for k2, v2 in items: + if k not in self.actions: + self.actions[k] = {} + a = Action(k2, k, v2) + self.actions[k][k2] = a + if self.debug: + self.log.dbg('new action: {}'.format(a)) + else: + # parse naked actions as post actions + if self.key_actions_post not in self.actions: + self.actions[self.key_actions_post] = {} + a = Action(k, '', v) + self.actions[self.key_actions_post][k] = a + if self.debug: + self.log.dbg('new action: {}'.format(a)) + def _abs_path(self, path): """return absolute path of path relative to the confpath""" path = os.path.expanduser(path) diff --git a/tests-ng/ext-actions.sh b/tests-ng/ext-actions.sh new file mode 100755 index 0000000..550d69b --- /dev/null +++ b/tests-ng/ext-actions.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2019, deadc0de6 +# +# test external actions +# 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 action temp +tmpa=`mktemp -d --suffix='-dotdrop-tests'` +# the dotfile source +tmps=`mktemp -d --suffix='-dotdrop-tests'` +mkdir -p ${tmps}/dotfiles +# the dotfile destination +tmpd=`mktemp -d --suffix='-dotdrop-tests'` + +act="${tmps}/actions.yaml" +cat > ${act} << _EOF +actions: + pre: + preaction: echo 'pre' > ${tmpa}/pre + post: + postaction: echo 'post' > ${tmpa}/post + nakedaction: echo 'naked' > ${tmpa}/naked + overwrite: echo 'over' > ${tmpa}/write +_EOF + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + import_actions: + - ${tmps}/actions.yaml +actions: + overwrite: echo 'write' > ${tmpa}/write +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc + actions: + - preaction + - postaction + - nakedaction + - overwrite +profiles: + p1: + dotfiles: + - f_abc +_EOF +#cat ${cfg} + +# create the dotfile +echo "test" > ${tmps}/dotfiles/abc + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 + +# checks +[ ! -e ${tmpa}/pre ] && exit 1 +grep pre ${tmpa}/pre >/dev/null +[ ! -e ${tmpa}/post ] && exit 1 +grep post ${tmpa}/post >/dev/null +[ ! -e ${tmpa}/naked ] && exit 1 +grep naked ${tmpa}/naked >/dev/null +[ ! -e ${tmpa}/write ] && exit 1 +grep write ${tmpa}/write >/dev/null + +## CLEANING +rm -rf ${tmps} ${tmpd} ${tmpa} + +echo "OK" +exit 0 From d88f29b27631bc825fdcac445b88a196d19ef43d Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 29 Mar 2019 23:00:22 +0100 Subject: [PATCH 2/2] allow relative load external actions --- dotdrop/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dotdrop/config.py b/dotdrop/config.py index da8167c..4dee701 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -224,6 +224,7 @@ class Cfg: # parse external actions if self.key_import_actions in self.lnk_settings: for path in self.lnk_settings[self.key_import_actions]: + path = self._abs_path(path) if self.debug: self.log.dbg('loading actions from {}'.format(path)) content = self._load_yaml(path)