diff --git a/dotdrop/cfg_aggregator.py b/dotdrop/cfg_aggregator.py index 747a2fb..3f074db 100644 --- a/dotdrop/cfg_aggregator.py +++ b/dotdrop/cfg_aggregator.py @@ -78,7 +78,7 @@ class CfgAggregator: self.log.dbg('trans_w: {}'.format(self.trans_w)) # variables - self.variables = self.cfgyaml.variables + self.variables = self.cfgyaml.get_variables() if self.debug: self.log.dbg('variables: {}'.format(self.variables)) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index bfe959b..1f3a1b6 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -100,6 +100,10 @@ class CfgYaml: if self.debug: self.log.dbg('after normalization: {}'.format(self.yaml_dict)) + def get_variables(self): + """retrieve all variables""" + return self._merge_dict(self.variables, self.dvariables) + def _parse_main_yaml(self, dic): """parse the different blocks""" self.ori_settings = self._get_entry(dic, self.key_settings) @@ -183,6 +187,17 @@ class CfgYaml: dst = dotfile[self.key_dotfile_dst] dotfile[self.key_dotfile_dst] = self._resolve_path(dst) + def _shell_dynvars(self, dvars): + new = {} + for k, v in dvars.items(): + ret, val = shell(v) + if not ret: + err = 'command \"{}\" failed: {}'.format(k, val) + self.log.err(err) + raise YamlException(err) + new[k] = val + return new + def _merge_and_apply_variables(self): """ resolve all variables across the config @@ -212,7 +227,7 @@ class CfgYaml: ret, out = shell(allvars[k]) if not ret: err = 'command \"{}\" failed: {}'.format(allvars[k], out) - self.log.error(err) + self.log.err(err) raise YamlException(err) allvars[k] = out @@ -296,6 +311,14 @@ class CfgYaml: self.log.dbg('resolved: {}'.format(new)) v[self.key_import_profile_dfs] = new + # profile includes + for k, v in self.profiles.items(): + if self.key_profile_include in v: + new = [] + for k in v[self.key_profile_include]: + new.append(t.generate_string(k)) + v[self.key_profile_include] = new + return allvars def _norm_actions(self, actions): @@ -435,7 +458,8 @@ class CfgYaml: mandatory=False) self.dvariables = self._import_sub(path, self.key_dvariables, self.dvariables, - mandatory=False) + mandatory=False, + patch_func=self._shell_dynvars) def _import_actions(self, paths): """import external actions from paths""" @@ -593,7 +617,7 @@ class CfgYaml: new = patch_func(new) if not new: self.log.warn('no \"{}\" imported from \"{}\"'.format(key, path)) - return + return current if self.debug: self.log.dbg('found: {}'.format(new)) if isinstance(current, dict) and isinstance(new, dict): @@ -609,6 +633,10 @@ class CfgYaml: def _merge_dict(self, high, low): """merge low into high""" + if not high: + high = {} + if not low: + low = {} return {**low, **high} def _get_entry(self, dic, key, mandatory=True): diff --git a/tests-ng/dynextvariables.sh b/tests-ng/dynextvariables.sh index 744e717..155b07e 100755 --- a/tests-ng/dynextvariables.sh +++ b/tests-ng/dynextvariables.sh @@ -135,7 +135,7 @@ echo "test: {{@@ test @@}}" >> ${tmps}/dotfiles/abc # install cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V -#cat ${tmpd}/abc +cat ${tmpd}/abc grep '^var1: var' ${tmpd}/abc >/dev/null grep '^dvar1: dynvar' ${tmpd}/abc >/dev/null diff --git a/tests-ng/ext-actions.sh b/tests-ng/ext-actions.sh index f753dbf..64ad359 100755 --- a/tests-ng/ext-actions.sh +++ b/tests-ng/ext-actions.sh @@ -101,12 +101,19 @@ cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V # checks [ ! -e ${tmpa}/pre ] && exit 1 grep pre ${tmpa}/pre >/dev/null +echo "pre is ok" + [ ! -e ${tmpa}/post ] && exit 1 grep post ${tmpa}/post >/dev/null +echo "post is ok" + [ ! -e ${tmpa}/naked ] && exit 1 grep naked ${tmpa}/naked >/dev/null +echo "naked is ok" + [ ! -e ${tmpa}/write ] && exit 1 -grep write ${tmpa}/write >/dev/null +grep over ${tmpa}/write >/dev/null +echo "write is ok" ## CLEANING rm -rf ${tmps} ${tmpd} ${tmpa} diff --git a/tests-ng/extvariables.sh b/tests-ng/extvariables.sh index 0787da8..07d3cb4 100755 --- a/tests-ng/extvariables.sh +++ b/tests-ng/extvariables.sh @@ -109,8 +109,7 @@ echo "evar1: {{@@ evar1 @@}}" >> ${tmps}/dotfiles/abc # install cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V -#cat ${tmpd}/abc - +cat ${tmpd}/abc grep '^var3: extvar1 var2 var3' ${tmpd}/abc >/dev/null grep '^dvar3: extdvar1 dvar2 dvar3' ${tmpd}/abc >/dev/null grep '^var4: echo extvar1 var2 var3' ${tmpd}/abc >/dev/null @@ -168,7 +167,7 @@ echo "varx: {{@@ varx @@}}" >> ${tmps}/dotfiles/abc # install cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V -#cat ${tmpd}/abc +cat ${tmpd}/abc grep '^var3: extvar1 var2 var3' ${tmpd}/abc >/dev/null grep '^dvar3: extdvar1 dvar2 dvar3' ${tmpd}/abc >/dev/null