1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 18:34:48 +00:00

implement import external actions for #113

This commit is contained in:
deadc0de6
2019-03-29 22:57:58 +01:00
parent d6c4377dec
commit 228524bdf3
2 changed files with 163 additions and 34 deletions

View File

@@ -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)

115
tests-ng/ext-actions.sh Executable file
View File

@@ -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