mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 21:14:00 +00:00
now pre action are only executed if dotfile is installed (#59)
This commit is contained in:
@@ -93,6 +93,7 @@ def install(opts, conf, temporary=False):
|
|||||||
debug=opts['debug'], totemp=tmpdir)
|
debug=opts['debug'], totemp=tmpdir)
|
||||||
installed = []
|
installed = []
|
||||||
for dotfile in dotfiles:
|
for dotfile in dotfiles:
|
||||||
|
preactions = []
|
||||||
if dotfile.actions and Cfg.key_actions_pre in dotfile.actions:
|
if dotfile.actions and Cfg.key_actions_pre in dotfile.actions:
|
||||||
for action in dotfile.actions[Cfg.key_actions_pre]:
|
for action in dotfile.actions[Cfg.key_actions_pre]:
|
||||||
if opts['dry']:
|
if opts['dry']:
|
||||||
@@ -100,11 +101,11 @@ def install(opts, conf, temporary=False):
|
|||||||
else:
|
else:
|
||||||
if opts['debug']:
|
if opts['debug']:
|
||||||
LOG.dbg('executing pre action {}'.format(action))
|
LOG.dbg('executing pre action {}'.format(action))
|
||||||
action.execute()
|
preactions.append(action)
|
||||||
if opts['debug']:
|
if opts['debug']:
|
||||||
LOG.dbg('installing {}'.format(dotfile))
|
LOG.dbg('installing {}'.format(dotfile))
|
||||||
if hasattr(dotfile, 'link') and dotfile.link:
|
if hasattr(dotfile, 'link') and dotfile.link:
|
||||||
r = inst.link(t, dotfile.src, dotfile.dst)
|
r = inst.link(t, dotfile.src, dotfile.dst, actions=preactions)
|
||||||
else:
|
else:
|
||||||
src = dotfile.src
|
src = dotfile.src
|
||||||
tmp = None
|
tmp = None
|
||||||
@@ -113,7 +114,7 @@ def install(opts, conf, temporary=False):
|
|||||||
if not tmp:
|
if not tmp:
|
||||||
continue
|
continue
|
||||||
src = tmp
|
src = tmp
|
||||||
r = inst.install(t, src, dotfile.dst)
|
r = inst.install(t, src, dotfile.dst, actions=preactions)
|
||||||
if tmp:
|
if tmp:
|
||||||
tmp = os.path.join(opts['dotpath'], tmp)
|
tmp = os.path.join(opts['dotpath'], tmp)
|
||||||
if os.path.exists(tmp):
|
if os.path.exists(tmp):
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class Installer:
|
|||||||
self.comparing = False
|
self.comparing = False
|
||||||
self.log = Logger()
|
self.log = Logger()
|
||||||
|
|
||||||
def install(self, templater, src, dst):
|
def install(self, templater, src, dst, actions=[]):
|
||||||
"""install the src to dst using a template"""
|
"""install the src to dst using a template"""
|
||||||
src = os.path.join(self.base, os.path.expanduser(src))
|
src = os.path.join(self.base, os.path.expanduser(src))
|
||||||
dst = os.path.expanduser(dst)
|
dst = os.path.expanduser(dst)
|
||||||
@@ -46,27 +46,27 @@ class Installer:
|
|||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('install {} to {}'.format(src, dst))
|
self.log.dbg('install {} to {}'.format(src, dst))
|
||||||
if os.path.isdir(src):
|
if os.path.isdir(src):
|
||||||
return self._handle_dir(templater, src, dst)
|
return self._handle_dir(templater, src, dst, actions=actions)
|
||||||
return self._handle_file(templater, src, dst)
|
return self._handle_file(templater, src, dst, actions=actions)
|
||||||
|
|
||||||
def link(self, templater, src, dst):
|
def link(self, templater, src, dst, actions=[]):
|
||||||
"""set src as the link target of dst"""
|
"""set src as the link target of dst"""
|
||||||
src = os.path.join(self.base, os.path.expanduser(src))
|
src = os.path.join(self.base, os.path.expanduser(src))
|
||||||
dst = os.path.expanduser(dst)
|
dst = os.path.expanduser(dst)
|
||||||
if self.totemp:
|
if self.totemp:
|
||||||
return self.install(templater, src, dst)
|
return self.install(templater, src, dst, actions=actions)
|
||||||
|
|
||||||
if Templategen.is_template(src):
|
if Templategen.is_template(src):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
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)
|
||||||
if not self.install(templater, src, tmp):
|
if not self.install(templater, src, tmp, actions=actions):
|
||||||
return []
|
return []
|
||||||
src = tmp
|
src = tmp
|
||||||
return self._link(src, dst)
|
return self._link(src, dst, actions=actions)
|
||||||
|
|
||||||
def _link(self, src, dst):
|
def _link(self, src, dst, actions=[]):
|
||||||
"""set src as a link target of dst"""
|
"""set src as a link target of dst"""
|
||||||
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):
|
||||||
@@ -93,11 +93,12 @@ class Installer:
|
|||||||
if not self._create_dirs(base):
|
if not self._create_dirs(base):
|
||||||
self.log.err('creating directory for \"{}\"'.format(dst))
|
self.log.err('creating directory for \"{}\"'.format(dst))
|
||||||
return []
|
return []
|
||||||
|
self._exec_pre_actions(actions)
|
||||||
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 [(src, dst)]
|
||||||
|
|
||||||
def _handle_file(self, templater, src, dst):
|
def _handle_file(self, templater, src, dst, actions=[]):
|
||||||
"""install src to dst when is a file"""
|
"""install src to dst when is a file"""
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('generate template for {}'.format(src))
|
self.log.dbg('generate template for {}'.format(src))
|
||||||
@@ -113,7 +114,7 @@ class Installer:
|
|||||||
self.log.err('source dotfile does not exist: \"{}\"'.format(src))
|
self.log.err('source dotfile does not exist: \"{}\"'.format(src))
|
||||||
return []
|
return []
|
||||||
st = os.stat(src)
|
st = os.stat(src)
|
||||||
ret = self._write(dst, content, st.st_mode)
|
ret = self._write(dst, content, st.st_mode, actions=actions)
|
||||||
if ret < 0:
|
if ret < 0:
|
||||||
self.log.err('installing \"{}\" to \"{}\"'.format(src, dst))
|
self.log.err('installing \"{}\" to \"{}\"'.format(src, dst))
|
||||||
return []
|
return []
|
||||||
@@ -127,18 +128,21 @@ class Installer:
|
|||||||
return [(src, dst)]
|
return [(src, dst)]
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _handle_dir(self, templater, src, dst):
|
def _handle_dir(self, templater, src, dst, actions=[]):
|
||||||
"""install src to dst when is a directory"""
|
"""install src to dst when is a directory"""
|
||||||
ret = []
|
ret = []
|
||||||
self._create_dirs(dst)
|
if not self._create_dirs(dst):
|
||||||
|
return []
|
||||||
# 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))
|
res = self._handle_file(templater, f, os.path.join(dst, entry),
|
||||||
|
actions=actions)
|
||||||
ret.extend(res)
|
ret.extend(res)
|
||||||
else:
|
else:
|
||||||
res = self._handle_dir(templater, f, os.path.join(dst, entry))
|
res = self._handle_dir(templater, f, os.path.join(dst, entry),
|
||||||
|
actions=actions)
|
||||||
ret.extend(res)
|
ret.extend(res)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@@ -149,7 +153,7 @@ class Installer:
|
|||||||
cur = f.read()
|
cur = f.read()
|
||||||
return cur == content
|
return cur == content
|
||||||
|
|
||||||
def _write(self, dst, content, rights):
|
def _write(self, dst, content, rights, actions=[]):
|
||||||
"""write content to file
|
"""write content to file
|
||||||
return 0 for success,
|
return 0 for success,
|
||||||
1 when already exists
|
1 when already exists
|
||||||
@@ -174,6 +178,7 @@ class Installer:
|
|||||||
return -1
|
return -1
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('write content to {}'.format(dst))
|
self.log.dbg('write content to {}'.format(dst))
|
||||||
|
self._exec_pre_actions(actions)
|
||||||
try:
|
try:
|
||||||
with open(dst, 'wb') as f:
|
with open(dst, 'wb') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
@@ -213,6 +218,11 @@ class Installer:
|
|||||||
sub = path.lstrip(os.sep)
|
sub = path.lstrip(os.sep)
|
||||||
return os.path.join(newdir, sub)
|
return os.path.join(newdir, sub)
|
||||||
|
|
||||||
|
def _exec_pre_actions(self, actions):
|
||||||
|
"""execute pre-actions if any"""
|
||||||
|
for action in actions:
|
||||||
|
action.execute()
|
||||||
|
|
||||||
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"""
|
||||||
tmpdst = self._pivot_path(dst, tmpdir)
|
tmpdst = self._pivot_path(dst, tmpdir)
|
||||||
|
|||||||
104
tests-ng/actions-pre.sh
Executable file
104
tests-ng/actions-pre.sh
Executable file
@@ -0,0 +1,104 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
# Copyright (c) 2017, deadc0de6
|
||||||
|
#
|
||||||
|
# test pre action execution
|
||||||
|
# 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 "RUNNING $(basename $BASH_SOURCE)"
|
||||||
|
|
||||||
|
################################################################
|
||||||
|
# this is the test
|
||||||
|
################################################################
|
||||||
|
|
||||||
|
# the action temp
|
||||||
|
tmpa=`mktemp -d`
|
||||||
|
# the dotfile source
|
||||||
|
tmps=`mktemp -d`
|
||||||
|
mkdir -p ${tmps}/dotfiles
|
||||||
|
# the dotfile destination
|
||||||
|
tmpd=`mktemp -d`
|
||||||
|
|
||||||
|
# create the config file
|
||||||
|
cfg="${tmps}/config.yaml"
|
||||||
|
|
||||||
|
cat > ${cfg} << _EOF
|
||||||
|
actions:
|
||||||
|
pre:
|
||||||
|
preaction: echo 'pre' > ${tmpa}/pre
|
||||||
|
nakedaction: echo 'naked' > ${tmpa}/naked
|
||||||
|
config:
|
||||||
|
backup: true
|
||||||
|
create: true
|
||||||
|
dotpath: dotfiles
|
||||||
|
dotfiles:
|
||||||
|
f_abc:
|
||||||
|
dst: ${tmpd}/abc
|
||||||
|
src: abc
|
||||||
|
actions:
|
||||||
|
- preaction
|
||||||
|
- nakedaction
|
||||||
|
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}/naked ] && exit 1
|
||||||
|
grep naked ${tmpa}/naked >/dev/null
|
||||||
|
|
||||||
|
# remove the pre action result and re-run
|
||||||
|
rm ${tmpa}/pre
|
||||||
|
|
||||||
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1
|
||||||
|
[ -e ${tmpa}/pre ] && exit 1
|
||||||
|
|
||||||
|
## CLEANING
|
||||||
|
rm -rf ${tmps} ${tmpd} ${tmpa}
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user