1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-09 18:44:16 +00:00

keep order of included profiles for #149

This commit is contained in:
deadc0de6
2019-06-02 13:31:36 +02:00
parent 8af57adfab
commit bebe6f5eae
5 changed files with 162 additions and 6 deletions

View File

@@ -14,7 +14,7 @@ from dotdrop.settings import Settings
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.templategen import Templategen from dotdrop.templategen import Templategen
from dotdrop.linktypes import LinkTypes from dotdrop.linktypes import LinkTypes
from dotdrop.utils import shell from dotdrop.utils import shell, uniq_list
class CfgYaml: class CfgYaml:
@@ -490,7 +490,10 @@ class CfgYaml:
others.extend(self._rec_resolve_profile_include(i)) others.extend(self._rec_resolve_profile_include(i))
current.extend(others) current.extend(others)
# unique them # 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, []) return values.get(self.key_profiles_dotfiles, [])
def _resolve_path(self, path): def _resolve_path(self, path):

View File

@@ -95,7 +95,7 @@ class Comparator:
# content is different # content is different
funny = comp.diff_files funny = comp.diff_files
funny.extend(comp.funny_files) funny.extend(comp.funny_files)
funny = list(set(funny)) funny = utils.uniq_list(funny)
for i in funny: for i in funny:
lfile = os.path.join(left, i) lfile = os.path.join(left, i)
rfile = os.path.join(right, i) rfile = os.path.join(right, i)

View File

@@ -15,7 +15,7 @@ from dotdrop.templategen import Templategen
from dotdrop.installer import Installer from dotdrop.installer import Installer
from dotdrop.updater import Updater from dotdrop.updater import Updater
from dotdrop.comparator import Comparator 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 from dotdrop.linktypes import LinkTypes
LOG = Logger() LOG = Logger()
@@ -71,7 +71,8 @@ def cmd_install(o):
dotfiles = o.dotfiles dotfiles = o.dotfiles
if o.install_keys: if o.install_keys:
# filtered dotfiles to install # 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: if not dotfiles:
msg = 'no dotfile to install for this profile (\"{}\")' msg = 'no dotfile to install for this profile (\"{}\")'
LOG.warn(msg.format(o.profile)) LOG.warn(msg.format(o.profile))
@@ -388,7 +389,8 @@ def cmd_detail(o):
dotfiles = o.dotfiles dotfiles = o.dotfiles
if o.detail_keys: if o.detail_keys:
# filtered dotfiles to install # 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)) LOG.emph('dotfiles details for profile \"{}\":\n'.format(o.profile))
for d in dotfiles: for d in dotfiles:
_detail(o.dotpath, d) _detail(o.dotpath, d)

View File

@@ -130,3 +130,12 @@ def must_ignore(paths, ignores, debug=False):
LOG.dbg('ignore \"{}\" match: {}'.format(i, p)) LOG.dbg('ignore \"{}\" match: {}'.format(i, p))
return True return True
return False 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

142
tests-ng/include-order.sh Executable file
View File

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