From 6ab0b009306f1cafd6f4f4763e7ac317d99c6b7f Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Thu, 19 Nov 2020 15:04:11 +0100 Subject: [PATCH] provide installer with is_template info --- dotdrop/dotdrop.py | 16 +++++++------ dotdrop/installer.py | 54 ++++++++++++++++++++----------------------- tests/test_install.py | 7 ++---- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index ef30e0d..af20daf 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -133,10 +133,11 @@ def _dotfile_compare(o, dotfile, tmp): LOG.dbg('points to itself') 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 ret, err, insttmp = inst.install_to_temp(t, tmp, src, dotfile.dst, - template=dotfile.template, + is_template=True, chmod=dotfile.chmod) if not ret: # 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(dotfile.prt()) + is_template = dotfile.template and Templategen.is_template(dotfile.src) if hasattr(dotfile, 'link') and dotfile.link == LinkTypes.LINK: # link r, err = inst.install(t, dotfile.src, dotfile.dst, dotfile.link, actionexec=pre_actions_exec, - template=dotfile.template, + is_template=is_template, chmod=dotfile.chmod) elif hasattr(dotfile, 'link') and \ 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, dotfile.link, actionexec=pre_actions_exec, - template=dotfile.template, + is_template=is_template, chmod=dotfile.chmod) else: # nolink @@ -236,7 +238,7 @@ def _dotfile_install(o, dotfile, tmpdir=None): actionexec=pre_actions_exec, noempty=dotfile.noempty, ignore=ignores, - template=dotfile.template, + is_template=is_template, chmod=dotfile.chmod) if 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)) if not os.path.isdir(path): template = 'no' - if Templategen.is_template(path): + if dotfile.template and Templategen.is_template(path): template = 'yes' LOG.sub('{} (template:{})'.format(path, template)) else: @@ -685,7 +687,7 @@ def _detail(dotpath, dotfile): for f in files: p = os.path.join(root, f) template = 'no' - if Templategen.is_template(p): + if dotfile.template and Templategen.is_template(p): template = 'yes' LOG.sub('{} (template:{})'.format(p, template)) diff --git a/dotdrop/installer.py b/dotdrop/installer.py index 51ecf99..248c7bf 100644 --- a/dotdrop/installer.py +++ b/dotdrop/installer.py @@ -12,7 +12,6 @@ import shutil # local imports from dotdrop.logger import Logger from dotdrop.linktypes import LinkTypes -from dotdrop.templategen import Templategen import dotdrop.utils as utils from dotdrop.exceptions import UndefinedException @@ -66,7 +65,7 @@ class Installer: def install(self, templater, src, dst, linktype, actionexec=None, noempty=False, - ignore=[], template=True, + ignore=[], is_template=True, chmod=None): """ install src to dst @@ -78,7 +77,7 @@ class Installer: @actionexec: action executor callback @noempty: render empty template flag @ignore: pattern to ignore when installing - @template: template this dotfile + @is_template: this dotfile is a template @chmod: rights to apply if any return @@ -105,7 +104,7 @@ class Installer: # and ignore any actions if self.totemp: r, err, _ = self.install_to_temp(templater, self.totemp, - src, dst, template=template, + src, dst, is_template=is_template, chmod=chmod) return self._log_install(r, err) @@ -120,19 +119,19 @@ class Installer: r, err = self._copy_dir(templater, src, dst, actionexec=actionexec, noempty=noempty, ignore=ignore, - template=template, + is_template=is_template, chmod=chmod) else: r, err = self._copy_file(templater, src, dst, actionexec=actionexec, noempty=noempty, ignore=ignore, - template=template, + is_template=is_template, chmod=chmod) elif linktype == LinkTypes.LINK: # symlink r, err = self._link(templater, src, dst, actionexec=actionexec, - template=template) + is_template=is_template) elif linktype == LinkTypes.LINK_CHILDREN: # symlink direct children if not isdir: @@ -144,7 +143,7 @@ class Installer: else: r, err = self._link_children(templater, src, dst, actionexec=actionexec, - template=template) + is_template=is_template) # handle chmod # - on success (r, not err) @@ -167,7 +166,7 @@ class Installer: return self._log_install(r, err) def install_to_temp(self, templater, tmpdir, src, dst, - template=True, chmod=None): + is_template=True, chmod=None): """ install a dotfile to a tempdir @@ -175,7 +174,7 @@ class Installer: @tmpdir: where to install @src: dotfile source path in dotpath @dst: dotfile destination path in the FS - @template: template this dotfile + @is_template: this dotfile is a template @chmod: rights to apply if any return @@ -205,7 +204,8 @@ class Installer: tmpdst = self._pivot_path(dst, tmpdir) ret, err = self.install(templater, src, tmpdst, LinkTypes.NOLINK, - template=template, chmod=chmod) + is_template=is_template, + chmod=chmod) if self.debug: if ret: self.log.dbg('tmp installed in {}'.format(tmpdst)) @@ -223,7 +223,7 @@ class Installer: # 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 @@ -233,7 +233,7 @@ class Installer: - False, None : ignored - False, 'aborted' : user aborted """ - if template and Templategen.is_template(src): + if is_template: if self.debug: self.log.dbg('is a template') self.log.dbg('install to {}'.format(self.workdir)) @@ -241,7 +241,7 @@ class Installer: r, err = self.install(templater, src, tmp, LinkTypes.NOLINK, actionexec=actionexec, - template=template) + is_template=is_template) if not r and not os.path.exists(tmp): return r, err src = tmp @@ -249,7 +249,7 @@ class Installer: return r, err def _link_children(self, templater, src, dst, - actionexec=None, template=True): + actionexec=None, is_template=True): """ install link:link_children @@ -292,7 +292,7 @@ class Installer: if self.debug: self.log.dbg('symlink child {} to {}'.format(subsrc, subdst)) - if template and Templategen.is_template(subsrc): + if is_template: if self.debug: self.log.dbg('child is a template') self.log.dbg('install to {} and symlink' @@ -301,7 +301,7 @@ class Installer: r, e = self.install(templater, subsrc, tmp, LinkTypes.NOLINK, actionexec=actionexec, - template=template) + is_template=is_template) if not r and e and not os.path.exists(tmp): continue subsrc = tmp @@ -379,7 +379,7 @@ class Installer: def _copy_file(self, templater, src, dst, actionexec=None, noempty=False, - ignore=[], template=True, + ignore=[], is_template=True, chmod=None): """ install src to dst when is a file @@ -394,7 +394,7 @@ class Installer: self.log.dbg('deploy file: {}'.format(src)) self.log.dbg('ignore empty: {}'.format(noempty)) 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)) # check no loop @@ -418,7 +418,7 @@ class Installer: # handle the file content = None - if template: + if is_template: # template the file saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst)) try: @@ -440,7 +440,6 @@ class Installer: ret, err = self._write(src, dst, content=content, actionexec=actionexec, - template=template, chmod=chmod) if ret and not err: if not self.dry and not self.comparing: @@ -449,7 +448,7 @@ class Installer: def _copy_dir(self, templater, src, dst, actionexec=None, noempty=False, - ignore=[], template=True, chmod=None): + ignore=[], is_template=True, chmod=None): """ install src to dst when is a directory @@ -481,7 +480,7 @@ class Installer: actionexec=actionexec, noempty=noempty, ignore=ignore, - template=template, + is_template=is_template, chmod=None) if not res and err: # error occured @@ -497,7 +496,7 @@ class Installer: actionexec=actionexec, noempty=noempty, ignore=ignore, - template=template, + is_template=is_template, chmod=None) if not res and err: # error occured @@ -509,12 +508,9 @@ class Installer: return ret def _write(self, src, dst, content=None, - actionexec=None, template=True, - chmod=None): + actionexec=None, chmod=None): """ copy dotfile / write content to file - content is always empty if template is False - and is to be ignored return - True, None : success @@ -573,7 +569,7 @@ class Installer: self.log.warn('ignoring {}'.format(dst)) return False, 'aborted' - if template: + if content: # write content the file try: with open(dst, 'wb') as f: diff --git a/tests/test_install.py b/tests/test_install.py index cbac55a..c20cd4e 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -6,7 +6,7 @@ basic unittest for the install function import os import unittest -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock import filecmp from dotdrop.cfg_aggregator import CfgAggregator as Cfg @@ -460,8 +460,7 @@ exec bspwm 'Remove regular file {} and replace with empty directory?' .format(dst)) - @patch('dotdrop.installer.Templategen') - def test_runs_templater(self, mocked_templategen): + def test_runs_templater(self): """test runs templater""" # create source dir src_dir = get_tempdir() @@ -480,8 +479,6 @@ exec bspwm installer = Installer() templater = MagicMock() 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, linktype=LinkTypes.LINK_CHILDREN, actionexec=None)