diff --git a/README.md b/README.md index 5132080..217fb0b 100644 --- a/README.md +++ b/README.md @@ -404,8 +404,6 @@ profiles: This way, we make sure [vim-plug](https://github.com/junegunn/vim-plug) is installed prior to deploying the `~/.vimrc` dotfile. -Note that `pre` actions are always executed even if the dotfile is not installed. - You can also define `post` actions like this: ```yaml @@ -418,6 +416,36 @@ If you don't specify neither `post` nor `pre`, the action will be executed after the dotfile deployment (which is equivalent to `post`). Actions cannot obviously be named `pre` or `post`. +Actions can even be parameterized. For example: +```yaml +actions: + echoaction: echo '{0}' > {1} +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_vimrc: + dst: ~/.vimrc + src: vimrc + actions: + - echoaction "vim installed" /tmp/mydotdrop.log + f_xinitrc: + dst: ~/.xinitrc + src: xinitrc + actions: + - echoaction "xinitrc installed" /tmp/myotherlog.log +profiles: + home: + dotfiles: + - f_vimrc + - f_xinitrc +``` + +The above will execute `echo 'vim installed' > /tmp/mydotdrop.log` when +vimrc is installed and `echo 'xinitrc installed' > /tmp/myotherlog.log'` +when xinitrc is installed. + ## Use transformations Transformations are used to transform a dotfile before it is diff --git a/dotdrop/action.py b/dotdrop/action.py index ab103da..391152e 100644 --- a/dotdrop/action.py +++ b/dotdrop/action.py @@ -32,12 +32,23 @@ class Cmd: class Action(Cmd): + def __init__(self, key, action, *args): + super(Action, self).__init__(key, action) + self.args = args + def execute(self): """execute the action in the shell""" ret = 1 - self.log.sub('executing \"{}\"'.format(self.action)) try: - ret = subprocess.call(self.action, shell=True) + cmd = self.action.format(*self.args) + except IndexError: + err = 'bad action: \"{}\"'.format(self.action) + err += ' with \"{}\"'.format(self.args) + self.log.warn(err) + return False + self.log.sub('executing \"{}\"'.format(cmd)) + try: + ret = subprocess.call(cmd, shell=True) except KeyboardInterrupt: self.log.warn('action interrupted') return ret == 0 diff --git a/dotdrop/config.py b/dotdrop/config.py index 7bce422..b1e66e7 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -7,6 +7,7 @@ yaml config file manager import yaml import os +import shlex # local import from dotdrop.dotfile import Dotfile @@ -258,16 +259,29 @@ class Cfg: self.key_actions_pre: [], self.key_actions_post: [], } - for entry in entries: + for line in entries: + fields = shlex.split(line) + entry = fields[0] + args = [] + if len(fields) > 1: + args = fields[1:] action = None if self.key_actions_pre in self.actions and \ entry in self.actions[self.key_actions_pre]: key = self.key_actions_pre - action = self.actions[self.key_actions_pre][entry] + if not args: + action = self.actions[self.key_actions_pre][entry] + else: + a = self.actions[self.key_actions_pre][entry].action + action = Action(key, a, *args) elif self.key_actions_post in self.actions and \ entry in self.actions[self.key_actions_post]: key = self.key_actions_post - action = self.actions[self.key_actions_post][entry] + if not args: + action = self.actions[self.key_actions_post][entry] + else: + a = self.actions[self.key_actions_post][entry].action + action = Action(key, a, *args) else: self.log.warn('unknown action \"{}\"'.format(entry)) continue diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index e087348..d80e58c 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -93,18 +93,14 @@ def install(opts, conf, temporary=False): debug=opts['debug'], totemp=tmpdir) installed = [] for dotfile in dotfiles: + preactions = [] if dotfile.actions and Cfg.key_actions_pre in dotfile.actions: for action in dotfile.actions[Cfg.key_actions_pre]: - if opts['dry']: - LOG.dry('would execute action: {}'.format(action)) - else: - if opts['debug']: - LOG.dbg('executing pre action {}'.format(action)) - action.execute() + preactions.append(action) if opts['debug']: LOG.dbg('installing {}'.format(dotfile)) if hasattr(dotfile, 'link') and dotfile.link: - r = inst.link(t, dotfile.src, dotfile.dst) + r = inst.link(t, dotfile.src, dotfile.dst, actions=preactions) else: src = dotfile.src tmp = None @@ -113,7 +109,7 @@ def install(opts, conf, temporary=False): if not tmp: continue src = tmp - r = inst.install(t, src, dotfile.dst) + r = inst.install(t, src, dotfile.dst, actions=preactions) if tmp: tmp = os.path.join(opts['dotpath'], tmp) if os.path.exists(tmp): diff --git a/dotdrop/installer.py b/dotdrop/installer.py index a3bc11c..a5ab075 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -31,10 +31,12 @@ class Installer: self.diff = diff self.totemp = totemp self.comparing = False + self.action_executed = False self.log = Logger() - def install(self, templater, src, dst): + def install(self, templater, src, dst, actions=[]): """install the src to dst using a template""" + self.action_executed = False src = os.path.join(self.base, os.path.expanduser(src)) if not os.path.exists(src): self.log.err('source dotfile does not exist: {}'.format(src)) @@ -48,30 +50,32 @@ class Installer: if self.debug: self.log.dbg('install {} to {}'.format(src, dst)) if os.path.isdir(src): - return self._handle_dir(templater, src, dst) - return self._handle_file(templater, src, dst) + return self._handle_dir(templater, src, dst, actions=actions) + return self._handle_file(templater, src, dst, actions=actions) - def link(self, templater, src, dst): + def link(self, templater, src, dst, actions=[]): """set src as the link target of dst""" + self.action_executed = False src = os.path.join(self.base, os.path.expanduser(src)) if not os.path.exists(src): self.log.err('source dotfile does not exist: {}'.format(src)) dst = os.path.expanduser(dst) if self.totemp: - return self.install(templater, src, dst) + # ignore actions + return self.install(templater, src, dst, actions=[]) if Templategen.is_template(src): if self.debug: self.log.dbg('dotfile is a template') self.log.dbg('install to {} and symlink'.format(self.workdir)) tmp = self._pivot_path(dst, self.workdir, striphome=True) - i = self.install(templater, src, tmp) + i = self.install(templater, src, tmp, actions=actions) if not i and not os.path.exists(tmp): return [] src = tmp - return self._link(src, dst) + return self._link(src, dst, actions=actions) - def _link(self, src, dst): + def _link(self, src, dst, actions=[]): """set src as a link target of dst""" if os.path.lexists(dst): if os.path.realpath(dst) == os.path.realpath(src): @@ -98,11 +102,12 @@ class Installer: if not self._create_dirs(base): self.log.err('creating directory for \"{}\"'.format(dst)) return [] + self._exec_pre_actions(actions) os.symlink(src, dst) self.log.sub('linked \"{}\" to \"{}\"'.format(dst, src)) return [(src, dst)] - def _handle_file(self, templater, src, dst): + def _handle_file(self, templater, src, dst, actions=[]): """install src to dst when is a file""" if self.debug: self.log.dbg('generate template for {}'.format(src)) @@ -118,7 +123,7 @@ class Installer: self.log.err('source dotfile does not exist: \"{}\"'.format(src)) return [] st = os.stat(src) - ret = self._write(dst, content, st.st_mode) + ret = self._write(dst, content, st.st_mode, actions=actions) if ret < 0: self.log.err('installing \"{}\" to \"{}\"'.format(src, dst)) return [] @@ -132,18 +137,21 @@ class Installer: return [(src, dst)] return [] - def _handle_dir(self, templater, src, dst): + def _handle_dir(self, templater, src, dst, actions=[]): """install src to dst when is a directory""" ret = [] - self._create_dirs(dst) + if not self._create_dirs(dst): + return [] # handle all files in dir for entry in os.listdir(src): f = os.path.join(src, entry) if not os.path.isdir(f): - res = self._handle_file(templater, f, os.path.join(dst, entry)) + res = self._handle_file(templater, f, os.path.join(dst, entry), + actions=actions) ret.extend(res) else: - res = self._handle_dir(templater, f, os.path.join(dst, entry)) + res = self._handle_dir(templater, f, os.path.join(dst, entry), + actions=actions) ret.extend(res) return ret @@ -154,7 +162,7 @@ class Installer: cur = f.read() return cur == content - def _write(self, dst, content, rights): + def _write(self, dst, content, rights, actions=[]): """write content to file return 0 for success, 1 when already exists @@ -179,6 +187,7 @@ class Installer: return -1 if self.debug: self.log.dbg('write content to {}'.format(dst)) + self._exec_pre_actions(actions) try: with open(dst, 'wb') as f: f.write(content) @@ -218,6 +227,19 @@ class Installer: sub = path.lstrip(os.sep) return os.path.join(newdir, sub) + def _exec_pre_actions(self, actions): + """execute pre-actions if any""" + if self.action_executed: + return + for action in actions: + if self.dry: + self.log.dry('would execute action: {}'.format(action)) + else: + if self.debug: + self.log.dbg('executing pre action {}'.format(action)) + action.execute() + self.action_executed = True + def _install_to_temp(self, templater, src, dst, tmpdir): """install a dotfile to a tempdir""" tmpdst = self._pivot_path(dst, tmpdir) diff --git a/tests-ng/actions-args.sh b/tests-ng/actions-args.sh new file mode 100755 index 0000000..a02eda1 --- /dev/null +++ b/tests-ng/actions-args.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test pre/post/naked actions with arguments +# 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 "RUNNING $(basename $BASH_SOURCE)" + +################################################################ +# this is the test +################################################################ + +# the action temp +tmpa=`mktemp -d` +# the dotfile source +tmps=`mktemp -d` +mkdir -p ${tmps}/dotfiles +# the dotfile destination +tmpd=`mktemp -d` + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +actions: + pre: + preaction: echo '{0} {1}' > ${tmpa}/pre + post: + postaction: echo '{0} {1} {2}' > ${tmpa}/post + nakedaction: echo '{0}' > ${tmpa}/naked + emptyaction: echo 'empty' > ${tmpa}/empty + tgtaction: echo 'tgt' > ${tmpa}/{0} +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc + actions: + - preaction test1 test2 + - postaction test3 test4 test5 + - nakedaction "test6 something" + - emptyaction + - tgtaction tgt +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 ] && echo "pre arg action not found" && exit 1 +grep test1 ${tmpa}/pre >/dev/null +grep test2 ${tmpa}/pre >/dev/null + +[ ! -e ${tmpa}/post ] && echo "post arg action not found" && exit 1 +grep test3 ${tmpa}/post >/dev/null +grep test4 ${tmpa}/post >/dev/null +grep test5 ${tmpa}/post >/dev/null + +[ ! -e ${tmpa}/naked ] && echo "naked arg action not found" && exit 1 +grep "test6 something" ${tmpa}/naked >/dev/null + +[ ! -e ${tmpa}/empty ] && echo "empty arg action not found" && exit 1 +grep empty ${tmpa}/empty >/dev/null + +[ ! -e ${tmpa}/tgt ] && echo "tgt arg action not found" && exit 1 +grep tgt ${tmpa}/tgt >/dev/null + +## CLEANING +rm -rf ${tmps} ${tmpd} ${tmpa} + +echo "OK" +exit 0 diff --git a/tests-ng/actions-pre.sh b/tests-ng/actions-pre.sh new file mode 100755 index 0000000..79a3ced --- /dev/null +++ b/tests-ng/actions-pre.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test pre action execution +# 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 "RUNNING $(basename $BASH_SOURCE)" + +################################################################ +# this is the test +################################################################ + +# the action temp +tmpa=`mktemp -d` +# the dotfile source +tmps=`mktemp -d` +mkdir -p ${tmps}/dotfiles +# the dotfile destination +tmpd=`mktemp -d` + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +actions: + pre: + preaction: echo 'pre' > ${tmpa}/pre + preaction2: echo 'pre2' > ${tmpa}/pre2 + preaction3: echo 'pre3' > ${tmpa}/pre3 + multiple: echo 'multiple' >> ${tmpa}/multiple + multiple2: echo 'multiple2' >> ${tmpa}/multiple2 + nakedaction: echo 'naked' > ${tmpa}/naked + nakedaction2: echo 'naked2' > ${tmpa}/naked2 + nakedaction3: echo 'naked3' > ${tmpa}/naked3 +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc + actions: + - preaction + - nakedaction + f_link: + dst: ${tmpd}/link + src: link + link: true + actions: + - preaction2 + - nakedaction2 + d_dir: + dst: ${tmpd}/dir + src: dir + actions: + - multiple + d_dlink: + dst: ${tmpd}/dlink + src: dlink + link: true + actions: + - preaction3 + - nakedaction3 + - multiple2 +profiles: + p1: + dotfiles: + - f_abc + - f_link + - d_dir + - d_dlink +_EOF +cat ${cfg} + +# create the dotfile +echo 'test' > ${tmps}/dotfiles/abc +echo 'link' > ${tmps}/dotfiles/link + +mkdir -p ${tmps}/dotfiles/dir +echo 'test1' > ${tmps}/dotfiles/dir/file1 +echo 'test2' > ${tmps}/dotfiles/dir/file2 + +mkdir -p ${tmps}/dotfiles/dlink +echo 'test3' > ${tmps}/dotfiles/dlink/dfile1 +echo 'test4' > ${tmps}/dotfiles/dlink/dfile2 + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V + +# checks +[ ! -e ${tmpa}/pre ] && echo 'pre action not executed' && exit 1 +grep pre ${tmpa}/pre >/dev/null +[ ! -e ${tmpa}/naked ] && echo 'naked action not executed' && exit 1 +grep naked ${tmpa}/naked >/dev/null + +[ ! -e ${tmpa}/multiple ] && echo 'pre action multiple not executed' && exit 1 +grep multiple ${tmpa}/multiple >/dev/null +[ "`wc -l ${tmpa}/multiple | awk '{print $1}'`" -gt "1" ] && echo 'pre action multiple executed twice' && exit 1 + +[ ! -e ${tmpa}/pre2 ] && echo 'pre action 2 not executed' && exit 1 +grep pre2 ${tmpa}/pre2 >/dev/null +[ ! -e ${tmpa}/naked2 ] && echo 'naked action 2 not executed' && exit 1 +grep naked2 ${tmpa}/naked2 >/dev/null + +[ ! -e ${tmpa}/multiple2 ] && echo 'pre action multiple 2 not executed' && exit 1 +grep multiple2 ${tmpa}/multiple2 >/dev/null +[ "`wc -l ${tmpa}/multiple2 | awk '{print $1}'`" -gt "1" ] && echo 'pre action multiple 2 executed twice' && exit 1 +[ ! -e ${tmpa}/naked3 ] && echo 'naked action 3 not executed' && exit 1 +grep naked3 ${tmpa}/naked3 >/dev/null + + +# remove the pre action result and re-run +rm ${tmpa}/pre + +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 +[ -e ${tmpa}/pre ] && exit 1 + +## CLEANING +rm -rf ${tmps} ${tmpd} ${tmpa} + +echo "OK" +exit 0