From bebe6f5eaee2d938721eabbc26582b73b0ee1934 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sun, 2 Jun 2019 13:31:36 +0200 Subject: [PATCH] keep order of included profiles for #149 --- dotdrop/cfg_yaml.py | 7 +- dotdrop/comparator.py | 2 +- dotdrop/dotdrop.py | 8 ++- dotdrop/utils.py | 9 +++ tests-ng/include-order.sh | 142 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 6 deletions(-) create mode 100755 tests-ng/include-order.sh diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index cb7267b..4199391 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -14,7 +14,7 @@ from dotdrop.settings import Settings from dotdrop.logger import Logger from dotdrop.templategen import Templategen from dotdrop.linktypes import LinkTypes -from dotdrop.utils import shell +from dotdrop.utils import shell, uniq_list class CfgYaml: @@ -490,7 +490,10 @@ class CfgYaml: others.extend(self._rec_resolve_profile_include(i)) current.extend(others) # unique them - values[self.key_profiles_dotfiles] = list(set(current)) + values[self.key_profiles_dotfiles] = uniq_list(current) + if self.debug: + dfs = values[self.key_profiles_dotfiles] + self.log.dbg('profile dfs after include: {}'.format(dfs)) return values.get(self.key_profiles_dotfiles, []) def _resolve_path(self, path): diff --git a/dotdrop/comparator.py b/dotdrop/comparator.py index 66a0ae2..992674d 100644 --- a/dotdrop/comparator.py +++ b/dotdrop/comparator.py @@ -95,7 +95,7 @@ class Comparator: # content is different funny = comp.diff_files funny.extend(comp.funny_files) - funny = list(set(funny)) + funny = utils.uniq_list(funny) for i in funny: lfile = os.path.join(left, i) rfile = os.path.join(right, i) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 497caa0..d90eab3 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -15,7 +15,7 @@ from dotdrop.templategen import Templategen from dotdrop.installer import Installer from dotdrop.updater import Updater from dotdrop.comparator import Comparator -from dotdrop.utils import get_tmpdir, remove, strip_home, run +from dotdrop.utils import get_tmpdir, remove, strip_home, run, uniq_list from dotdrop.linktypes import LinkTypes LOG = Logger() @@ -71,7 +71,8 @@ def cmd_install(o): dotfiles = o.dotfiles if o.install_keys: # filtered dotfiles to install - dotfiles = [d for d in dotfiles if d.key in set(o.install_keys)] + uniq = uniq_list(o.install_keys) + dotfiles = [d for d in dotfiles if d.key in uniq] if not dotfiles: msg = 'no dotfile to install for this profile (\"{}\")' LOG.warn(msg.format(o.profile)) @@ -388,7 +389,8 @@ def cmd_detail(o): dotfiles = o.dotfiles if o.detail_keys: # filtered dotfiles to install - dotfiles = [d for d in dotfiles if d.key in set(o.details_keys)] + uniq = uniq_list(o.details_keys) + dotfiles = [d for d in dotfiles if d.key in uniq] LOG.emph('dotfiles details for profile \"{}\":\n'.format(o.profile)) for d in dotfiles: _detail(o.dotpath, d) diff --git a/dotdrop/utils.py b/dotdrop/utils.py index 88dbf75..2954a96 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -130,3 +130,12 @@ def must_ignore(paths, ignores, debug=False): LOG.dbg('ignore \"{}\" match: {}'.format(i, p)) return True return False + + +def uniq_list(a_list): + """unique elements of a list while preserving order""" + new = [] + for a in a_list: + if a not in new: + new.append(a) + return new diff --git a/tests-ng/include-order.sh b/tests-ng/include-order.sh new file mode 100755 index 0000000..eca2187 --- /dev/null +++ b/tests-ng/include-order.sh @@ -0,0 +1,142 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2019, deadc0de6 +# +# test the use of the keyword "include" +# that has to be ordered +# 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'` +# temporary +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: + first: 'echo first > ${tmpa}/cookie' + second: 'echo second >> ${tmpa}/cookie' + third: 'echo third >> ${tmpa}/cookie' +dotfiles: + f_first: + dst: ${tmpd}/first + src: first + actions: + - first + f_second: + dst: ${tmpd}/second + src: second + actions: + - second + f_third: + dst: ${tmpd}/third + src: third + actions: + - third +profiles: + p0: + dotfiles: + - f_first + include: + - second + - third + second: + dotfiles: + - f_second + third: + dotfiles: + - f_third +_EOF + +# create the source +mkdir -p ${tmps}/dotfiles/ +echo "first" > ${tmps}/dotfiles/first +echo "second" > ${tmps}/dotfiles/second +echo "third" > ${tmps}/dotfiles/third + +attempts="3" +for ((i=0;i<${attempts};i++)); do + # install + cd ${ddpath} | ${bin} install -f -c ${cfg} -p p0 -V + + # checks timestamp + echo "first timestamp: `stat -c %y ${tmpd}/first`" + echo "second timestamp: `stat -c %y ${tmpd}/second`" + echo "third timestamp: `stat -c %y ${tmpd}/third`" + + ts_first=`date "+%S%N" -d "$(stat -c %y ${tmpd}/first)"` + ts_second=`date "+%S%N" -d "$(stat -c %y ${tmpd}/second)"` + ts_third=`date "+%S%N" -d "$(stat -c %y ${tmpd}/third)"` + + #echo "first ts: ${ts_first}" + #echo "second ts: ${ts_second}" + #echo "third ts: ${ts_third}" + + [ "${ts_first}" -ge "${ts_second}" ] && echo "second created before first" && exit 1 + [ "${ts_second}" -ge "${ts_third}" ] && echo "third created before second" && exit 1 + + # check cookie + cat ${tmpa}/cookie + content=`cat ${tmpa}/cookie | xargs` + [ "${content}" != "first second third" ] && echo "bad cookie" && exit 1 + + # clean + rm ${tmpa}/cookie + rm ${tmpd}/first ${tmpd}/second ${tmpd}/third +done + +## CLEANING +rm -rf ${tmps} ${tmpd} ${tmpa} + +echo "OK" +exit 0