From d9247f9d4bb6a8e4da30e106ec0cdfebea87b178 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 22 Aug 2022 21:16:34 +0200 Subject: [PATCH] add variable enrichment --- dotdrop/cfg_aggregator.py | 61 +++++++++++++++ tests-ng/variables-enrich.sh | 142 +++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100755 tests-ng/variables-enrich.sh diff --git a/dotdrop/cfg_aggregator.py b/dotdrop/cfg_aggregator.py index 01f1125..780dd74 100644 --- a/dotdrop/cfg_aggregator.py +++ b/dotdrop/cfg_aggregator.py @@ -7,6 +7,7 @@ handle higher level of the config file import os import shlex +import platform # local imports @@ -29,6 +30,12 @@ class CfgAggregator: file_prefix = 'f' dir_prefix = 'd' + variable_os = 'os' + variable_release = 'release' + variable_distro_id = 'distro_id' + variable_distro_like = 'distro_like' + variable_distro_version = 'distro_version' + def __init__(self, path, profile_key, debug=False, dry=False): """ high level config parser @@ -283,6 +290,8 @@ class CfgAggregator: self.variables = self.cfgyaml.variables debug_dict('variables', self.variables, self.debug) + self._enrich_variables() + # patch dotfiles in profiles self._patch_keys_to_objs(self.profiles, "dotfiles", self.get_dotfile) @@ -311,6 +320,58 @@ class CfgAggregator: self._get_trans_w_args(self.get_trans_w), islist=False) + def _enrich_variables(self): + """ + enrich available variables + """ + if self.variable_os not in self.variables: + # enrich with os variable + # https://docs.python.org/3/library/platform.html#platform.system + var_os = platform.system().lower() + self.variables[self.variable_os] = var_os + msg = 'enrich variables with {}={}' + self.log.dbg(msg.format(self.variable_os, var_os)) + if self.variable_release not in self.variables: + # enrich with release variable + # https://docs.python.org/3/library/platform.html#platform.release + var_release = platform.release().lower() + self.variables[self.variable_release] = var_release + msg = 'enrich variables with {}={}' + self.log.dbg(msg.format(self.variable_release, var_release)) + + has_distro = True + try: + import distro + assert distro + except ImportError as exc: + self.log.dbg('distro packages not found!') + has_distro = False + + if has_distro and self.variable_distro_id not in self.variables: + # enrich with distro variable + # https://pypi.org/project/distro/ + # https://distro.readthedocs.io/en/latest/#distro.id + var_distro_id = distro.id().lower() + self.variables[self.variable_distro_id] = var_distro_id + msg = 'enrich variables with {}={}' + self.log.dbg(msg.format(self.variable_distro_id, var_distro_id)) + if has_distro and self.variable_distro_version not in self.variables: + # enrich with distro variable + # https://pypi.org/project/distro/ + # https://distro.readthedocs.io/en/latest/#distro.version + var_version = distro.version().lower() + self.variables[self.variable_distro_version] = var_version + msg = 'enrich variables with {}={}' + self.log.dbg(msg.format(self.variable_distro_version, var_version)) + if has_distro and self.variable_distro_like not in self.variables: + # enrich with distro variable + # https://pypi.org/project/distro/ + # https://distro.readthedocs.io/en/latest/#distro.like + var_like = distro.like().lower() + self.variables[self.variable_distro_like] = var_like + msg = 'enrich variables with {}={}' + self.log.dbg(msg.format(self.variable_distro_like, var_like)) + def _patch_keys_to_objs(self, containers, keys, get_by_key, islist=True): """ map for each key in the attribute 'keys' in 'containers' diff --git a/tests-ng/variables-enrich.sh b/tests-ng/variables-enrich.sh new file mode 100755 index 0000000..3a45ed9 --- /dev/null +++ b/tests-ng/variables-enrich.sh @@ -0,0 +1,142 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2022, deadc0de6 +# +# test variables enrichment +# 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" +hash coverage 2>/dev/null && bin="coverage run -a --source=dotdrop -m dotdrop.dotdrop" || true + +echo "dotdrop path: ${ddpath}" +echo "pythonpath: ${PYTHONPATH}" + +# get the helpers +source ${cur}/helpers + +echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)" + +################################################################ +# this is the test +################################################################ + +# the dotfile source +tmps=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d` +mkdir -p ${tmps}/dotfiles + +# the dotfile destination +tmpd=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d` + +clear_on_exit "${tmps}" +clear_on_exit "${tmpd}" + +# create the config file +cfg="${tmps}/config.yaml" +export dotdrop_test_dst="${tmpd}/def" + +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF +#cat ${cfg} + +# create the dotfile +echo "os={{@@ os @@}}" > ${tmps}/dotfiles/abc +echo "release={{@@ release @@}}" >> ${tmps}/dotfiles/abc +echo "distro_id={{@@ distro_id @@}}" >> ${tmps}/dotfiles/abc +echo "distro_like={{@@ distro_like @@}}" >> ${tmps}/dotfiles/abc +echo "distro_version={{@@ distro_version @@}}" >> ${tmps}/dotfiles/abc + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 --verbose + +pybin="python3" +real_os=$(${pybin} -c 'import platform; print(platform.system().lower())') +real_release=$(${pybin} -c 'import platform; print(platform.release().lower())') +real_distro_id=$(${pybin} -c 'import distro; print(distro.id().lower())') +real_distro_like=$(${pybin} -c 'import distro; print(distro.like().lower())') +real_distro_version=$(${pybin} -c 'import distro; print(distro.version().lower())') + +# tests +[ ! -e ${tmpd}/abc ] && echo "abc not installed" && exit 1 +cat "${tmpd}/abc" + ## only test this on CI/CD +grep "^os=${real_os}" ${tmpd}/abc >/dev/null +grep "^release=${real_release}" ${tmpd}/abc >/dev/null +grep "^distro_id=${real_distro_id}" ${tmpd}/abc >/dev/null +grep "^distro_like=${real_distro_like}" ${tmpd}/abc >/dev/null +grep "^distro_version=${real_distro_version}" ${tmpd}/abc >/dev/null + +# already defined variables +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +variables: + os: "abc" + release: "def" + distro_id: "ghi" + distro_like: "jkl" + distro_version: "mno" +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF +rm -f "${tmpd}/abc" + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 --verbose + +# tests +[ ! -e ${tmpd}/abc ] && echo "abc not installed" && exit 1 +cat "${tmpd}/abc" +grep '^os=abc$' ${tmpd}/abc >/dev/null +grep '^release=def$' ${tmpd}/abc >/dev/null +grep '^distro_id=ghi$' ${tmpd}/abc >/dev/null +grep '^distro_like=jkl$' ${tmpd}/abc >/dev/null +grep '^distro_version=mno$' ${tmpd}/abc >/dev/null + +echo "OK" +exit 0