1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 12:34:16 +00:00

better handle error on dotfile installation for #111

This commit is contained in:
deadc0de6
2019-03-31 22:19:47 +02:00
parent d6c4377dec
commit 74054fbf44
3 changed files with 126 additions and 101 deletions

View File

@@ -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

View File

@@ -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)
@@ -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,61 @@ 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 = [] ret = True, 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), res, err = self._handle_file(templater, f,
actions=actions, noempty=noempty) os.path.join(dst, entry),
ret.extend(res) actions=actions,
noempty=noempty)
if not res:
ret = res, err
break
else: else:
res = self._handle_dir(templater, f, os.path.join(dst, entry), res, err = self._handle_dir(templater, f,
actions=actions, noempty=noempty) os.path.join(dst, entry),
ret.extend(res) actions=actions,
noempty=noempty)
if not res:
ret = res, err
break
return ret return ret
def _fake_diff(self, dst, content): def _fake_diff(self, dst, content):
@@ -284,13 +307,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 +321,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 +334,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 +379,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 +414,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 +422,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"""

View File

@@ -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"""