1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-05 06:48:49 +00:00

modes install and compare

This commit is contained in:
deadc0de6
2020-11-12 22:24:09 +01:00
parent c07a865809
commit 2bb684d3da
8 changed files with 349 additions and 108 deletions

View File

@@ -76,16 +76,12 @@ On `install` the following rules are applied:
* if `chmod` is specified, it will be applied to the installed dotfile
* if file exists and its permissions differ from `umask` and no `chmod` is specified user needs
to confirm installation (unless `-f --force` is used) **TODO unless permissions match existing file**
to confirm installation (unless `-f --force` is used or the permission match the dotfile in the `dotpath`)
On `update`:
* **TODO**
One `compare`:
* **TODO**
## Symlink dotfiles
Dotdrop is able to install dotfiles in three different ways

View File

@@ -657,10 +657,6 @@ class CfgYaml:
err = 'bad format for chmod: {}'.format(val)
self._log.err(err)
raise YamlException('config content error: {}'.format(err))
if v[self.key_dotfile_link] == self.lnk_children:
err = 'incompatible use of chmod and link_children'
self._log.err(err)
raise YamlException('config content error: {}'.format(err))
v[self.key_dotfile_chmod] = val
return new

View File

@@ -10,7 +10,8 @@ import filecmp
# local imports
from dotdrop.logger import Logger
from dotdrop.utils import must_ignore, uniq_list, diff
from dotdrop.utils import must_ignore, uniq_list, diff, \
get_file_perm
class Comparator:
@@ -31,6 +32,7 @@ class Comparator:
if self.debug:
self.log.dbg('comparing {} and {}'.format(left, right))
self.log.dbg('ignore pattern(s): {}'.format(ignore))
# test type of file
if os.path.isdir(left) and not os.path.isdir(right):
return '\"{}\" is a dir while \"{}\" is a file\n'.format(left,
@@ -38,14 +40,32 @@ class Comparator:
if not os.path.isdir(left) and os.path.isdir(right):
return '\"{}\" is a file while \"{}\" is a dir\n'.format(left,
right)
# test content
if not os.path.isdir(left):
if self.debug:
self.log.dbg('is file')
return self._comp_file(left, right, ignore)
ret = self._comp_file(left, right, ignore)
if not ret:
ret = self._comp_mode(left, right)
return ret
if self.debug:
self.log.dbg('is directory')
return self._comp_dir(left, right, ignore)
ret = self._comp_dir(left, right, ignore)
if not ret:
ret = self._comp_mode(left, right)
return ret
def _comp_mode(self, left, right):
"""compare mode"""
left_mode = get_file_perm(left)
right_mode = get_file_perm(right)
if left_mode == right_mode:
return ''
ret = 'modes differ ({} vs {})\n'.format(left_mode, right_mode)
return ret
def _comp_file(self, left, right, ignore):
"""compare a file"""

View File

@@ -108,7 +108,8 @@ def _dotfile_install(o, dotfile, tmpdir=None):
# link_children
r, err = inst.link_children(t, dotfile.src, dotfile.dst,
actionexec=pre_actions_exec,
template=dotfile.template)
template=dotfile.template,
chmod=dotfile.chmod)
else:
# nolink
src = dotfile.src
@@ -342,7 +343,6 @@ def cmd_compare(o, tmp):
def cmd_update(o):
"""update the dotfile(s) from path(s) or key(s)"""
# TODO chmod
ret = True
paths = o.update_path
iskey = o.update_iskey

View File

