mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-16 07:11:10 +00:00
Merge branch 'master' of https://github.com/deadc0de6/dotdrop
This commit is contained in:
@@ -33,7 +33,8 @@ class Cfg:
|
|||||||
key_showdiff = 'showdiff'
|
key_showdiff = 'showdiff'
|
||||||
key_deflink = 'link_by_default'
|
key_deflink = 'link_by_default'
|
||||||
key_workdir = 'workdir'
|
key_workdir = 'workdir'
|
||||||
key_include_vars = 'import_variables'
|
key_import_vars = 'import_variables'
|
||||||
|
key_import_actions = 'import_actions'
|
||||||
|
|
||||||
# actions keys
|
# actions keys
|
||||||
key_actions = 'actions'
|
key_actions = 'actions'
|
||||||
@@ -216,45 +217,37 @@ class Cfg:
|
|||||||
self._abs_path(self.curworkdir)
|
self._abs_path(self.curworkdir)
|
||||||
|
|
||||||
# load external variables/dynvariables
|
# load external variables/dynvariables
|
||||||
if self.key_include_vars in self.lnk_settings:
|
if self.key_import_vars in self.lnk_settings:
|
||||||
paths = self.lnk_settings[self.key_include_vars]
|
paths = self.lnk_settings[self.key_import_vars]
|
||||||
self._load_ext_variables(paths, profile=profile)
|
self._load_ext_variables(paths, profile=profile)
|
||||||
|
|
||||||
# parse all actions
|
# parse external actions
|
||||||
if self.key_actions in self.content:
|
if self.key_import_actions in self.lnk_settings:
|
||||||
if self.content[self.key_actions] is not None:
|
for path in self.lnk_settings[self.key_import_actions]:
|
||||||
for k, v in self.content[self.key_actions].items():
|
path = self._abs_path(path)
|
||||||
# loop through all actions
|
if self.debug:
|
||||||
if k in [self.key_actions_pre, self.key_actions_post]:
|
self.log.dbg('loading actions from {}'.format(path))
|
||||||
# parse pre/post actions
|
content = self._load_yaml(path)
|
||||||
items = self.content[self.key_actions][k].items()
|
if self.key_actions in content and \
|
||||||
for k2, v2 in items:
|
content[self.key_actions] is not None:
|
||||||
if k not in self.actions:
|
self._load_actions(content[self.key_actions])
|
||||||
self.actions[k] = {}
|
|
||||||
a = Action(k2, k, v2)
|
# parse local actions
|
||||||
self.actions[k][k2] = a
|
if self.key_actions in self.content and \
|
||||||
if self.debug:
|
self.content[self.key_actions] is not None:
|
||||||
self.log.dbg('new action: {}'.format(a))
|
self._load_actions(self.content[self.key_actions])
|
||||||
else:
|
|
||||||
# parse naked actions as post actions
|
|
||||||
if self.key_actions_post not in self.actions:
|
|
||||||
self.actions[self.key_actions_post] = {}
|
|
||||||
a = Action(k, '', v)
|
|
||||||
self.actions[self.key_actions_post][k] = a
|
|
||||||
if self.debug:
|
|
||||||
self.log.dbg('new action: {}'.format(a))
|
|
||||||
|
|
||||||
# parse read transformations
|
# parse read transformations
|
||||||
if self.key_trans_r in self.content:
|
if self.key_trans_r in self.content and \
|
||||||
if self.content[self.key_trans_r] is not None:
|
self.content[self.key_trans_r] is not None:
|
||||||
for k, v in self.content[self.key_trans_r].items():
|
for k, v in self.content[self.key_trans_r].items():
|
||||||
self.trans_r[k] = Transform(k, v)
|
self.trans_r[k] = Transform(k, v)
|
||||||
|
|
||||||
# parse write transformations
|
# parse write transformations
|
||||||
if self.key_trans_w in self.content:
|
if self.key_trans_w in self.content and \
|
||||||
if self.content[self.key_trans_w] is not None:
|
self.content[self.key_trans_w] is not None:
|
||||||
for k, v in self.content[self.key_trans_w].items():
|
for k, v in self.content[self.key_trans_w].items():
|
||||||
self.trans_w[k] = Transform(k, v)
|
self.trans_w[k] = Transform(k, v)
|
||||||
|
|
||||||
# parse the dotfiles
|
# parse the dotfiles
|
||||||
# and construct the dict of objects per dotfile key
|
# and construct the dict of objects per dotfile key
|
||||||
@@ -425,6 +418,28 @@ class Cfg:
|
|||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('loaded ext dynvariables: {}'.format(dvariables))
|
self.log.dbg('loaded ext dynvariables: {}'.format(dvariables))
|
||||||
|
|
||||||
|
def _load_actions(self, dic):
|
||||||
|
for k, v in dic.items():
|
||||||
|
# loop through all actions
|
||||||
|
if k in [self.key_actions_pre, self.key_actions_post]:
|
||||||
|
# parse pre/post actions
|
||||||
|
items = dic[k].items()
|
||||||
|
for k2, v2 in items:
|
||||||
|
if k not in self.actions:
|
||||||
|
self.actions[k] = {}
|
||||||
|
a = Action(k2, k, v2)
|
||||||
|
self.actions[k][k2] = a
|
||||||
|
if self.debug:
|
||||||
|
self.log.dbg('new action: {}'.format(a))
|
||||||
|
else:
|
||||||
|
# parse naked actions as post actions
|
||||||
|
if self.key_actions_post not in self.actions:
|
||||||
|
self.actions[self.key_actions_post] = {}
|
||||||
|
a = Action(k, '', v)
|
||||||
|
self.actions[self.key_actions_post][k] = a
|
||||||
|
if self.debug:
|
||||||
|
self.log.dbg('new action: {}'.format(a))
|
||||||
|
|
||||||
def _abs_path(self, path):
|
def _abs_path(self, path):
|
||||||
"""return absolute path of path relative to the confpath"""
|
"""return absolute path of path relative to the confpath"""
|
||||||
path = os.path.expanduser(path)
|
path = os.path.expanduser(path)
|
||||||
@@ -569,8 +584,9 @@ class Cfg:
|
|||||||
"""writes the config to file"""
|
"""writes the config to file"""
|
||||||
ret = False
|
ret = False
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w') as f:
|
||||||
ret = yaml.dump(content, f,
|
ret = yaml.safe_dump(content, f,
|
||||||
default_flow_style=False, indent=2)
|
default_flow_style=False,
|
||||||
|
indent=2)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _norm_key_elem(self, elem):
|
def _norm_key_elem(self, elem):
|
||||||
@@ -827,7 +843,9 @@ class Cfg:
|
|||||||
self.lnk_settings[self.key_dotpath] = self.curdotpath
|
self.lnk_settings[self.key_dotpath] = self.curdotpath
|
||||||
self.lnk_settings[self.key_workdir] = self.curworkdir
|
self.lnk_settings[self.key_workdir] = self.curworkdir
|
||||||
# dump
|
# dump
|
||||||
ret = yaml.dump(self.content, default_flow_style=False, indent=2)
|
ret = yaml.safe_dump(self.content,
|
||||||
|
default_flow_style=False,
|
||||||
|
indent=2)
|
||||||
ret = ret.replace('{}', '')
|
ret = ret.replace('{}', '')
|
||||||
# restore paths
|
# restore paths
|
||||||
self.lnk_settings[self.key_dotpath] = dotpath
|
self.lnk_settings[self.key_dotpath] = dotpath
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ def cmd_install(o):
|
|||||||
totemp=tmpdir,
|
totemp=tmpdir,
|
||||||
showdiff=o.install_showdiff,
|
showdiff=o.install_showdiff,
|
||||||
backup_suffix=o.install_backup_suffix)
|
backup_suffix=o.install_backup_suffix)
|
||||||
installed = []
|
installed = 0
|
||||||
for dotfile in dotfiles:
|
for dotfile in dotfiles:
|
||||||
preactions = []
|
preactions = []
|
||||||
if not o.install_temporary and dotfile.actions \
|
if not o.install_temporary and dotfile.actions \
|
||||||
@@ -73,13 +73,13 @@ def cmd_install(o):
|
|||||||
if not tmp:
|
if not tmp:
|
||||||
continue
|
continue
|
||||||
src = tmp
|
src = tmp
|
||||||
r = inst.install(t, src, dotfile.dst, actions=preactions,
|
r, err = inst.install(t, src, dotfile.dst, actions=preactions,
|
||||||
noempty=dotfile.noempty)
|
noempty=dotfile.noempty)
|
||||||
if tmp:
|
if tmp:
|
||||||
tmp = os.path.join(o.dotpath, tmp)
|
tmp = os.path.join(o.dotpath, tmp)
|
||||||
if os.path.exists(tmp):
|
if os.path.exists(tmp):
|
||||||
remove(tmp)
|
remove(tmp)
|
||||||
if len(r) > 0:
|
if r:
|
||||||
if not o.install_temporary and \
|
if not o.install_temporary and \
|
||||||
Cfg.key_actions_post in dotfile.actions:
|
Cfg.key_actions_post in dotfile.actions:
|
||||||
actions = dotfile.actions[Cfg.key_actions_post]
|
actions = dotfile.actions[Cfg.key_actions_post]
|
||||||
@@ -91,10 +91,12 @@ def cmd_install(o):
|
|||||||
if o.debug:
|
if o.debug:
|
||||||
LOG.dbg('executing post action {}'.format(action))
|
LOG.dbg('executing post action {}'.format(action))
|
||||||
action.execute()
|
action.execute()
|
||||||
installed.extend(r)
|
installed += 1
|
||||||
|
elif not r and err:
|
||||||
|
LOG.err('installing \"{}\" failed: {}'.format(dotfile.key, err))
|
||||||
if o.install_temporary:
|
if o.install_temporary:
|
||||||
LOG.log('\nInstalled to tmp \"{}\".'.format(tmpdir))
|
LOG.log('\ninstalled to tmp \"{}\".'.format(tmpdir))
|
||||||
LOG.log('\n{} dotfile(s) installed.'.format(len(installed)))
|
LOG.log('\n{} dotfile(s) installed.'.format(installed))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -50,21 +50,27 @@ class Installer:
|
|||||||
self.log = Logger()
|
self.log = Logger()
|
||||||
|
|
||||||
def install(self, templater, src, dst, actions=[], noempty=False):
|
def install(self, templater, src, dst, actions=[], noempty=False):
|
||||||
"""install the src to dst using a template"""
|
"""
|
||||||
|
install src to dst using a template
|
||||||
|
return
|
||||||
|
- True, None: success
|
||||||
|
- False, error_msg: error
|
||||||
|
- False, None, ignored
|
||||||
|
"""
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('install {} to {}'.format(src, dst))
|
self.log.dbg('install {} to {}'.format(src, dst))
|
||||||
self.action_executed = False
|
self.action_executed = False
|
||||||
src = os.path.join(self.base, os.path.expanduser(src))
|
src = os.path.join(self.base, os.path.expanduser(src))
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
self.log.err('source dotfile does not exist: {}'.format(src))
|
err = 'source dotfile does not exist: {}'.format(src)
|
||||||
return []
|
return False, err
|
||||||
dst = os.path.expanduser(dst)
|
dst = os.path.expanduser(dst)
|
||||||
if self.totemp:
|
if self.totemp:
|
||||||
dst = self._pivot_path(dst, self.totemp)
|
dst = self._pivot_path(dst, self.totemp)
|
||||||
if utils.samefile(src, dst):
|
if utils.samefile(src, dst):
|
||||||
# symlink loop
|
# symlink loop
|
||||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
err = 'dotfile points to itself: {}'.format(dst)
|
||||||
return []
|
return False, err
|
||||||
isdir = os.path.isdir(src)
|
isdir = os.path.isdir(src)
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('install {} to {}'.format(src, dst))
|
self.log.dbg('install {} to {}'.format(src, dst))
|
||||||
@@ -76,15 +82,21 @@ class Installer:
|
|||||||
actions=actions, noempty=noempty)
|
actions=actions, noempty=noempty)
|
||||||
|
|
||||||
def link(self, templater, src, dst, actions=[]):
|
def link(self, templater, src, dst, actions=[]):
|
||||||
"""set src as the link target of dst"""
|
"""
|
||||||
|
set src as the link target of dst
|
||||||
|
return
|
||||||
|
- True, None: success
|
||||||
|
- False, error_msg: error
|
||||||
|
- False, None, ignored
|
||||||
|
"""
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('link {} to {}'.format(src, dst))
|
self.log.dbg('link {} to {}'.format(src, dst))
|
||||||
self.action_executed = False
|
self.action_executed = False
|
||||||
src = os.path.normpath(os.path.join(self.base,
|
src = os.path.normpath(os.path.join(self.base,
|
||||||
os.path.expanduser(src)))
|
os.path.expanduser(src)))
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
self.log.err('source dotfile does not exist: {}'.format(src))
|
err = 'source dotfile does not exist: {}'.format(src)
|
||||||
return []
|
return False, err
|
||||||
dst = os.path.normpath(os.path.expanduser(dst))
|
dst = os.path.normpath(os.path.expanduser(dst))
|
||||||
if self.totemp:
|
if self.totemp:
|
||||||
# ignore actions
|
# ignore actions
|
||||||
@@ -95,14 +107,20 @@ class Installer:
|
|||||||
self.log.dbg('dotfile is a template')
|
self.log.dbg('dotfile is a template')
|
||||||
self.log.dbg('install to {} and symlink'.format(self.workdir))
|
self.log.dbg('install to {} and symlink'.format(self.workdir))
|
||||||
tmp = self._pivot_path(dst, self.workdir, striphome=True)
|
tmp = self._pivot_path(dst, self.workdir, striphome=True)
|
||||||
i = self.install(templater, src, tmp, actions=actions)
|
i, err = self.install(templater, src, tmp, actions=actions)
|
||||||
if not i and not os.path.exists(tmp):
|
if not i and not os.path.exists(tmp):
|
||||||
return []
|
return i, err
|
||||||
src = tmp
|
src = tmp
|
||||||
return self._link(src, dst, actions=actions)
|
return self._link(src, dst, actions=actions)
|
||||||
|
|
||||||
def link_children(self, templater, src, dst, actions=[]):
|
def link_children(self, templater, src, dst, actions=[]):
|
||||||
"""link all dotfiles in a given directory"""
|
"""
|
||||||
|
link all dotfiles in a given directory
|
||||||
|
return
|
||||||
|
- True, None: success
|
||||||
|
- False, error_msg: error
|
||||||
|
- False, None, ignored
|
||||||
|
"""
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('link_children {} to {}'.format(src, dst))
|
self.log.dbg('link_children {} to {}'.format(src, dst))
|
||||||
self.action_executed = False
|
self.action_executed = False
|
||||||
@@ -110,17 +128,16 @@ class Installer:
|
|||||||
|
|
||||||
# Fail if source doesn't exist
|
# Fail if source doesn't exist
|
||||||
if not os.path.exists(parent):
|
if not os.path.exists(parent):
|
||||||
self.log.err('source dotfile does not exist: {}'.format(parent))
|
err = 'source dotfile does not exist: {}'.format(parent)
|
||||||
return []
|
return False, err
|
||||||
|
|
||||||
# Fail if source not a directory
|
# Fail if source not a directory
|
||||||
if not os.path.isdir(parent):
|
if not os.path.isdir(parent):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('symlink children of {} to {}'.format(src, dst))
|
self.log.dbg('symlink children of {} to {}'.format(src, dst))
|
||||||
|
|
||||||
self.log.err('source dotfile is not a directory: {}'
|
err = 'source dotfile is not a directory: {}'.format(parent)
|
||||||
.format(parent))
|
return False, err
|
||||||
return []
|
|
||||||
|
|
||||||
dst = os.path.normpath(os.path.expanduser(dst))
|
dst = os.path.normpath(os.path.expanduser(dst))
|
||||||
if not os.path.lexists(dst):
|
if not os.path.lexists(dst):
|
||||||
@@ -134,9 +151,8 @@ class Installer:
|
|||||||
]).format(dst)
|
]).format(dst)
|
||||||
|
|
||||||
if self.safe and not self.log.ask(msg):
|
if self.safe and not self.log.ask(msg):
|
||||||
msg = 'ignoring "{}", nothing installed'
|
err = 'ignoring "{}", nothing installed'.format(dst)
|
||||||
self.log.warn(msg.format(dst))
|
return False, err
|
||||||
return []
|
|
||||||
os.unlink(dst)
|
os.unlink(dst)
|
||||||
os.mkdir(dst)
|
os.mkdir(dst)
|
||||||
|
|
||||||
@@ -159,8 +175,8 @@ class Installer:
|
|||||||
self.log.dbg('install to {} and symlink'
|
self.log.dbg('install to {} and symlink'
|
||||||
.format(self.workdir))
|
.format(self.workdir))
|
||||||
tmp = self._pivot_path(dst, self.workdir, striphome=True)
|
tmp = self._pivot_path(dst, self.workdir, striphome=True)
|
||||||
i = self.install(templater, src, tmp, actions=actions)
|
r, e = self.install(templater, src, tmp, actions=actions)
|
||||||
if not i and not os.path.exists(tmp):
|
if not r and e and not os.path.exists(tmp):
|
||||||
continue
|
continue
|
||||||
src = tmp
|
src = tmp
|
||||||
|
|
||||||
@@ -171,54 +187,52 @@ class Installer:
|
|||||||
if len(result):
|
if len(result):
|
||||||
actions = []
|
actions = []
|
||||||
|
|
||||||
return (src, dst)
|
return True, None
|
||||||
|
|
||||||
def _link(self, src, dst, actions=[]):
|
def _link(self, src, dst, actions=[]):
|
||||||
"""set src as a link target of dst"""
|
"""set src as a link target of dst"""
|
||||||
overwrite = not self.safe
|
overwrite = not self.safe
|
||||||
if os.path.lexists(dst):
|
if os.path.lexists(dst):
|
||||||
if os.path.realpath(dst) == os.path.realpath(src):
|
if os.path.realpath(dst) == os.path.realpath(src):
|
||||||
if self.debug:
|
err = 'ignoring "{}", link exists'.format(dst)
|
||||||
self.log.dbg('ignoring "{}", link exists'.format(dst))
|
return False, err
|
||||||
return []
|
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry('would remove {} and link to {}'.format(dst, src))
|
self.log.dry('would remove {} and link to {}'.format(dst, src))
|
||||||
return []
|
return True, None
|
||||||
msg = 'Remove "{}" for link creation?'.format(dst)
|
msg = 'Remove "{}" for link creation?'.format(dst)
|
||||||
if self.safe and not self.log.ask(msg):
|
if self.safe and not self.log.ask(msg):
|
||||||
msg = 'ignoring "{}", link was not created'
|
err = 'ignoring "{}", link was not created'.format(dst)
|
||||||
self.log.warn(msg.format(dst))
|
return False, err
|
||||||
return []
|
|
||||||
overwrite = True
|
overwrite = True
|
||||||
try:
|
try:
|
||||||
utils.remove(dst)
|
utils.remove(dst)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
self.log.err('something went wrong with {}: {}'.format(src, e))
|
err = 'something went wrong with {}: {}'.format(src, e)
|
||||||
return []
|
return False, err
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry('would link {} to {}'.format(dst, src))
|
self.log.dry('would link {} to {}'.format(dst, src))
|
||||||
return []
|
return True, None
|
||||||
base = os.path.dirname(dst)
|
base = os.path.dirname(dst)
|
||||||
if not self._create_dirs(base):
|
if not self._create_dirs(base):
|
||||||
self.log.err('creating directory for {}'.format(dst))
|
err = 'creating directory for {}'.format(dst)
|
||||||
return []
|
return False, err
|
||||||
if not self._exec_pre_actions(actions):
|
r, e = self._exec_pre_actions(actions)
|
||||||
return []
|
if not r:
|
||||||
|
return False, e
|
||||||
# re-check in case action created the file
|
# re-check in case action created the file
|
||||||
if os.path.lexists(dst):
|
if os.path.lexists(dst):
|
||||||
msg = 'Remove "{}" for link creation?'.format(dst)
|
msg = 'Remove "{}" for link creation?'.format(dst)
|
||||||
if self.safe and not overwrite and not self.log.ask(msg):
|
if self.safe and not overwrite and not self.log.ask(msg):
|
||||||
msg = 'ignoring "{}", link was not created'
|
err = 'ignoring "{}", link was not created'.format(dst)
|
||||||
self.log.warn(msg.format(dst))
|
return False, err
|
||||||
return []
|
|
||||||
try:
|
try:
|
||||||
utils.remove(dst)
|
utils.remove(dst)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
self.log.err('something went wrong with {}: {}'.format(src, e))
|
err = 'something went wrong with {}: {}'.format(src, e)
|
||||||
return []
|
return False, err
|
||||||
os.symlink(src, dst)
|
os.symlink(src, dst)
|
||||||
self.log.sub('linked {} to {}'.format(dst, src))
|
self.log.sub('linked {} to {}'.format(dst, src))
|
||||||
return [(src, dst)]
|
return True, None
|
||||||
|
|
||||||
def _handle_file(self, templater, src, dst, actions=[], noempty=False):
|
def _handle_file(self, templater, src, dst, actions=[], noempty=False):
|
||||||
"""install src to dst when is a file"""
|
"""install src to dst when is a file"""
|
||||||
@@ -227,52 +241,72 @@ class Installer:
|
|||||||
self.log.dbg('ignore empty: {}'.format(noempty))
|
self.log.dbg('ignore empty: {}'.format(noempty))
|
||||||
if utils.samefile(src, dst):
|
if utils.samefile(src, dst):
|
||||||
# symlink loop
|
# symlink loop
|
||||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
err = 'dotfile points to itself: {}'.format(dst)
|
||||||
return []
|
return False, err
|
||||||
content = templater.generate(src)
|
content = templater.generate(src)
|
||||||
if noempty and utils.content_empty(content):
|
if noempty and utils.content_empty(content):
|
||||||
self.log.warn('ignoring empty template: {}'.format(src))
|
self.log.dbg('ignoring empty template: {}'.format(src))
|
||||||
return []
|
return False, None
|
||||||
if content is None:
|
if content is None:
|
||||||
self.log.err('generate from template {}'.format(src))
|
err = 'empty template {}'.format(src)
|
||||||
return []
|
return False, err
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
self.log.err('source dotfile does not exist: {}'.format(src))
|
err = 'source dotfile does not exist: {}'.format(src)
|
||||||
return []
|
return False, err
|
||||||
st = os.stat(src)
|
st = os.stat(src)
|
||||||
ret = self._write(src, dst, content, st.st_mode, actions=actions)
|
ret, err = self._write(src, dst, content, st.st_mode, actions=actions)
|
||||||
if ret < 0:
|
if ret < 0:
|
||||||
self.log.err('installing {} to {}'.format(src, dst))
|
return False, err
|
||||||
return []
|
|
||||||
if ret > 0:
|
if ret > 0:
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('ignoring {}'.format(dst))
|
self.log.dbg('ignoring {}'.format(dst))
|
||||||
return []
|
return False, None
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
if not self.dry and not self.comparing:
|
if not self.dry and not self.comparing:
|
||||||
self.log.sub('copied {} to {}'.format(src, dst))
|
self.log.sub('copied {} to {}'.format(src, dst))
|
||||||
return [(src, dst)]
|
return True, None
|
||||||
return []
|
err = 'installing {} to {}'.format(src, dst)
|
||||||
|
return False, err
|
||||||
|
|
||||||
def _handle_dir(self, templater, src, dst, actions=[], noempty=False):
|
def _handle_dir(self, templater, src, dst, actions=[], noempty=False):
|
||||||
"""install src to dst when is a directory"""
|
"""install src to dst when is a directory"""
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('install dir {}'.format(src))
|
self.log.dbg('install dir {}'.format(src))
|
||||||
self.log.dbg('ignore empty: {}'.format(noempty))
|
self.log.dbg('ignore empty: {}'.format(noempty))
|
||||||
ret = []
|
# default to nothing installed and no error
|
||||||
|
ret = False, None
|
||||||
if not self._create_dirs(dst):
|
if not self._create_dirs(dst):
|
||||||
return []
|
err = 'creating directory for {}'.format(dst)
|
||||||
|
return False, err
|
||||||
# handle all files in dir
|
# handle all files in dir
|
||||||
for entry in os.listdir(src):
|
for entry in os.listdir(src):
|
||||||
f = os.path.join(src, entry)
|
f = os.path.join(src, entry)
|
||||||
if not os.path.isdir(f):
|
if not os.path.isdir(f):
|
||||||
res = self._handle_file(templater, f, os.path.join(dst, entry),
|
# is file
|
||||||
actions=actions, noempty=noempty)
|
res, err = self._handle_file(templater, f,
|
||||||
ret.extend(res)
|
os.path.join(dst, entry),
|
||||||
|
actions=actions,
|
||||||
|
noempty=noempty)
|
||||||
|
if not res and err:
|
||||||
|
# error occured
|
||||||
|
ret = res, err
|
||||||
|
break
|
||||||
|
elif res:
|
||||||
|
# something got installed
|
||||||
|
ret = True, None
|
||||||
else:
|
else:
|
||||||
res = self._handle_dir(templater, f, os.path.join(dst, entry),
|
# is directory
|
||||||
actions=actions, noempty=noempty)
|
res, err = self._handle_dir(templater, f,
|
||||||
ret.extend(res)
|
os.path.join(dst, entry),
|
||||||
|
actions=actions,
|
||||||
|
noempty=noempty)
|
||||||
|
if not res and err:
|
||||||
|
# error occured
|
||||||
|
ret = res, err
|
||||||
|
break
|
||||||
|
elif res:
|
||||||
|
# something got installed
|
||||||
|
ret = True, None
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _fake_diff(self, dst, content):
|
def _fake_diff(self, dst, content):
|
||||||
@@ -284,13 +318,13 @@ class Installer:
|
|||||||
|
|
||||||
def _write(self, src, dst, content, rights, actions=[]):
|
def _write(self, src, dst, content, rights, actions=[]):
|
||||||
"""write content to file
|
"""write content to file
|
||||||
return 0 for success,
|
return 0, None: for success,
|
||||||
1 when already exists
|
1, None: when already exists
|
||||||
-1 when error"""
|
-1, err: when error"""
|
||||||
overwrite = not self.safe
|
overwrite = not self.safe
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry('would install {}'.format(dst))
|
self.log.dry('would install {}'.format(dst))
|
||||||
return 0
|
return 0, None
|
||||||
if os.path.lexists(dst):
|
if os.path.lexists(dst):
|
||||||
samerights = False
|
samerights = False
|
||||||
try:
|
try:
|
||||||
@@ -298,12 +332,12 @@ class Installer:
|
|||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == errno.ENOENT:
|
if e.errno == errno.ENOENT:
|
||||||
# broken symlink
|
# broken symlink
|
||||||
self.log.err('broken symlink {}'.format(dst))
|
err = 'broken symlink {}'.format(dst)
|
||||||
return -1
|
return -1, err
|
||||||
if self.diff and self._fake_diff(dst, content) and samerights:
|
if self.diff and self._fake_diff(dst, content) and samerights:
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('{} is the same'.format(dst))
|
self.log.dbg('{} is the same'.format(dst))
|
||||||
return 1
|
return 1, None
|
||||||
if self.safe:
|
if self.safe:
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('change detected for {}'.format(dst))
|
self.log.dbg('change detected for {}'.format(dst))
|
||||||
@@ -311,32 +345,33 @@ class Installer:
|
|||||||
self._diff_before_write(src, dst, content)
|
self._diff_before_write(src, dst, content)
|
||||||
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
||||||
self.log.warn('ignoring {}'.format(dst))
|
self.log.warn('ignoring {}'.format(dst))
|
||||||
return 1
|
return 1, None
|
||||||
overwrite = True
|
overwrite = True
|
||||||
if self.backup and os.path.lexists(dst):
|
if self.backup and os.path.lexists(dst):
|
||||||
self._backup(dst)
|
self._backup(dst)
|
||||||
base = os.path.dirname(dst)
|
base = os.path.dirname(dst)
|
||||||
if not self._create_dirs(base):
|
if not self._create_dirs(base):
|
||||||
self.log.err('creating directory for {}'.format(dst))
|
err = 'creating directory for {}'.format(dst)
|
||||||
return -1
|
return -1, err
|
||||||
if not self._exec_pre_actions(actions):
|
r, e = self._exec_pre_actions(actions)
|
||||||
return -1
|
if not r:
|
||||||
|
return -1, e
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('write content to {}'.format(dst))
|
self.log.dbg('write content to {}'.format(dst))
|
||||||
# re-check in case action created the file
|
# re-check in case action created the file
|
||||||
if self.safe and not overwrite and os.path.lexists(dst):
|
if self.safe and not overwrite and os.path.lexists(dst):
|
||||||
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
||||||
self.log.warn('ignoring {}'.format(dst))
|
self.log.warn('ignoring {}'.format(dst))
|
||||||
return 1
|
return 1, None
|
||||||
# write the file
|
# write the file
|
||||||
try:
|
try:
|
||||||
with open(dst, 'wb') as f:
|
with open(dst, 'wb') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
except NotADirectoryError as e:
|
except NotADirectoryError as e:
|
||||||
self.log.err('opening dest file: {}'.format(e))
|
err = 'opening dest file: {}'.format(e)
|
||||||
return -1
|
return -1, err
|
||||||
os.chmod(dst, rights)
|
os.chmod(dst, rights)
|
||||||
return 0
|
return 0, None
|
||||||
|
|
||||||
def _diff_before_write(self, src, dst, src_content):
|
def _diff_before_write(self, src, dst, src_content):
|
||||||
"""diff before writing when using --showdiff - not efficient"""
|
"""diff before writing when using --showdiff - not efficient"""
|
||||||
@@ -355,7 +390,7 @@ class Installer:
|
|||||||
def _create_dirs(self, directory):
|
def _create_dirs(self, directory):
|
||||||
"""mkdir -p <directory>"""
|
"""mkdir -p <directory>"""
|
||||||
if not self.create and not os.path.exists(directory):
|
if not self.create and not os.path.exists(directory):
|
||||||
return False
|
return False,
|
||||||
if os.path.exists(directory):
|
if os.path.exists(directory):
|
||||||
return True
|
return True
|
||||||
if self.dry:
|
if self.dry:
|
||||||
@@ -390,7 +425,7 @@ class Installer:
|
|||||||
def _exec_pre_actions(self, actions):
|
def _exec_pre_actions(self, actions):
|
||||||
"""execute pre-actions if any"""
|
"""execute pre-actions if any"""
|
||||||
if self.action_executed:
|
if self.action_executed:
|
||||||
return True
|
return True, None
|
||||||
for action in actions:
|
for action in actions:
|
||||||
if self.dry:
|
if self.dry:
|
||||||
self.log.dry('would execute action: {}'.format(action))
|
self.log.dry('would execute action: {}'.format(action))
|
||||||
@@ -398,10 +433,10 @@ class Installer:
|
|||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('executing pre action {}'.format(action))
|
self.log.dbg('executing pre action {}'.format(action))
|
||||||
if not action.execute():
|
if not action.execute():
|
||||||
self.log.err('pre-action {} failed'.format(action.key))
|
err = 'pre-action \"{}\" failed'.format(action.key)
|
||||||
return False
|
return False, err
|
||||||
self.action_executed = True
|
self.action_executed = True
|
||||||
return True
|
return True, None
|
||||||
|
|
||||||
def _install_to_temp(self, templater, src, dst, tmpdir):
|
def _install_to_temp(self, templater, src, dst, tmpdir):
|
||||||
"""install a dotfile to a tempdir"""
|
"""install a dotfile to a tempdir"""
|
||||||
|
|||||||
115
tests-ng/ext-actions.sh
Executable file
115
tests-ng/ext-actions.sh
Executable file
@@ -0,0 +1,115 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
# Copyright (c) 2019, deadc0de6
|
||||||
|
#
|
||||||
|
# test external actions
|
||||||
|
# returns 1 in case of error
|
||||||
|
#
|
||||||
|
|
||||||
|
# exit on first error
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# all this crap to get current path
|
||||||
|
rl="readlink -f"
|
||||||
|
if ! ${rl} "${0}" >/dev/null 2>&1; then
|
||||||
|
rl="realpath"
|
||||||
|
|
||||||
|
if ! hash ${rl}; then
|
||||||
|
echo "\"${rl}\" not found !" && exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
cur=$(dirname "$(${rl} "${0}")")
|
||||||
|
|
||||||
|
#hash dotdrop >/dev/null 2>&1
|
||||||
|
#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1
|
||||||
|
|
||||||
|
#echo "called with ${1}"
|
||||||
|
|
||||||
|
# dotdrop path can be pass as argument
|
||||||
|
ddpath="${cur}/../"
|
||||||
|
[ "${1}" != "" ] && ddpath="${1}"
|
||||||
|
[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1
|
||||||
|
|
||||||
|
export PYTHONPATH="${ddpath}:${PYTHONPATH}"
|
||||||
|
bin="python3 -m dotdrop.dotdrop"
|
||||||
|
|
||||||
|
echo "dotdrop path: ${ddpath}"
|
||||||
|
echo "pythonpath: ${PYTHONPATH}"
|
||||||
|
|
||||||
|
# get the helpers
|
||||||
|
source ${cur}/helpers
|
||||||
|
|
||||||
|
echo -e "\e[96m\e[1m==> RUNNING $(basename $BASH_SOURCE) <==\e[0m"
|
||||||
|
|
||||||
|
################################################################
|
||||||
|
# this is the test
|
||||||
|
################################################################
|
||||||
|
|
||||||
|
# the action temp
|
||||||
|
tmpa=`mktemp -d --suffix='-dotdrop-tests'`
|
||||||
|
# the dotfile source
|
||||||
|
tmps=`mktemp -d --suffix='-dotdrop-tests'`
|
||||||
|
mkdir -p ${tmps}/dotfiles
|
||||||
|
# the dotfile destination
|
||||||
|
tmpd=`mktemp -d --suffix='-dotdrop-tests'`
|
||||||
|
|
||||||
|
act="${tmps}/actions.yaml"
|
||||||
|
cat > ${act} << _EOF
|
||||||
|
actions:
|
||||||
|
pre:
|
||||||
|
preaction: echo 'pre' > ${tmpa}/pre
|
||||||
|
post:
|
||||||
|
postaction: echo 'post' > ${tmpa}/post
|
||||||
|
nakedaction: echo 'naked' > ${tmpa}/naked
|
||||||
|
overwrite: echo 'over' > ${tmpa}/write
|
||||||
|
_EOF
|
||||||
|
|
||||||
|
# create the config file
|
||||||
|
cfg="${tmps}/config.yaml"
|
||||||
|
|
||||||
|
cat > ${cfg} << _EOF
|
||||||
|
config:
|
||||||
|
backup: true
|
||||||
|
create: true
|
||||||
|
dotpath: dotfiles
|
||||||
|
import_actions:
|
||||||
|
- ${tmps}/actions.yaml
|
||||||
|
actions:
|
||||||
|
overwrite: echo 'write' > ${tmpa}/write
|
||||||
|
dotfiles:
|
||||||
|
f_abc:
|
||||||
|
dst: ${tmpd}/abc
|
||||||
|
src: abc
|
||||||
|
actions:
|
||||||
|
- preaction
|
||||||
|
- postaction
|
||||||
|
- nakedaction
|
||||||
|
- overwrite
|
||||||
|
profiles:
|
||||||
|
p1:
|
||||||
|
dotfiles:
|
||||||
|
- f_abc
|
||||||
|
_EOF
|
||||||
|
#cat ${cfg}
|
||||||
|
|
||||||
|
# create the dotfile
|
||||||
|
echo "test" > ${tmps}/dotfiles/abc
|
||||||
|
|
||||||
|
# install
|
||||||
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1
|
||||||
|
|
||||||
|
# checks
|
||||||
|
[ ! -e ${tmpa}/pre ] && exit 1
|
||||||
|
grep pre ${tmpa}/pre >/dev/null
|
||||||
|
[ ! -e ${tmpa}/post ] && exit 1
|
||||||
|
grep post ${tmpa}/post >/dev/null
|
||||||
|
[ ! -e ${tmpa}/naked ] && exit 1
|
||||||
|
grep naked ${tmpa}/naked >/dev/null
|
||||||
|
[ ! -e ${tmpa}/write ] && exit 1
|
||||||
|
grep write ${tmpa}/write >/dev/null
|
||||||
|
|
||||||
|
## CLEANING
|
||||||
|
rm -rf ${tmps} ${tmpd} ${tmpa}
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
@@ -77,14 +77,14 @@ _EOF
|
|||||||
# create the dotfile
|
# create the dotfile
|
||||||
mkdir -p ${tmps}/dotfiles/d1
|
mkdir -p ${tmps}/dotfiles/d1
|
||||||
echo "{{@@ var1 @@}}" > ${tmps}/dotfiles/d1/empty
|
echo "{{@@ var1 @@}}" > ${tmps}/dotfiles/d1/empty
|
||||||
echo "not empty" >> ${tmps}/dotfiles/d1/notempty
|
echo "not empty" > ${tmps}/dotfiles/d1/notempty
|
||||||
|
|
||||||
# install
|
# install
|
||||||
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
||||||
|
|
||||||
# test existence
|
# test existence
|
||||||
[ -e ${tmpd}/d1/empty ] && exit 1
|
[ -e ${tmpd}/d1/empty ] && echo 'empty should not exist' && exit 1
|
||||||
[ ! -e ${tmpd}/d1/notempty ] && exit 1
|
[ ! -e ${tmpd}/d1/notempty ] && echo 'not empty should exist' && exit 1
|
||||||
|
|
||||||
# through the dotfile
|
# through the dotfile
|
||||||
cat > ${cfg} << _EOF
|
cat > ${cfg} << _EOF
|
||||||
@@ -112,8 +112,8 @@ rm -rf ${tmpd}/*
|
|||||||
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
||||||
|
|
||||||
# test existence
|
# test existence
|
||||||
[ -e ${tmpd}/d1/empty ] && exit 1
|
[ -e ${tmpd}/d1/empty ] && echo 'empty should not exist' && exit 1
|
||||||
[ ! -e ${tmpd}/d1/notempty ] && exit 1
|
[ ! -e ${tmpd}/d1/notempty ] && echo 'not empty should exist' && exit 1
|
||||||
|
|
||||||
## CLEANING
|
## CLEANING
|
||||||
rm -rf ${tmps} ${tmpd}
|
rm -rf ${tmps} ${tmpd}
|
||||||
|
|||||||
@@ -79,7 +79,8 @@ class TestConfig(unittest.TestCase):
|
|||||||
|
|
||||||
# save the new config
|
# save the new config
|
||||||
with open(confpath, 'w') as f:
|
with open(confpath, 'w') as f:
|
||||||
yaml.dump(content, f, default_flow_style=False, indent=2)
|
yaml.safe_dump(content, f, default_flow_style=False,
|
||||||
|
indent=2)
|
||||||
|
|
||||||
# do the tests
|
# do the tests
|
||||||
conf = Cfg(confpath)
|
conf = Cfg(confpath)
|
||||||
@@ -109,7 +110,8 @@ class TestConfig(unittest.TestCase):
|
|||||||
|
|
||||||
# save the new config
|
# save the new config
|
||||||
with open(confpath, 'w') as f:
|
with open(confpath, 'w') as f:
|
||||||
yaml.dump(content, f, default_flow_style=False, indent=2)
|
yaml.safe_dump(content, f, default_flow_style=False,
|
||||||
|
indent=2)
|
||||||
|
|
||||||
# do the tests
|
# do the tests
|
||||||
conf = Cfg(confpath)
|
conf = Cfg(confpath)
|
||||||
|
|||||||
@@ -266,12 +266,12 @@ exec bspwm
|
|||||||
logger = MagicMock()
|
logger = MagicMock()
|
||||||
installer.log.err = logger
|
installer.log.err = logger
|
||||||
|
|
||||||
res = installer.link_children(templater=MagicMock(), src=src,
|
res, err = installer.link_children(templater=MagicMock(), src=src,
|
||||||
dst='/dev/null', actions=[])
|
dst='/dev/null', actions=[])
|
||||||
|
|
||||||
self.assertEqual(res, [])
|
self.assertFalse(res)
|
||||||
logger.assert_called_with('source dotfile does not exist: {}'
|
e = 'source dotfile does not exist: {}'.format(src)
|
||||||
.format(src))
|
self.assertEqual(err, e)
|
||||||
|
|
||||||
def test_fails_when_src_file(self):
|
def test_fails_when_src_file(self):
|
||||||
"""test fails when src file"""
|
"""test fails when src file"""
|
||||||
@@ -288,14 +288,13 @@ exec bspwm
|
|||||||
installer.log.err = logger
|
installer.log.err = logger
|
||||||
|
|
||||||
# pass src file not src dir
|
# pass src file not src dir
|
||||||
res = installer.link_children(templater=templater, src=src,
|
res, err = installer.link_children(templater=templater, src=src,
|
||||||
dst='/dev/null', actions=[])
|
dst='/dev/null', actions=[])
|
||||||
|
|
||||||
# ensure nothing performed
|
# ensure nothing performed
|
||||||
self.assertEqual(res, [])
|
self.assertFalse(res)
|
||||||
# ensure logger logged error
|
e = 'source dotfile is not a directory: {}'.format(src)
|
||||||
logger.assert_called_with('source dotfile is not a directory: {}'
|
self.assertEqual(err, e)
|
||||||
.format(src))
|
|
||||||
|
|
||||||
def test_creates_dst(self):
|
def test_creates_dst(self):
|
||||||
"""test creates dst"""
|
"""test creates dst"""
|
||||||
|
|||||||
Reference in New Issue
Block a user