1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-12 10:43:30 +00:00

provide installer with is_template info

This commit is contained in:
deadc0de6
2020-11-19 15:04:11 +01:00
parent d253610f2d
commit 6ab0b00930
3 changed files with 36 additions and 41 deletions

View File

@@ -133,10 +133,11 @@ def _dotfile_compare(o, dotfile, tmp):
LOG.dbg('points to itself') LOG.dbg('points to itself')
return True return True
if dotfile.template or Templategen.is_template(src): insttmp = None
if dotfile.template and Templategen.is_template(src):
# install dotfile to temporary dir for compare # install dotfile to temporary dir for compare
ret, err, insttmp = inst.install_to_temp(t, tmp, src, dotfile.dst, ret, err, insttmp = inst.install_to_temp(t, tmp, src, dotfile.dst,
template=dotfile.template, is_template=True,
chmod=dotfile.chmod) chmod=dotfile.chmod)
if not ret: if not ret:
# failed to install to tmp # failed to install to tmp
@@ -205,12 +206,13 @@ def _dotfile_install(o, dotfile, tmpdir=None):
LOG.dbg('installing dotfile: \"{}\"'.format(dotfile.key)) LOG.dbg('installing dotfile: \"{}\"'.format(dotfile.key))
LOG.dbg(dotfile.prt()) LOG.dbg(dotfile.prt())
is_template = dotfile.template and Templategen.is_template(dotfile.src)
if hasattr(dotfile, 'link') and dotfile.link == LinkTypes.LINK: if hasattr(dotfile, 'link') and dotfile.link == LinkTypes.LINK:
# link # link
r, err = inst.install(t, dotfile.src, dotfile.dst, r, err = inst.install(t, dotfile.src, dotfile.dst,
dotfile.link, dotfile.link,
actionexec=pre_actions_exec, actionexec=pre_actions_exec,
template=dotfile.template, is_template=is_template,
chmod=dotfile.chmod) chmod=dotfile.chmod)
elif hasattr(dotfile, 'link') and \ elif hasattr(dotfile, 'link') and \
dotfile.link == LinkTypes.LINK_CHILDREN: dotfile.link == LinkTypes.LINK_CHILDREN:
@@ -218,7 +220,7 @@ def _dotfile_install(o, dotfile, tmpdir=None):
r, err = inst.install(t, dotfile.src, dotfile.dst, r, err = inst.install(t, dotfile.src, dotfile.dst,
dotfile.link, dotfile.link,
actionexec=pre_actions_exec, actionexec=pre_actions_exec,
template=dotfile.template, is_template=is_template,
chmod=dotfile.chmod) chmod=dotfile.chmod)
else: else:
# nolink # nolink
@@ -236,7 +238,7 @@ def _dotfile_install(o, dotfile, tmpdir=None):
actionexec=pre_actions_exec, actionexec=pre_actions_exec,
noempty=dotfile.noempty, noempty=dotfile.noempty,
ignore=ignores, ignore=ignores,
template=dotfile.template, is_template=is_template,
chmod=dotfile.chmod) chmod=dotfile.chmod)
if tmp: if tmp:
tmp = os.path.join(o.dotpath, tmp) tmp = os.path.join(o.dotpath, tmp)
@@ -677,7 +679,7 @@ def _detail(dotpath, dotfile):
path = os.path.join(dotpath, os.path.expanduser(dotfile.src)) path = os.path.join(dotpath, os.path.expanduser(dotfile.src))
if not os.path.isdir(path): if not os.path.isdir(path):
template = 'no' template = 'no'
if Templategen.is_template(path): if dotfile.template and Templategen.is_template(path):
template = 'yes' template = 'yes'
LOG.sub('{} (template:{})'.format(path, template)) LOG.sub('{} (template:{})'.format(path, template))
else: else:
@@ -685,7 +687,7 @@ def _detail(dotpath, dotfile):
for f in files: for f in files:
p = os.path.join(root, f) p = os.path.join(root, f)
template = 'no' template = 'no'
if Templategen.is_template(p): if dotfile.template and Templategen.is_template(p):
template = 'yes' template = 'yes'
LOG.sub('{} (template:{})'.format(p, template)) LOG.sub('{} (template:{})'.format(p, template))

View File

