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

fix several issues with variable importation and resolving

This commit is contained in:
deadc0de6
2019-06-17 15:41:03 +02:00
parent f6fc834fbc
commit dc45a6dc78
5 changed files with 43 additions and 9 deletions

View File

@@ -78,7 +78,7 @@ class CfgAggregator:
self.log.dbg('trans_w: {}'.format(self.trans_w)) self.log.dbg('trans_w: {}'.format(self.trans_w))
# variables # variables
self.variables = self.cfgyaml.variables self.variables = self.cfgyaml.get_variables()
if self.debug: if self.debug:
self.log.dbg('variables: {}'.format(self.variables)) self.log.dbg('variables: {}'.format(self.variables))

View File

@@ -100,6 +100,10 @@ class CfgYaml:
if self.debug: if self.debug:
self.log.dbg('after normalization: {}'.format(self.yaml_dict)) 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): def _parse_main_yaml(self, dic):
"""parse the different blocks""" """parse the different blocks"""
self.ori_settings = self._get_entry(dic, self.key_settings) self.ori_settings = self._get_entry(dic, self.key_settings)
@@ -183,6 +187,17 @@ class CfgYaml:
dst = dotfile[self.key_dotfile_dst] dst = dotfile[self.key_dotfile_dst]
dotfile[self.key_dotfile_dst] = self._resolve_path(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): def _merge_and_apply_variables(self):
""" """
resolve all variables across the config resolve all variables across the config
@@ -212,7 +227,7 @@ class CfgYaml:
ret, out = shell(allvars[k]) ret, out = shell(allvars[k])
if not ret: if not ret:
err = 'command \"{}\" failed: {}'.format(allvars[k], out) err = 'command \"{}\" failed: {}'.format(allvars[k], out)
self.log.error(err) self.log.err(err)
raise YamlException(err) raise YamlException(err)
allvars[k] = out allvars[k] = out
@@ -296,6 +311,14 @@ class CfgYaml:
self.log.dbg('resolved: {}'.format(new)) self.log.dbg('resolved: {}'.format(new))
v[self.key_import_profile_dfs] = 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 return allvars
def _norm_actions(self, actions): def _norm_actions(self, actions):
@@ -435,7 +458,8 @@ class CfgYaml:
mandatory=False) mandatory=False)
self.dvariables = self._import_sub(path, self.key_dvariables, self.dvariables = self._import_sub(path, self.key_dvariables,
self.dvariables, self.dvariables,
mandatory=False) mandatory=False,
patch_func=self._shell_dynvars)
def _import_actions(self, paths): def _import_actions(self, paths):
"""import external actions from paths""" """import external actions from paths"""
@@ -593,7 +617,7 @@ class CfgYaml:
new = patch_func(new) new = patch_func(new)
if not new: if not new:
self.log.warn('no \"{}\" imported from \"{}\"'.format(key, path)) self.log.warn('no \"{}\" imported from \"{}\"'.format(key, path))
return return current
if self.debug: if self.debug:
self.log.dbg('found: {}'.format(new)) self.log.dbg('found: {}'.format(new))
if isinstance(current, dict) and isinstance(new, dict): if isinstance(current, dict) and isinstance(new, dict):
@@ -609,6 +633,10 @@ class CfgYaml:
def _merge_dict(self, high, low): def _merge_dict(self, high, low):
"""merge low into high""" """merge low into high"""
if not high:
high = {}
if not low:
low = {}
return {**low, **high} return {**low, **high}
def _get_entry(self, dic, key, mandatory=True): def _get_entry(self, dic, key, mandatory=True):

View File

@@ -135,7 +135,7 @@ echo "test: {{@@ test @@}}" >> ${tmps}/dotfiles/abc
# install # install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
#cat ${tmpd}/abc cat ${tmpd}/abc
grep '^var1: var' ${tmpd}/abc >/dev/null grep '^var1: var' ${tmpd}/abc >/dev/null
grep '^dvar1: dynvar' ${tmpd}/abc >/dev/null grep '^dvar1: dynvar' ${tmpd}/abc >/dev/null

View File

@@ -101,12 +101,19 @@ cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
# checks # checks
[ ! -e ${tmpa}/pre ] && exit 1 [ ! -e ${tmpa}/pre ] && exit 1
grep pre ${tmpa}/pre >/dev/null grep pre ${tmpa}/pre >/dev/null
echo "pre is ok"
[ ! -e ${tmpa}/post ] && exit 1 [ ! -e ${tmpa}/post ] && exit 1
grep post ${tmpa}/post >/dev/null grep post ${tmpa}/post >/dev/null
echo "post is ok"
[ ! -e ${tmpa}/naked ] && exit 1 [ ! -e ${tmpa}/naked ] && exit 1
grep naked ${tmpa}/naked >/dev/null grep naked ${tmpa}/naked >/dev/null
echo "naked is ok"
[ ! -e ${tmpa}/write ] && exit 1 [ ! -e ${tmpa}/write ] && exit 1
grep write ${tmpa}/write >/dev/null grep over ${tmpa}/write >/dev/null
echo "write is ok"
## CLEANING ## CLEANING
rm -rf ${tmps} ${tmpd} ${tmpa} rm -rf ${tmps} ${tmpd} ${tmpa}

View File

@@ -109,8 +109,7 @@ echo "evar1: {{@@ evar1 @@}}" >> ${tmps}/dotfiles/abc
# install # install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V 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 '^var3: extvar1 var2 var3' ${tmpd}/abc >/dev/null
grep '^dvar3: extdvar1 dvar2 dvar3' ${tmpd}/abc >/dev/null grep '^dvar3: extdvar1 dvar2 dvar3' ${tmpd}/abc >/dev/null
grep '^var4: echo extvar1 var2 var3' ${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 # install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V 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 '^var3: extvar1 var2 var3' ${tmpd}/abc >/dev/null
grep '^dvar3: extdvar1 dvar2 dvar3' ${tmpd}/abc >/dev/null grep '^dvar3: extdvar1 dvar2 dvar3' ${tmpd}/abc >/dev/null