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

adding profile actions for #141

This commit is contained in:
deadc0de6
2019-06-02 13:50:31 +02:00
parent bebe6f5eae
commit 58dd284118
5 changed files with 145 additions and 3 deletions

View File

@@ -83,13 +83,14 @@ class CfgAggregator:
self._patch_keys_to_objs(self.profiles,
"dotfiles", self.get_dotfile)
# patch action in actions
# patch action in dotfiles actions
self._patch_keys_to_objs(self.dotfiles,
"actions", self._get_action_w_args)
# patch action in profiles actions
self._patch_keys_to_objs(self.profiles,
"actions", self._get_action_w_args)
# patch default actions in settings
# patch actions in settings default_actions
self._patch_keys_to_objs([self.settings],
"default_actions", self._get_action_w_args)
if self.debug:
@@ -254,6 +255,13 @@ class CfgAggregator:
"""return profiles"""
return self.profiles
def get_profile(self, key):
"""return profile by key"""
try:
return next(x for x in self.profiles if x.key == key)
except StopIteration:
return None
def get_dotfiles(self, profile=None):
"""return dotfiles dict for this profile key"""
if not profile:

View File

@@ -101,6 +101,8 @@ def cmd_install(o):
preactions = []
if not o.install_temporary:
preactions.extend(dotfile.get_pre_actions())
prof = o.conf.get_profile(o.profile)
preactions.extend(prof.get_pre_actions())
defactions = o.install_default_actions_pre
pre_actions_exec = action_executor(o, dotfile, preactions,
defactions, t, post=False)
@@ -134,6 +136,8 @@ def cmd_install(o):
if not o.install_temporary:
defactions = o.install_default_actions_post
postactions = dotfile.get_post_actions()
prof = o.conf.get_profile(o.profile)
postactions.extend(prof.get_post_actions())
post_actions_exec = action_executor(o, dotfile, postactions,
defactions, t, post=True)
post_actions_exec()
@@ -145,6 +149,8 @@ def cmd_install(o):
pre_actions_exec()
LOG.dbg('force post pre action execution ...')
postactions = dotfile.get_post_actions()
prof = o.conf.get_profile(o.profile)
postactions.extend(prof.get_post_actions())
post_actions_exec = action_executor(o, dotfile, postactions,
defactions, t, post=True)
post_actions_exec()

View File

@@ -51,7 +51,8 @@ USAGE = """
Usage:
dotdrop install [-VbtfndDa] [-c <path>] [-p <profile>] [<key>...]
dotdrop import [-Vbd] [-c <path>] [-p <profile>] [-l <link>] <path>...
dotdrop import [-Vbd] [-c <path>] [-p <profile>]
[-l <link>] <path>...
dotdrop compare [-Vb] [-c <path>] [-p <profile>]
[-o <opts>] [-C <file>...] [-i <pattern>...]
dotdrop update [-VbfdkP] [-c <path>] [-p <profile>]

View File

@@ -6,6 +6,7 @@ represent a profile in dotdrop
"""
from dotdrop.dictparser import DictParser
from dotdrop.action import Action
class Profile(DictParser):
@@ -27,6 +28,14 @@ class Profile(DictParser):
self.dotfiles = dotfiles
self.variables = variables
def get_pre_actions(self):
"""return all 'pre' actions"""
return [a for a in self.actions if a.kind == Action.pre]
def get_post_actions(self):
"""return all 'post' actions"""
return [a for a in self.actions if a.kind == Action.post]
@classmethod
def _adjust_yaml_keys(cls, value):
"""patch dict"""

118
tests-ng/profile-actions.sh Executable file
View File

@@ -0,0 +1,118 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2019, deadc0de6
#
# test actions per profile
# 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'`
#echo "dotfile destination: ${tmpd}"
# the action temp
tmpa=`mktemp -d --suffix='-dotdrop-tests'`
# create the config file
cfg="${tmps}/config.yaml"
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
actions:
pre:
preaction: echo 'pre' >> ${tmpa}/pre
preaction2: echo 'pre2' >> ${tmpa}/pre2
post:
postaction: echo 'post' >> ${tmpa}/post
postaction2: echo 'post2' >> ${tmpa}/post2
nakedaction: echo 'naked' >> ${tmpa}/naked
dotfiles:
f_abc:
dst: ${tmpd}/abc
src: abc
profiles:
p0:
actions:
- preaction2
- postaction2
- nakedaction
dotfiles:
- f_abc
_EOF
#cat ${cfg}
# create the dotfile
echo "test" > ${tmps}/dotfiles/abc
# install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p0 -V
# check actions executed
[ ! -e ${tmpa}/pre2 ] && echo 'action not executed' && exit 1
[ ! -e ${tmpa}/post2 ] && echo 'action not executed' && exit 1
[ ! -e ${tmpa}/naked ] && echo 'action not executed' && exit 1
grep pre2 ${tmpa}/pre2
grep post2 ${tmpa}/post2
grep naked ${tmpa}/naked
# install again
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p0 -V
# check actions not executed twice
nb=`wc -l ${tmpa}/pre2 | awk '{print $1}'`
[ "${nb}" -gt "1" ] && echo "action executed twice" && exit 1
nb=`wc -l ${tmpa}/post2 | awk '{print $1}'`
[ "${nb}" -gt "1" ] && echo "action executed twice" && exit 1
nb=`wc -l ${tmpa}/naked | awk '{print $1}'`
[ "${nb}" -gt "1" ] && echo "action executed twice" && exit 1
## CLEANING
rm -rf ${tmps} ${tmpd} ${tmpa}
echo "OK"
exit 0