@@ -73,36 +73,41 @@ class Installer:
- False, error_msg : error
- False, None : ignored
"""
if not self._check_chmod(dst, chmod):
err = 'ignoring "{}", nothing installed'.format(dst)
if not self._check_chmod(src, dst, chmod):
err = 'ignoring "{}", not installed'.format(dst)
return False, err
r, err = self._install(templater, src, dst,
actionexec=actionexec,
noempty=noempty,
ignore=ignore,
template=template)
if r and chmod:
os.chmod(dst, int(chmod, 8))
template=template,
chmod=chmod)
return self._log_install(r, err)
def _install(self, templater, src, dst,
actionexec=None, noempty=False,
ignore=[], template=True):
ignore=[], template=True,
chmod=None):
"""install link:nolink"""
if self.debug:
self.log.dbg('installing \"{}\" to \"{}\"'.format(src, dst))
# check dst exists in config
if not dst or not src:
if self.debug:
self.log.dbg('empty dst for {}'.format(src))
return True, None
self.action_executed = False
# check source file exists
src = os.path.join(self.base, os.path.expanduser(src))
if not os.path.exists(src):
err = 'source dotfile does not exist: {}'.format(src)
return False, err
# check dst is valid
dst = os.path.expanduser(dst)
if self.totemp:
dst = self._pivot_path(dst, self.totemp)
@@ -110,20 +115,23 @@ class Installer:
# symlink loop
err = 'dotfile points to itself: {}'.format(dst)
return False, err
isdir = os.path.isdir(src)
if self.debug:
self.log.dbg('install {} to {}'.format(src, dst))
self.log.dbg('is a directory \"{}\": {}'.format(src, isdir))
self.log.dbg('\"{}\" is a directory: {}'.format(src, isdir))
if isdir:
b, e = self._install_dir(templater, src, dst,
actionexec=actionexec,
noempty=noempty, ignore=ignore,
template=template)
b, e = self._deploy_dir(templater, src, dst,
actionexec=actionexec,
noempty=noempty, ignore=ignore,
template=template,
chmod=chmod)
return b, e
b, e = self._install_file(templater, src, dst,
actionexec=actionexec,
noempty=noempty, ignore=ignore,
template=template)
b, e = self._deploy_file(templater, src, dst,
actionexec=actionexec,
noempty=noempty, ignore=ignore,
template=template,
chmod=chmod)
return b, e
def link(self, templater, src, dst, actionexec=None,
@@ -142,15 +150,16 @@ class Installer:
- False, error_msg : error
- False, None : ignored
"""
if not self._check_chmod(dst, chmod):
if not self._check_chmod(src, dst, chmod):
err = 'ignoring "{}", nothing installed'.format(dst)
return False, err
r, err = self._link(templater, src, dst,
actionexec=actionexec,
template=template)
if chmod:
os.chmod(dst, int(chmod, 8))
if chmod and not self.dry:
# apply mode
utils.chmod(dst, chmod, self.debug)
return self._log_install(r, err)
def _link(self, templater, src, dst, actionexec=None,
@@ -189,7 +198,7 @@ class Installer:
return b, e
def link_children(self, templater, src, dst, actionexec=None,
template=True):
template=True, chmod=None):
"""
link all files under a given directory
@templater: the templater
@@ -197,19 +206,23 @@ class Installer:
@dst: dotfile destination path in the FS
@actionexec: action executor callback
@template: template this dotfile
@chmod: file mode to apply
return
- True, None: success
- False, error_msg: error
- False, None, ignored
"""
if not self._check_chmod(src, dst, chmod):
err = 'ignoring "{}", nothing installed'.format(dst)
return False, err
r, err = self._link_children(templater, src, dst,
actionexec=actionexec,
template=template)
template=template, chmod=chmod)
return self._log_install(r, err)
def _link_children(self, templater, src, dst, actionexec=None,
template=True):
template=True, chmod=None):
"""install link:link_children"""
if self.debug:
self.log.dbg('link_children \"{}\" to \"{}\"'.format(src, dst))
@@ -245,7 +258,7 @@ class Installer:
]).format(dst)
if self.safe and not self.log.ask(msg):
err = 'ignoring "{}", nothing installed'.format(dst)
err = 'ignoring "{}", not installed'.format(dst)
return False, err
os.unlink(dst)
os.mkdir(dst)
@@ -258,25 +271,26 @@ class Installer:
installed = 0
for i in range(len(children)):
src = srcs[i]
dst = dsts[i]
subsrc = srcs[i]
subdst = dsts[i]
if self.debug:
self.log.dbg('symlink child {} to {}'.format(src, dst))
self.log.dbg('symlink child {} to {}'.format(subsrc, subdst))
if template and Templategen.is_template(src):
if template and Templategen.is_template(subsrc):
if self.debug:
self.log.dbg('dotfile is a template')
self.log.dbg('install to {} and symlink'
.format(self.workdir))
tmp = self._pivot_path(dst, self.workdir, striphome=True)
r, e = self.install(templater, src, tmp, actionexec=actionexec,
tmp = self._pivot_path(subdst, self.workdir, striphome=True)
r, e = self.install(templater, subsrc, tmp,
actionexec=actionexec,
template=template)
if not r and e and not os.path.exists(tmp):
continue
src = tmp
subsrc = tmp
ret, err = self._symlink(src, dst, actionexec=actionexec)
ret, err = self._symlink(subsrc, subdst, actionexec=actionexec)
if ret:
installed += 1
# void actionexec if dotfile installed
@@ -286,6 +300,10 @@ class Installer:
if err:
return ret, err
if chmod and not self.dry:
# apply mode
utils.chmod(dst, chmod, debug=self.debug)
return installed > 0, None
def _symlink(self, src, dst, actionexec=None):
@@ -350,9 +368,10 @@ class Installer:
tmp['_dotfile_sub_abs_dst'] = dst
return tmp
def _install_file(self, templater, src, dst,
actionexec=None, noempty=False,
ignore=[], template=True):
def _deploy_file(self, templater, src, dst,
actionexec=None, noempty=False,
ignore=[], template=True,
chmod=None):
"""install src to dst when is a file"""
if self.debug:
self.log.dbg('deploy file: {}'.format(src))
@@ -367,7 +386,7 @@ class Installer:
return False, None
if utils.samefile(src, dst):
# symlink loop
# loop
err = 'dotfile points to itself: {}'.format(dst)
return False, err
@@ -386,6 +405,7 @@ class Installer:
return False, str(e)
finally:
templater.restore_vars(saved)
# test is empty
if noempty and utils.content_empty(content):
if self.debug:
self.log.dbg('ignoring empty template: {}'.format(src))
@@ -393,10 +413,13 @@ class Installer:
if content is None:
err = 'empty template {}'.format(src)
return False, err
# write the file
ret, err = self._write(src, dst,
content=content,
actionexec=actionexec,
template=template)
template=template,
chmod=chmod)
# build return values
if ret < 0:
@@ -416,29 +439,36 @@ class Installer:
err = 'installing {} to {}'.format(src, dst)
return False, err
def _install_dir(self, templater, src, dst,
actionexec=None, noempty=False,
ignore=[], template=True):
def _deploy_dir(self, templater, src, dst,
actionexec=None, noempty=False,
ignore=[], template=True, chmod=None):
"""install src to dst when is a directory"""
if self.debug:
self.log.dbg('install dir {}'.format(src))
self.log.dbg('ignore empty: {}'.format(noempty))
self.log.dbg('deploy dir {}'.format(src))
# default to nothing installed and no error
ret = False, None
# create the directory anyway
if self.debug:
self.log.dbg('mkdir -p {}'.format(dst))
if not self._create_dirs(dst):
err = 'creating directory for {}'.format(dst)
return False, err
# handle all files in dir
for entry in os.listdir(src):
f = os.path.join(src, entry)
if self.debug:
self.log.dbg('deploy sub from {}: {}'.format(dst, entry))
if not os.path.isdir(f):
# is file
res, err = self._install_file(templater, f,
os.path.join(dst, entry),
actionexec=actionexec,
noempty=noempty,
ignore=ignore,
template=template)
res, err = self._deploy_file(templater, f,
os.path.join(dst, entry),
actionexec=actionexec,
noempty=noempty,
ignore=ignore,
template=template,
chmod=None)
if not res and err:
# error occured
ret = res, err
@@ -448,12 +478,13 @@ class Installer:
ret = True, None
else:
# is directory
res, err = self._install_dir(templater, f,
os.path.join(dst, entry),
actionexec=actionexec,
noempty=noempty,
ignore=ignore,
template=template)
res, err = self._deploy_dir(templater, f,
os.path.join(dst, entry),
actionexec=actionexec,
noempty=noempty,
ignore=ignore,
template=template,
chmod=None)
if not res and err:
# error occured
ret = res, err
@@ -461,20 +492,15 @@ class Installer:
elif res:
# something got installed
ret = True, None
if chmod and not self.dry:
# apply mode
utils.chmod(dst, chmod, debug=self.debug)
return ret
def _fake_diff(self, dst, content):
"""
fake diff by comparing file content with content
returns True if same
"""
cur = ''
with open(dst, 'br') as f:
cur = f.read()
return cur == content
def _write(self, src, dst, content=None,
actionexec=None, template=True):
actionexec=None, template=True,
chmod=None):
"""
copy dotfile / write content to file
return 0, None: for success,
@@ -487,11 +513,16 @@ class Installer:
if self.dry:
self.log.dry('would install {}'.format(dst))
return 0, None
if os.path.lexists(dst):
rights = os.stat(src).st_mode
if chmod:
rights = chmod
else:
rights = utils.get_file_perm(src)
samerights = False
try:
samerights = os.stat(dst).st_mode == rights
dstrights = utils.get_file_perm(dst)
samerights = dstrights == rights
except OSError as e:
if e.errno == errno.ENOENT:
# broken symlink
@@ -531,7 +562,7 @@ class Installer:
if not r:
return -1, e
if self.debug:
self.log.dbg('install dotfile to \"{}\"'.format(dst))
self.log.dbg('install file to \"{}\"'.format(dst))
# re-check in case action created the file
if self.safe and not overwrite and os.path.lexists(dst):
if not self.log.ask('Overwrite \"{}\"'.format(dst)):
@@ -556,6 +587,9 @@ class Installer:
shutil.copymode(src, dst)
except Exception as e:
return -1, str(e)
if chmod:
utils.chmod(dst, chmod, debug=self.debug)
return 0, None
def _diff_before_write(self, src, dst, content=None, quiet=False):
@@ -595,6 +629,7 @@ class Installer:
return True
if self.debug:
self.log.dbg('mkdir -p {}'.format(directory))
os.makedirs(directory)
return os.path.exists(directory)
@@ -681,12 +716,19 @@ class Installer:
self.log.dbg('install: IGNORED')
return boolean, err
def _check_chmod(self, path, chmod):
def _check_chmod(self, src, dst, chmod):
"""chmod file if needed and return True to continue"""
if chmod:
return True
if os.path.exists(path) and self.safe:
perms = utils.get_file_perm(path)
if not os.path.exists(dst):
return True
if not os.path.exists(src):
return True
cperms = utils.get_file_perm(src)
perms = utils.get_file_perm(dst)
if perms == cperms:
return True
if self.safe:
if perms & self.umask:
# perms and umask differ
msg = 'File mode ({}) differs from umask ({})'

View File

@@ -315,3 +315,10 @@ def get_umask():
def get_file_perm(path):
"""return file permission"""
return os.stat(path).st_mode & 0o777
def chmod(path, mode, debug=False):
cm = int(mode, 8)
if debug:
LOG.dbg('chmod {} {}'.format(oct(cm), path))
os.chmod(path, cm)

121
tests-ng/chmod-compare.sh Executable file
View File

@@ -0,0 +1,121 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2020, deadc0de6
#
# test chmod on compare
#
# 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"
hash coverage 2>/dev/null && bin="coverage run -a --source=dotdrop -m dotdrop.dotdrop" || true
echo "dotdrop path: ${ddpath}"
echo "pythonpath: ${PYTHONPATH}"
# get the helpers
source ${cur}/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)"
################################################################
# this is the test
################################################################
# the dotfile source
tmps=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
mkdir -p ${tmps}/dotfiles
# the dotfile destination
tmpd=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
#echo "dotfile destination: ${tmpd}"
# create the dotfile
dnormal="${tmpd}/dir_normal"
mkdir -p ${dnormal}
echo "dir_normal/f1" > ${dnormal}/file1
echo "dir_normal/f2" > ${dnormal}/file2
chmod 777 ${dnormal}
dlink="${tmpd}/dir_link"
mkdir -p ${dlink}
echo "dir_link/f1" > ${dlink}/file1
echo "dir_link/f2" > ${dlink}/file2
chmod 777 ${dlink}
dlinkchildren="${tmpd}/dir_link_children"
mkdir -p ${dlinkchildren}
echo "dir_linkchildren/f1" > ${dlinkchildren}/file1
echo "dir_linkchildren/f2" > ${dlinkchildren}/file2
chmod 777 ${dlinkchildren}
fnormal="${tmpd}/filenormal"
echo "filenormal" > ${fnormal}
chmod 777 ${fnormal}
flink="${tmpd}/filelink"
echo "filelink" > ${flink}
chmod 777 ${flink}
toimport="${dnormal} ${dlink} ${dlinkchildren} ${fnormal} ${flink}"
# create the config file
cfg="${tmps}/config.yaml"
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
dotfiles:
profiles:
_EOF
#cat ${cfg}
# import without --preserve-mode
for i in ${toimport}; do
cd ${ddpath} | ${bin} import -c ${cfg} -f -p p1 ${i}
done
#cat ${cfg}
# patch rights
chmod 700 ${dnormal}
chmod 700 ${dlink}
chmod 700 ${dlinkchildren}
chmod 700 ${fnormal}
chmod 700 ${flink}
set +e
cnt=`cd ${ddpath} | ${bin} compare -c ${cfg} -p p1 2>&1 | grep 'modes differ' | wc -l`
set -e
[ "${cnt}" != "5" ] && echo "compare modes failed" && exit 1
## CLEANING
rm -rf ${tmps} ${tmpd}
echo "OK"
exit 0

View File

@@ -6,13 +6,21 @@
# with files and directories
# with different link
#
# TODO
# - test for symlink templates
# - check for mode difference when install
# exit on first error
set -e
# $1 path
# $2 rights
has_rights()
{
echo "testing ${1} is ${2}"
[ ! -e "$1" ] && echo "`basename $1` does not exist" && exit 1
local mode=`stat -L -c '%a' "$1"`
[ "${mode}" != "$2" ] && echo "bad mode for `basename $1`" && exit 1
true
}
# all this crap to get current path
rl="readlink -f"
if ! ${rl} "${0}" >/dev/null 2>&1; then
@@ -73,6 +81,23 @@ chmod 644 ${tmpd}/exists
echo "existslink" > ${tmps}/dotfiles/existslink
chmod 644 ${tmpd}/exists
mkdir -p ${tmps}/dotfiles/direxists
echo "f1" > ${tmps}/dotfiles/direxists/f1
mkdir -p ${tmpd}/direxists
echo "f1" > ${tmpd}/direxists/f1
chmod 644 ${tmpd}/direxists/f1
chmod 744 ${tmpd}/direxists
mkdir -p ${tmps}/dotfiles/linkchildren
echo "f1" > ${tmps}/dotfiles/linkchildren/f1
mkdir -p ${tmps}/dotfiles/linkchildren/d1
echo "f2" > ${tmps}/dotfiles/linkchildren/d1/f2
echo '{{@@ profile @@}}' > ${tmps}/dotfiles/symlinktemplate
mkdir -p ${tmps}/dotfiles/symlinktemplatedir
echo "{{@@ profile @@}}" > ${tmps}/dotfiles/symlinktemplatedir/t
cat > ${cfg} << _EOF
config:
backup: true
@@ -101,6 +126,25 @@ dotfiles:
dst: ${tmpd}/existslink
chmod: 777
link: link
d_direxists:
src: direxists
dst: ${tmpd}/direxists
chmod: 777
d_linkchildren:
src: linkchildren
dst: ${tmpd}/linkchildren
chmod: 777
link: link_children
f_symlinktemplate:
src: symlinktemplate
dst: ${tmpd}/symlinktemplate
chmod: 777
link: link
d_symlinktemplatedir:
src: symlinktemplatedir
dst: ${tmpd}/symlinktemplatedir
chmod: 777
link: link
profiles:
p1:
dotfiles:
@@ -109,45 +153,60 @@ profiles:
- d_dir
- f_exists
- f_existslink
- d_direxists
- d_linkchildren
- f_symlinktemplate
- d_symlinktemplatedir
p2:
dotfiles:
- f_exists
- f_existslink
- d_linkchildren
- f_symlinktemplate
_EOF
#cat ${cfg}
# install
echo "first install round"
cd ${ddpath} | ${bin} install -c ${cfg} -f -p p1 -V ${i}
mode=`stat -c '%a' "${tmpd}/f777"`
[ "${mode}" != "777" ] && echo "bad mode for f777" && exit 1
has_rights "${tmpd}/f777" "777"
has_rights "${tmpd}/link" "777"
has_rights "${tmpd}/dir" "777"
has_rights "${tmpd}/exists" "777"
has_rights "${tmpd}/existslink" "777"
has_rights "${tmpd}/direxists" "777"
has_rights "${tmpd}/direxists/f1" "644"
has_rights "${tmpd}/linkchildren" "777"
has_rights "${tmpd}/linkchildren/f1" "644"
has_rights "${tmpd}/linkchildren/d1" "755"
has_rights "${tmpd}/linkchildren/d1/f2" "644"
has_rights "${tmpd}/symlinktemplate" "777"
mode=`stat -c '%a' "${tmpd}/link"`
[ "${mode}" != "777" ] && echo "bad mode for link" && exit 1
mode=`stat -c '%a' "${tmpd}/dir"`
[ "${mode}" != "777" ] && echo "bad mode for dir" && exit 1
mode=`stat -c '%a' "${tmpd}/exists"`
[ "${mode}" != "777" ] && echo "bad mode for exists" && exit 1
mode=`stat -c '%a' "${tmpd}/existslink"`
[ "${mode}" != "777" ] && echo "bad mode for existslink" && exit 1
grep 'p1' ${tmpd}/symlinktemplate
grep 'p1' ${tmpd}/symlinktemplatedir/t
## second round
echo "exists" > ${tmps}/dotfiles/exists
chmod 644 ${tmps}/dotfiles/exists
chmod 600 ${tmps}/dotfiles/exists
echo "exists" > ${tmpd}/exists
chmod 644 ${tmpd}/exists
chmod 600 ${tmpd}/exists
chmod 644 ${tmpd}/existslink
chmod 600 ${tmpd}/existslink
cd ${ddpath} | ${bin} install -c ${cfg} -p p2 -V ${i}
chmod 700 ${tmpd}/linkchildren
mode=`stat -c '%a' "${tmpd}/exists"`
[ "${mode}" != "777" ] && echo "bad mode for exists" && exit 1
chmod 600 ${tmpd}/symlinktemplate
mode=`stat -c '%a' "${tmpd}/existslink"`
[ "${mode}" != "777" ] && echo "bad mode for existslink" && exit 1
echo "second install round"
cd ${ddpath} | ${bin} install -c ${cfg} -p p2 -f -V ${i}
has_rights "${tmpd}/exists" "777"
has_rights "${tmpd}/existslink" "777"
has_rights "${tmpd}/linkchildren/f1" "644"
has_rights "${tmpd}/linkchildren/d1" "755"
has_rights "${tmpd}/linkchildren/d1/f2" "644"
has_rights "${tmpd}/symlinktemplate" "777"
## CLEANING
rm -rf ${tmps} ${tmpd}