@@ -12,7 +12,6 @@ import shutil
# local imports # local imports
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.linktypes import LinkTypes from dotdrop.linktypes import LinkTypes
from dotdrop.templategen import Templategen
import dotdrop.utils as utils import dotdrop.utils as utils
from dotdrop.exceptions import UndefinedException from dotdrop.exceptions import UndefinedException
@@ -66,7 +65,7 @@ class Installer:
def install(self, templater, src, dst, linktype, def install(self, templater, src, dst, linktype,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], template=True, ignore=[], is_template=True,
chmod=None): chmod=None):
""" """
install src to dst install src to dst
@@ -78,7 +77,7 @@ class Installer:
@actionexec: action executor callback @actionexec: action executor callback
@noempty: render empty template flag @noempty: render empty template flag
@ignore: pattern to ignore when installing @ignore: pattern to ignore when installing
@template: template this dotfile @is_template: this dotfile is a template
@chmod: rights to apply if any @chmod: rights to apply if any
return return
@@ -105,7 +104,7 @@ class Installer:
# and ignore any actions # and ignore any actions
if self.totemp: if self.totemp:
r, err, _ = self.install_to_temp(templater, self.totemp, r, err, _ = self.install_to_temp(templater, self.totemp,
src, dst, template=template, src, dst, is_template=is_template,
chmod=chmod) chmod=chmod)
return self._log_install(r, err) return self._log_install(r, err)
@@ -120,19 +119,19 @@ class Installer:
r, err = self._copy_dir(templater, src, dst, r, err = self._copy_dir(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, ignore=ignore, noempty=noempty, ignore=ignore,
template=template, is_template=is_template,
chmod=chmod) chmod=chmod)
else: else:
r, err = self._copy_file(templater, src, dst, r, err = self._copy_file(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, ignore=ignore, noempty=noempty, ignore=ignore,
template=template, is_template=is_template,
chmod=chmod) chmod=chmod)
elif linktype == LinkTypes.LINK: elif linktype == LinkTypes.LINK:
# symlink # symlink
r, err = self._link(templater, src, dst, r, err = self._link(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
template=template) is_template=is_template)
elif linktype == LinkTypes.LINK_CHILDREN: elif linktype == LinkTypes.LINK_CHILDREN:
# symlink direct children # symlink direct children
if not isdir: if not isdir:
@@ -144,7 +143,7 @@ class Installer:
else: else:
r, err = self._link_children(templater, src, dst, r, err = self._link_children(templater, src, dst,
actionexec=actionexec, actionexec=actionexec,
template=template) is_template=is_template)
# handle chmod # handle chmod
# - on success (r, not err) # - on success (r, not err)
@@ -167,7 +166,7 @@ class Installer:
return self._log_install(r, err) return self._log_install(r, err)
def install_to_temp(self, templater, tmpdir, src, dst, def install_to_temp(self, templater, tmpdir, src, dst,
template=True, chmod=None): is_template=True, chmod=None):
""" """
install a dotfile to a tempdir install a dotfile to a tempdir
@@ -175,7 +174,7 @@ class Installer:
@tmpdir: where to install @tmpdir: where to install
@src: dotfile source path in dotpath @src: dotfile source path in dotpath
@dst: dotfile destination path in the FS @dst: dotfile destination path in the FS
@template: template this dotfile @is_template: this dotfile is a template
@chmod: rights to apply if any @chmod: rights to apply if any
return return
@@ -205,7 +204,8 @@ class Installer:
tmpdst = self._pivot_path(dst, tmpdir) tmpdst = self._pivot_path(dst, tmpdir)
ret, err = self.install(templater, src, tmpdst, ret, err = self.install(templater, src, tmpdst,
LinkTypes.NOLINK, LinkTypes.NOLINK,
template=template, chmod=chmod) is_template=is_template,
chmod=chmod)
if self.debug: if self.debug:
if ret: if ret:
self.log.dbg('tmp installed in {}'.format(tmpdst)) self.log.dbg('tmp installed in {}'.format(tmpdst))
@@ -223,7 +223,7 @@ class Installer:
# low level accessors for public methods # low level accessors for public methods
######################################################## ########################################################
def _link(self, templater, src, dst, actionexec=None, template=True): def _link(self, templater, src, dst, actionexec=None, is_template=True):
""" """
install link:link install link:link
@@ -233,7 +233,7 @@ class Installer:
- False, None : ignored - False, None : ignored
- False, 'aborted' : user aborted - False, 'aborted' : user aborted
""" """
if template and Templategen.is_template(src): if is_template:
if self.debug: if self.debug:
self.log.dbg('is a template') self.log.dbg('is a template')
self.log.dbg('install to {}'.format(self.workdir)) self.log.dbg('install to {}'.format(self.workdir))
@@ -241,7 +241,7 @@ class Installer:
r, err = self.install(templater, src, tmp, r, err = self.install(templater, src, tmp,
LinkTypes.NOLINK, LinkTypes.NOLINK,
actionexec=actionexec, actionexec=actionexec,
template=template) is_template=is_template)
if not r and not os.path.exists(tmp): if not r and not os.path.exists(tmp):
return r, err return r, err
src = tmp src = tmp
@@ -249,7 +249,7 @@ class Installer:
return r, err return r, err
def _link_children(self, templater, src, dst, def _link_children(self, templater, src, dst,
actionexec=None, template=True): actionexec=None, is_template=True):
""" """
install link:link_children install link:link_children
@@ -292,7 +292,7 @@ class Installer:
if self.debug: if self.debug:
self.log.dbg('symlink child {} to {}'.format(subsrc, subdst)) self.log.dbg('symlink child {} to {}'.format(subsrc, subdst))
if template and Templategen.is_template(subsrc): if is_template:
if self.debug: if self.debug:
self.log.dbg('child is a template') self.log.dbg('child is a template')
self.log.dbg('install to {} and symlink' self.log.dbg('install to {} and symlink'
@@ -301,7 +301,7 @@ class Installer:
r, e = self.install(templater, subsrc, tmp, r, e = self.install(templater, subsrc, tmp,
LinkTypes.NOLINK, LinkTypes.NOLINK,
actionexec=actionexec, actionexec=actionexec,
template=template) is_template=is_template)
if not r and e and not os.path.exists(tmp): if not r and e and not os.path.exists(tmp):
continue continue
subsrc = tmp subsrc = tmp
@@ -379,7 +379,7 @@ class Installer:
def _copy_file(self, templater, src, dst, def _copy_file(self, templater, src, dst,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], template=True, ignore=[], is_template=True,
chmod=None): chmod=None):
""" """
install src to dst when is a file install src to dst when is a file
@@ -394,7 +394,7 @@ class Installer:
self.log.dbg('deploy file: {}'.format(src)) self.log.dbg('deploy file: {}'.format(src))
self.log.dbg('ignore empty: {}'.format(noempty)) self.log.dbg('ignore empty: {}'.format(noempty))
self.log.dbg('ignore pattern: {}'.format(ignore)) self.log.dbg('ignore pattern: {}'.format(ignore))
self.log.dbg('template: {}'.format(template)) self.log.dbg('is_template: {}'.format(is_template))
self.log.dbg('no empty: {}'.format(noempty)) self.log.dbg('no empty: {}'.format(noempty))
# check no loop # check no loop
@@ -418,7 +418,7 @@ class Installer:
# handle the file # handle the file
content = None content = None
if template: if is_template:
# template the file # template the file
saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst)) saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
try: try:
@@ -440,7 +440,6 @@ class Installer:
ret, err = self._write(src, dst, ret, err = self._write(src, dst,
content=content, content=content,
actionexec=actionexec, actionexec=actionexec,
template=template,
chmod=chmod) chmod=chmod)
if ret and not err: if ret and not err:
if not self.dry and not self.comparing: if not self.dry and not self.comparing:
@@ -449,7 +448,7 @@ class Installer:
def _copy_dir(self, templater, src, dst, def _copy_dir(self, templater, src, dst,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], template=True, chmod=None): ignore=[], is_template=True, chmod=None):
""" """
install src to dst when is a directory install src to dst when is a directory
@@ -481,7 +480,7 @@ class Installer:
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, noempty=noempty,
ignore=ignore, ignore=ignore,
template=template, is_template=is_template,
chmod=None) chmod=None)
if not res and err: if not res and err:
# error occured # error occured
@@ -497,7 +496,7 @@ class Installer:
actionexec=actionexec, actionexec=actionexec,
noempty=noempty, noempty=noempty,
ignore=ignore, ignore=ignore,
template=template, is_template=is_template,
chmod=None) chmod=None)
if not res and err: if not res and err:
# error occured # error occured
@@ -509,12 +508,9 @@ class Installer:
return ret return ret
def _write(self, src, dst, content=None, def _write(self, src, dst, content=None,
actionexec=None, template=True, actionexec=None, chmod=None):
chmod=None):
""" """
copy dotfile / write content to file copy dotfile / write content to file
content is always empty if template is False
and is to be ignored
return return
- True, None : success - True, None : success
@@ -573,7 +569,7 @@ class Installer:
self.log.warn('ignoring {}'.format(dst)) self.log.warn('ignoring {}'.format(dst))
return False, 'aborted' return False, 'aborted'
if template: if content:
# write content the file # write content the file
try: try:
with open(dst, 'wb') as f: with open(dst, 'wb') as f:

