From 622fe71f065c93984a9824f757f33b618109ed93 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 20:09:39 +0200 Subject: [PATCH 01/18] action with arguments for #59 --- dotdrop/action.py | 15 +++++- dotdrop/config.py | 19 +++++-- tests-ng/actions-args.sh | 113 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 5 deletions(-) create mode 100755 tests-ng/actions-args.sh 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..aa2435a 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -258,16 +258,29 @@ class Cfg: self.key_actions_pre: [], self.key_actions_post: [], } - for entry in entries: + for line in entries: + fields = line.split(' ') + 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/tests-ng/actions-args.sh b/tests-ng/actions-args.sh new file mode 100755 index 0000000..106bd8e --- /dev/null +++ b/tests-ng/actions-args.sh @@ -0,0 +1,113 @@ +#!/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 +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 + - emptyaction +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 ${tmpa}/naked >/dev/null + +[ ! -e ${tmpa}/empty ] && echo "empty arg action not found" && exit 1 +grep empty ${tmpa}/empty >/dev/null + +## CLEANING +rm -rf ${tmps} ${tmpd} ${tmpa} + +echo "OK" +exit 0 From f5c9d494e7b792b9a7b7f39eff7aa7c2df80cb63 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 20:23:48 +0200 Subject: [PATCH 02/18] correctly split action arguments --- dotdrop/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotdrop/config.py b/dotdrop/config.py index aa2435a..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 @@ -259,7 +260,7 @@ class Cfg: self.key_actions_post: [], } for line in entries: - fields = line.split(' ') + fields = shlex.split(line) entry = fields[0] args = [] if len(fields) > 1: From cfd6cc4e72e65ca8668f49c350cc68c907d4ad56 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 20:23:55 +0200 Subject: [PATCH 03/18] add more tests --- tests-ng/actions-args.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests-ng/actions-args.sh b/tests-ng/actions-args.sh index 106bd8e..a02eda1 100755 --- a/tests-ng/actions-args.sh +++ b/tests-ng/actions-args.sh @@ -64,6 +64,7 @@ actions: 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 @@ -75,8 +76,9 @@ dotfiles: actions: - preaction test1 test2 - postaction test3 test4 test5 - - nakedaction test6 + - nakedaction "test6 something" - emptyaction + - tgtaction tgt profiles: p1: dotfiles: @@ -101,11 +103,14 @@ 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 ${tmpa}/naked >/dev/null +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} From 689a348d352e098218ca1d649c80de22e35dd984 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 20:24:01 +0200 Subject: [PATCH 04/18] update readme --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index d3cf0e8..be16bbd 100644 --- a/README.md +++ b/README.md @@ -418,6 +418,31 @@ 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`. +Action can even be parameterized. You can define an action taking one or more +arguments which will be replaced depending on its use on different dotfiles. +For example: +```bash +actions: + echoaction: echo '{0}' >> {1} +... +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 +... +``` + +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 From 9b84c4e828df17163113f206dd89cdf0872fcc7a Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 20:27:35 +0200 Subject: [PATCH 05/18] update readme --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index be16bbd..4f46ea1 100644 --- a/README.md +++ b/README.md @@ -418,13 +418,14 @@ 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`. -Action can even be parameterized. You can define an action taking one or more -arguments which will be replaced depending on its use on different dotfiles. -For example: -```bash +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 @@ -436,7 +437,11 @@ dotfiles: 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 From c58ebe7bb3ec46fc63824f24a5b87494a25b9a77 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 20:29:54 +0200 Subject: [PATCH 06/18] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f46ea1..489e76d 100644 --- a/README.md +++ b/README.md @@ -421,7 +421,7 @@ Actions cannot obviously be named `pre` or `post`. Actions can even be parameterized. For example: ```yaml actions: - echoaction: echo '{0}' >> {1} + echoaction: echo '{0}' > {1} config: backup: true create: true From d36e525ca4767d97ef75ecf3c620a96cdd3606b5 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 22:02:36 +0200 Subject: [PATCH 07/18] ensure source dotfile exists --- dotdrop/comparator.py | 2 ++ dotdrop/installer.py | 4 ++++ dotdrop/templategen.py | 2 ++ 3 files changed, 8 insertions(+) diff --git a/dotdrop/comparator.py b/dotdrop/comparator.py index b516408..27cfa5f 100644 --- a/dotdrop/comparator.py +++ b/dotdrop/comparator.py @@ -43,6 +43,8 @@ class Comparator: def _comp_dir(self, left, right, ignore): """compare a directory""" + if not os.path.exists(right): + return '' if self._ignore([left, right], ignore): if self.debug: self.log.dbg('ignoring diff {} and {}'.format(left, right)) diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 022dffb..132ac6a 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -36,6 +36,8 @@ class Installer: def install(self, templater, src, dst): """install the src to dst using a template""" 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: dst = self._pivot_path(dst, self.totemp) @@ -52,6 +54,8 @@ class Installer: def link(self, templater, src, dst): """set src as the link target of dst""" 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) diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index 67408eb..af422bc 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -93,6 +93,8 @@ class Templategen: def is_template(path): """recursively check if any file is a template within path""" + if not os.path.exists(path): + return False if os.path.isfile(path): # is file return Templategen._is_template(path) From 5527f61b6639e6ed299a38c67c8ca0d59d2c7f97 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 22:12:55 +0200 Subject: [PATCH 08/18] properly handle binary when checking for template #58 --- dotdrop/templategen.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index af422bc..06138d3 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -114,8 +114,12 @@ class Templategen: """test if file pointed by path is a template""" if not os.path.isfile(path): return False - with open(path, 'r') as f: - data = f.read() + try: + with open(path, 'r') as f: + data = f.read() + except UnicodeDecodeError: + # is binary so surely no template + return False markers = [BLOCK_START, VAR_START, COMMENT_START] for marker in markers: if marker in data: From 5fc53333f58ce9dc2b1c7128ee93384385dd3c47 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 22:19:25 +0200 Subject: [PATCH 09/18] now pre action are only executed if dotfile is installed (#59) --- dotdrop/dotdrop.py | 7 +-- dotdrop/installer.py | 40 ++++++++++------ tests-ng/actions-pre.sh | 104 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 18 deletions(-) create mode 100755 tests-ng/actions-pre.sh diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index e087348..217670d 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -93,6 +93,7 @@ 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']: @@ -100,11 +101,11 @@ def install(opts, conf, temporary=False): 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 +114,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 022dffb..246e347 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -33,7 +33,7 @@ class Installer: self.comparing = 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""" src = os.path.join(self.base, os.path.expanduser(src)) dst = os.path.expanduser(dst) @@ -46,27 +46,27 @@ 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""" src = os.path.join(self.base, os.path.expanduser(src)) dst = os.path.expanduser(dst) if self.totemp: - return self.install(templater, src, dst) + return self.install(templater, src, dst, actions=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) - if not self.install(templater, src, tmp): + if not self.install(templater, src, tmp, actions=actions): 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): @@ -93,11 +93,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)) @@ -113,7 +114,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 [] @@ -127,18 +128,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 @@ -149,7 +153,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 @@ -174,6 +178,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) @@ -213,6 +218,11 @@ class Installer: sub = path.lstrip(os.sep) return os.path.join(newdir, sub) + def _exec_pre_actions(self, actions): + """execute pre-actions if any""" + for action in actions: + action.execute() + 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-pre.sh b/tests-ng/actions-pre.sh new file mode 100755 index 0000000..41f40e2 --- /dev/null +++ b/tests-ng/actions-pre.sh @@ -0,0 +1,104 @@ +#!/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 + nakedaction: echo 'naked' > ${tmpa}/naked +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc + actions: + - preaction + - nakedaction +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}/naked ] && exit 1 +grep naked ${tmpa}/naked >/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 From d86567a861ba4c53d245cc022680a9f60f1ab18a Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 22:34:30 +0200 Subject: [PATCH 10/18] force symlink re-creation when moving from template to non-template and vice-versa --- dotdrop/installer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 022dffb..6e5385d 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -61,7 +61,8 @@ class Installer: 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) - if not self.install(templater, src, tmp): + i = self.install(templater, src, tmp) + if not i and not os.path.exists(tmp): return [] src = tmp return self._link(src, dst) From 67ae4bec8b853bcc4bab43f04a925347dc639420 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 22:39:51 +0200 Subject: [PATCH 11/18] update readme --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 489e76d..19fb610 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 From 61eb611dc0397116746ef67039c55191fd3528f5 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Sep 2018 23:01:03 +0200 Subject: [PATCH 12/18] update readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index d3cf0e8..5132080 100644 --- a/README.md +++ b/README.md @@ -547,6 +547,15 @@ When using templating directives, the dotfiles are first installed into `workdir` (defaults to *~/.config/dotdrop*, see [Config](#config)) and then symlinked there. +For example +```bash +# with template +/home/user/.xyz -> /home/user/.config/dotdrop/.xyz + +# without template +/home/user/.xyz -> /home/user/dotdrop/dotfiles/xyz +``` + # Config The config file (defaults to *config.yaml*) is a yaml file containing From ef9718165e647fdfc179564adae869b2172945e1 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 07:25:58 +0200 Subject: [PATCH 13/18] ensure pre action are only executed once --- dotdrop/dotdrop.py | 7 +------ dotdrop/installer.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 217670d..d80e58c 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -96,12 +96,7 @@ def install(opts, conf, temporary=False): 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)) - preactions.append(action) + preactions.append(action) if opts['debug']: LOG.dbg('installing {}'.format(dotfile)) if hasattr(dotfile, 'link') and dotfile.link: diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 246e347..63fa09c 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, actions=[]): """install the src to dst using a template""" + self.action_executed = False src = os.path.join(self.base, os.path.expanduser(src)) dst = os.path.expanduser(dst) if self.totemp: @@ -51,10 +53,12 @@ class Installer: 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)) dst = os.path.expanduser(dst) if self.totemp: - return self.install(templater, src, dst, actions=actions) + # ignore actions + return self.install(templater, src, dst, actions=[]) if Templategen.is_template(src): if self.debug: @@ -220,8 +224,16 @@ class Installer: def _exec_pre_actions(self, actions): """execute pre-actions if any""" + if self.action_executed: + return for action in actions: - action.execute() + 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""" From ef58a44fc0da7b0f7c4e943ba26b7109d413ab3b Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 07:26:06 +0200 Subject: [PATCH 14/18] adding more tests --- tests-ng/actions-pre.sh | 62 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/tests-ng/actions-pre.sh b/tests-ng/actions-pre.sh index 41f40e2..79a3ced 100755 --- a/tests-ng/actions-pre.sh +++ b/tests-ng/actions-pre.sh @@ -60,7 +60,13 @@ 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 @@ -72,25 +78,73 @@ dotfiles: 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 '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 +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V # checks -[ ! -e ${tmpa}/pre ] && exit 1 +[ ! -e ${tmpa}/pre ] && echo 'pre action not executed' && exit 1 grep pre ${tmpa}/pre >/dev/null -[ ! -e ${tmpa}/naked ] && exit 1 +[ ! -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 From a39dd4704e117df04888a42df29263320006a9b9 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 08:23:32 +0200 Subject: [PATCH 15/18] add more tests --- tests/test_install.py | 10 ++++- tests/test_update.py | 92 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/test_update.py diff --git a/tests/test_install.py b/tests/test_install.py index 29dd263..02103f0 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -93,6 +93,13 @@ exec bspwm dst3 = os.path.join(dst, get_string(6)) d3 = Dotfile(get_string(5), dst3, os.path.basename(f3)) + # create a directory dotfile + dir1 = os.path.join(tmp, 'somedir') + create_dir(dir1) + fd, _ = create_random_file(dir1) + dstd = os.path.join(dst, get_string(6)) + ddot = Dotfile(get_string(5), dstd, os.path.basename(dir1)) + # to test backup f4, c4 = create_random_file(tmp) dst4 = os.path.join(dst, get_string(6)) @@ -152,7 +159,7 @@ exec bspwm # generate the config and stuff profile = get_string(5) confpath = os.path.join(tmp, self.CONFIG_NAME) - self.fake_config(confpath, [d1, d2, d3, d4, d5, d6, d7, d8, d9], + self.fake_config(confpath, [d1, d2, d3, d4, d5, d6, d7, d8, d9, ddot], profile, tmp, [act1], [tr]) conf = Cfg(confpath) self.assertTrue(conf is not None) @@ -172,6 +179,7 @@ exec bspwm self.assertTrue(os.path.exists(dst6)) self.assertTrue(os.path.exists(dst7)) self.assertTrue(os.path.exists(dst8)) + self.assertTrue(os.path.exists(fd)) # check if 'dst5' is a link whose target is 'f5' self.assertTrue(os.path.islink(dst5)) diff --git a/tests/test_update.py b/tests/test_update.py new file mode 100644 index 0000000..06b0343 --- /dev/null +++ b/tests/test_update.py @@ -0,0 +1,92 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2017, deadc0de6 +basic unittest for the update function +""" + + +import unittest +import os +import yaml + +from dotdrop.config import Cfg +from dotdrop.dotdrop import importer +from dotdrop.dotdrop import update +from dotdrop.dotfile import Dotfile + +from tests.helpers import * + + +class TestUpdate(unittest.TestCase): + + CONFIG_BACKUP = False + CONFIG_CREATE = True + CONFIG_DOTPATH = 'dotfiles' + CONFIG_NAME = 'config.yaml' + + def edit_content(self, path, newcontent, binary=False): + mode = 'w' + if binary: + mode = 'wb' + with open(path, mode) as f: + f.write(newcontent) + + def test_update(self): + '''Test the update function''' + # setup some directories + fold_config = os.path.join(os.path.expanduser('~'), '.config') + create_dir(fold_config) + fold_subcfg = os.path.join(os.path.expanduser('~'), '.config', + get_string(5)) + create_dir(fold_subcfg) + self.addCleanup(clean, fold_subcfg) + fold_tmp = get_tempdir() + create_dir(fold_tmp) + self.addCleanup(clean, fold_tmp) + + # create the directories + tmp = get_tempdir() + self.assertTrue(os.path.exists(tmp)) + self.addCleanup(clean, tmp) + + dotfilespath = get_tempdir() + self.assertTrue(os.path.exists(dotfilespath)) + self.addCleanup(clean, dotfilespath) + + # create the dotfiles to test + d1, c1 = create_random_file(fold_config) + self.assertTrue(os.path.exists(d1)) + self.addCleanup(clean, d1) + + # create the config file + profile = get_string(5) + confpath = create_fake_config(dotfilespath, + configname=self.CONFIG_NAME, + dotpath=self.CONFIG_DOTPATH, + backup=self.CONFIG_BACKUP, + create=self.CONFIG_CREATE) + self.assertTrue(os.path.exists(confpath)) + conf, opts = load_config(confpath, profile) + dfiles = [d1] + + # import the files + importer(opts, conf, dfiles) + conf, opts = load_config(confpath, profile) + + # edit the file + self.edit_content(d1, 'newcontent') + + # update it + update(opts, conf, d1) + + # test content + newcontent = open(d1, 'r').read() + self.assertTrue(newcontent == 'newcontent') + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() From 8d60e0ca5c0a91f0513f7e8ef635b783599ffaac Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 08:35:57 +0200 Subject: [PATCH 16/18] more test for update on directory --- tests/test_update.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/test_update.py b/tests/test_update.py index 06b0343..3a226b8 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -58,6 +58,11 @@ class TestUpdate(unittest.TestCase): self.assertTrue(os.path.exists(d1)) self.addCleanup(clean, d1) + # create the directory to test + dpath = os.path.join(fold_config, get_string(5)) + dir1 = create_dir(dpath) + dirf1, _ = create_random_file(dpath) + # create the config file profile = get_string(5) confpath = create_fake_config(dotfilespath, @@ -67,21 +72,29 @@ class TestUpdate(unittest.TestCase): create=self.CONFIG_CREATE) self.assertTrue(os.path.exists(confpath)) conf, opts = load_config(confpath, profile) - dfiles = [d1] + dfiles = [d1, dir1] # import the files importer(opts, conf, dfiles) conf, opts = load_config(confpath, profile) - # edit the file + # edit the files self.edit_content(d1, 'newcontent') + self.edit_content(dirf1, 'newcontent') + + # add more file + dirf2, _ = create_random_file(dpath) # update it - update(opts, conf, d1) + opts['safe'] = False + opts['debug'] = True + update(opts, conf, [d1, dir1]) # test content newcontent = open(d1, 'r').read() self.assertTrue(newcontent == 'newcontent') + newcontent = open(dirf1, 'r').read() + self.assertTrue(newcontent == 'newcontent') def main(): From 3819511e725c375533197ea75fb757071b483098 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 08:51:56 +0200 Subject: [PATCH 17/18] update tests and improve --- tests/helpers.py | 9 +++++++++ tests/test_compare.py | 18 ++++++------------ tests/test_import.py | 3 +-- tests/test_install.py | 14 +++++++++++++- tests/test_update.py | 16 +++++++--------- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index 293777d..4ebec85 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -55,6 +55,15 @@ def create_random_file(directory, content=None, binary=False): return path, content +def edit_content(path, newcontent, binary=False): + '''edit file content''' + mode = 'w' + if binary: + mode = 'wb' + with open(path, mode) as f: + f.write(newcontent) + + def create_dir(path): '''Create a directory''' if not os.path.exists(path): diff --git a/tests/test_compare.py b/tests/test_compare.py index 649f47a..3e04ed2 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -41,7 +41,8 @@ class TestCompare(unittest.TestCase): if not ret: results[path] = False continue - diff = comp.compare(insttmp, dotfile.dst) + diff = comp.compare(insttmp, dotfile.dst, + ignore=['whatever', 'whatelse']) print('XXXX diff for {} and {}:\n{}'.format(dotfile.src, dotfile.dst, diff)) @@ -49,13 +50,6 @@ class TestCompare(unittest.TestCase): results[path] = diff == '' return results - def edit_content(self, path, newcontent, binary=False): - mode = 'w' - if binary: - mode = 'wb' - with open(path, mode) as f: - f.write(newcontent) - def test_compare(self): '''Test the compare function''' # setup some directories @@ -118,13 +112,13 @@ class TestCompare(unittest.TestCase): self.assertTrue(results == expected) # modify file - self.edit_content(d1, get_string(20)) + edit_content(d1, get_string(20)) expected = {d1: False, d2: True, d3: True, d4: True, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) # modify binary file - self.edit_content(d4, bytes(get_string(20), 'ascii'), binary=True) + edit_content(d4, bytes(get_string(20), 'ascii'), binary=True) expected = {d1: False, d2: True, d3: True, d4: False, d5: True} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) @@ -137,8 +131,8 @@ class TestCompare(unittest.TestCase): self.assertTrue(results == expected) # modify all files - self.edit_content(d2, get_string(20)) - self.edit_content(d3, get_string(21)) + edit_content(d2, get_string(20)) + edit_content(d3, get_string(21)) expected = {d1: False, d2: False, d3: False, d4: False, d5: False} results = self.compare(opts, conf, tmp, len(dfiles)) self.assertTrue(results == expected) diff --git a/tests/test_import.py b/tests/test_import.py index 0c35f36..75b0edc 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -199,8 +199,7 @@ class TestImport(unittest.TestCase): # fake test update editcontent = 'edited' - with open(dotfile1, 'w') as f: - f.write('edited') + edit_content(dotfile1, editcontent) opts['safe'] = False update(opts, conf, [dotfile1]) c2 = open(indt1, 'r').read() diff --git a/tests/test_install.py b/tests/test_install.py index 02103f0..3c7145a 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -156,10 +156,16 @@ exec bspwm dst9 = os.path.join(dst, get_string(6)) d9 = Dotfile(get_string(6), dst9, os.path.basename(f9), trans=[tr]) + # to test template + f10, _ = create_random_file(tmp, content='{{@@ profile @@}}') + dst10 = os.path.join(dst, get_string(6)) + d10 = Dotfile(get_string(6), dst10, os.path.basename(f10)) + # generate the config and stuff profile = get_string(5) confpath = os.path.join(tmp, self.CONFIG_NAME) - self.fake_config(confpath, [d1, d2, d3, d4, d5, d6, d7, d8, d9, ddot], + dotfiles = [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, ddot] + self.fake_config(confpath, dotfiles, profile, tmp, [act1], [tr]) conf = Cfg(confpath) self.assertTrue(conf is not None) @@ -179,6 +185,7 @@ exec bspwm self.assertTrue(os.path.exists(dst6)) self.assertTrue(os.path.exists(dst7)) self.assertTrue(os.path.exists(dst8)) + self.assertTrue(os.path.exists(dst10)) self.assertTrue(os.path.exists(fd)) # check if 'dst5' is a link whose target is 'f5' @@ -209,6 +216,11 @@ exec bspwm transcontent = open(dst9, 'r').read().rstrip() self.assertTrue(transcontent == trans2) + # test template has been remplaced + self.assertTrue(os.path.exists(dst10)) + tempcontent = open(dst10, 'r').read().rstrip() + self.assertTrue(tempcontent == profile) + def main(): unittest.main() diff --git a/tests/test_update.py b/tests/test_update.py index 3a226b8..6f7dfc6 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -24,13 +24,6 @@ class TestUpdate(unittest.TestCase): CONFIG_DOTPATH = 'dotfiles' CONFIG_NAME = 'config.yaml' - def edit_content(self, path, newcontent, binary=False): - mode = 'w' - if binary: - mode = 'wb' - with open(path, mode) as f: - f.write(newcontent) - def test_update(self): '''Test the update function''' # setup some directories @@ -79,12 +72,17 @@ class TestUpdate(unittest.TestCase): conf, opts = load_config(confpath, profile) # edit the files - self.edit_content(d1, 'newcontent') - self.edit_content(dirf1, 'newcontent') + edit_content(d1, 'newcontent') + edit_content(dirf1, 'newcontent') # add more file dirf2, _ = create_random_file(dpath) + # add more dirs + dpath = os.path.join(dpath, get_string(5)) + create_dir(dpath) + create_random_file(dpath) + # update it opts['safe'] = False opts['debug'] = True From 91b02996fb8e78ac99354d693b36ab225c1e1afc Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 25 Sep 2018 10:02:05 +0200 Subject: [PATCH 18/18] bump version --- dotdrop/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotdrop/version.py b/dotdrop/version.py index e5222c4..c38037e 100644 --- a/dotdrop/version.py +++ b/dotdrop/version.py @@ -3,4 +3,4 @@ author: deadc0de6 (https://github.com/deadc0de6) Copyright (c) 2018, deadc0de6 """ -__version__ = '0.20.3' +__version__ = '0.21.0'