mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-04 19:44:45 +00:00
Merge pull request #227 from deadc0de6/templatenotfound
Templatenotfound
This commit is contained in:
@@ -209,6 +209,7 @@ def cmd_compare(o, tmp):
|
||||
t = Templategen(base=o.dotpath, variables=o.variables,
|
||||
func_file=o.func_file, filter_file=o.filter_file,
|
||||
debug=o.debug)
|
||||
tvars = t.add_tmp_vars()
|
||||
inst = Installer(create=o.create, backup=o.backup,
|
||||
dry=o.dry, base=o.dotpath,
|
||||
workdir=o.workdir, debug=o.debug,
|
||||
@@ -217,6 +218,11 @@ def cmd_compare(o, tmp):
|
||||
comp = Comparator(diff_cmd=o.diff_command, debug=o.debug)
|
||||
|
||||
for dotfile in selected:
|
||||
# add dotfile variables
|
||||
t.restore_vars(tvars)
|
||||
newvars = dotfile.get_dotfile_variables()
|
||||
t.add_tmp_vars(newvars=newvars)
|
||||
|
||||
if o.debug:
|
||||
LOG.dbg('comparing {}'.format(dotfile))
|
||||
src = dotfile.src
|
||||
|
||||
@@ -6,7 +6,8 @@ jinja2 template generator
|
||||
"""
|
||||
|
||||
import os
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from jinja2 import Environment, FileSystemLoader, \
|
||||
ChoiceLoader, FunctionLoader, TemplateNotFound
|
||||
|
||||
# local imports
|
||||
import dotdrop.utils as utils
|
||||
@@ -35,7 +36,9 @@ class Templategen:
|
||||
self.base = base.rstrip(os.sep)
|
||||
self.debug = debug
|
||||
self.log = Logger()
|
||||
loader = FileSystemLoader(self.base)
|
||||
loader1 = FileSystemLoader(self.base)
|
||||
loader2 = FunctionLoader(self._template_loader)
|
||||
loader = ChoiceLoader([loader1, loader2])
|
||||
self.env = Environment(loader=loader,
|
||||
trim_blocks=True, lstrip_blocks=True,
|
||||
keep_trailing_newline=True,
|
||||
@@ -142,6 +145,16 @@ class Templategen:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _template_loader(self, relpath):
|
||||
"""manually load template when outside of base"""
|
||||
path = os.path.join(self.base, relpath)
|
||||
path = os.path.normpath(path)
|
||||
if not os.path.exists(path):
|
||||
raise TemplateNotFound(path)
|
||||
with open(path, 'r') as f:
|
||||
content = f.read()
|
||||
return content
|
||||
|
||||
def _handle_text_file(self, src):
|
||||
"""write text to file"""
|
||||
template_rel_path = os.path.relpath(src, self.base)
|
||||
|
||||
@@ -47,6 +47,7 @@ echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)"
|
||||
# the dotfile source
|
||||
tmps=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
|
||||
mkdir -p ${tmps}/dotfiles
|
||||
mkdir -p ${tmps}/dotfiles-other
|
||||
# the dotfile destination
|
||||
tmpd=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
|
||||
|
||||
@@ -90,7 +91,7 @@ cat > ${cfg2} << _EOF
|
||||
config:
|
||||
backup: true
|
||||
create: true
|
||||
dotpath: dotfiles
|
||||
dotpath: dotfiles-other
|
||||
dotfiles:
|
||||
f_def:
|
||||
dst: ${tmpd}/def
|
||||
@@ -98,10 +99,14 @@ dotfiles:
|
||||
f_ghi:
|
||||
dst: ${tmpd}/ghi
|
||||
src: ghi
|
||||
f_asub:
|
||||
dst: ${tmpd}/subdir/sub/asub
|
||||
src: subdir/sub/asub
|
||||
profiles:
|
||||
p2:
|
||||
dotfiles:
|
||||
- f_def
|
||||
- f_asub
|
||||
psubsub:
|
||||
dotfiles:
|
||||
- f_sub
|
||||
@@ -110,10 +115,23 @@ _EOF
|
||||
# create the source
|
||||
mkdir -p ${tmps}/dotfiles/
|
||||
echo "abc" > ${tmps}/dotfiles/abc
|
||||
echo "def" > ${tmps}/dotfiles/def
|
||||
echo "ghi" > ${tmps}/dotfiles/ghi
|
||||
echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles/abc
|
||||
|
||||
echo "def" > ${tmps}/dotfiles-other/def
|
||||
echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles-other/def
|
||||
|
||||
echo "ghi" > ${tmps}/dotfiles-other/ghi
|
||||
echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles-other/ghi
|
||||
|
||||
echo "zzz" > ${tmps}/dotfiles/zzz
|
||||
echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles/zzz
|
||||
|
||||
echo "sub" > ${tmps}/dotfiles/sub
|
||||
echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles/sub
|
||||
|
||||
mkdir -p ${tmps}/dotfiles-other/subdir/sub
|
||||
echo "subsub" > ${tmps}/dotfiles-other/subdir/sub/asub
|
||||
echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles-other/subdir/sub/asub
|
||||
|
||||
# install
|
||||
cd ${ddpath} | ${bin} files -c ${cfg1} -p p0 -V | grep f_def
|
||||
@@ -123,6 +141,39 @@ cd ${ddpath} | ${bin} files -c ${cfg1} -p p3 -V | grep f_zzz
|
||||
cd ${ddpath} | ${bin} files -c ${cfg1} -p pup -V | grep f_sub
|
||||
cd ${ddpath} | ${bin} files -c ${cfg1} -p psubsub -V | grep f_sub
|
||||
|
||||
# test compare too
|
||||
cd ${ddpath} | ${bin} install -c ${cfg1} -p p2 -V
|
||||
cd ${ddpath} | ${bin} compare -c ${cfg1} -p p2 -V
|
||||
|
||||
# test with non-existing dotpath this time
|
||||
rm -rf ${tmps}/dotfiles
|
||||
cat > ${cfg1} << _EOF
|
||||
config:
|
||||
backup: true
|
||||
create: true
|
||||
dotpath: dotfiles
|
||||
import_configs:
|
||||
- ${cfg2}
|
||||
dotfiles:
|
||||
profiles:
|
||||
_EOF
|
||||
cat > ${cfg2} << _EOF
|
||||
config:
|
||||
backup: true
|
||||
create: true
|
||||
dotpath: dotfiles-other
|
||||
dotfiles:
|
||||
f_asub:
|
||||
dst: ${tmpd}/subdir/sub/asub
|
||||
src: subdir/sub/asub
|
||||
profiles:
|
||||
p2:
|
||||
dotfiles:
|
||||
- f_asub
|
||||
_EOF
|
||||
cd ${ddpath} | ${bin} install -c ${cfg1} -p p2 -V
|
||||
cd ${ddpath} | ${bin} compare -c ${cfg1} -p p2 -V
|
||||
|
||||
## CLEANING
|
||||
rm -rf ${tmps} ${tmpd}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user