View File

@@ -6,7 +6,7 @@ basic unittest for the install function
import os import os
import unittest import unittest
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock
import filecmp import filecmp
from dotdrop.cfg_aggregator import CfgAggregator as Cfg from dotdrop.cfg_aggregator import CfgAggregator as Cfg
@@ -460,8 +460,7 @@ exec bspwm
'Remove regular file {} and replace with empty directory?' 'Remove regular file {} and replace with empty directory?'
.format(dst)) .format(dst))
@patch('dotdrop.installer.Templategen') def test_runs_templater(self):
def test_runs_templater(self, mocked_templategen):
"""test runs templater""" """test runs templater"""
# create source dir # create source dir
src_dir = get_tempdir() src_dir = get_tempdir()
@@ -480,8 +479,6 @@ exec bspwm
installer = Installer() installer = Installer()
templater = MagicMock() templater = MagicMock()
templater.generate.return_value = b'content' templater.generate.return_value = b'content'
# make templategen treat everything as a template
mocked_templategen.is_template.return_value = True
installer.install(templater=templater, src=src_dir, dst=dst_dir, installer.install(templater=templater, src=src_dir, dst=dst_dir,
linktype=LinkTypes.LINK_CHILDREN, actionexec=None) linktype=LinkTypes.LINK_CHILDREN, actionexec=None)