1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 22:39:43 +00:00

remove error reported by logger for #268

This commit is contained in:
deadc0de6
2020-10-26 13:22:13 +01:00
parent 0ae35fa071
commit bd09d4070f
4 changed files with 38 additions and 28 deletions

View File

@@ -154,7 +154,7 @@ def cmd_install(o):
if tmp:
tmp = os.path.join(o.dotpath, tmp)
if os.path.exists(tmp):
remove(tmp)
remove(tmp, LOG)
if r:
# dotfile was installed
if not o.install_temporary:
@@ -287,7 +287,7 @@ def cmd_compare(o, tmp):
if tmpsrc:
tmpsrc = os.path.join(o.dotpath, tmpsrc)
if os.path.exists(tmpsrc):
remove(tmpsrc)
remove(tmpsrc, LOG)
# print diff result
if diff == '':
@@ -584,11 +584,7 @@ def cmd_remove(o):
# remove dotfile from dotpath
dtpath = os.path.join(o.dotpath, dotfile.src)
try:
remove(dtpath)
except OSError:
# did not exist
pass
remove(dtpath, LOG)
# remove empty directory
parent = os.path.dirname(dtpath)
# remove any empty parent up to dotpath
@@ -597,7 +593,7 @@ def cmd_remove(o):
msg = 'Remove empty dir \"{}\"'.format(parent)
if o.safe and not LOG.ask(msg):
break
remove(parent)
remove(parent, LOG)
parent = os.path.dirname(parent)
removed.append(dotfile.key)
@@ -669,7 +665,7 @@ def apply_trans(dotpath, dotfile, templater, debug=False):
msg = 'transformation \"{}\" failed for {}'
LOG.err(msg.format(trans.key, dotfile.key))
if new_src and os.path.exists(new_src):
remove(new_src)
remove(new_src, LOG)
return None
return new_src
@@ -728,7 +724,7 @@ def main():
tmp = get_tmpdir()
ret = cmd_compare(o, tmp)
# clean tmp directory
remove(tmp)
remove(tmp, LOG)
elif o.cmd_import:
# import dotfile(s)

View File

@@ -537,7 +537,7 @@ class Installer:
diff = utils.diff(modified=src, original=dst, raw=False,
diff_cmd=self.diff_cmd)
if tmp:
utils.remove(tmp, quiet=True)
utils.remove(tmp, logger=self.log)
if not quiet and diff:
self._print_diff(src, dst, diff)

View File

@@ -114,7 +114,7 @@ class Updater:
ret = self._handle_file(new_path, dtpath)
# clean temporary files
if new_path != path and os.path.exists(new_path):
remove(new_path)
remove(new_path, logger=self.log)
return ret
def _apply_trans_w(self, path, dotfile):
@@ -133,7 +133,7 @@ class Updater:
msg = 'transformation \"{}\" failed for {}'
self.log.err(msg.format(trans.key, dotfile.key))
if os.path.exists(tmp):
remove(tmp)
remove(tmp, logger=self.log)
return None
return tmp
@@ -273,7 +273,7 @@ class Updater:
self.log.dbg('rm -r {}'.format(old))
if not self._confirm_rm_r(old):
continue
remove(old)
remove(old, logger=self.log)
self.log.sub('\"{}\" dir removed'.format(old))
# handle files diff
@@ -326,7 +326,7 @@ class Updater:
continue
if self.debug:
self.log.dbg('rm {}'.format(new))
remove(new)
remove(new, logger=self.log)
self.log.sub('\"{}\" removed'.format(new))
# compare rights

View File

@@ -103,28 +103,42 @@ def get_unique_tmp_name():
return os.path.join(tempfile.gettempdir(), unique)
def remove(path, quiet=False):
"""remove a file/directory/symlink"""
def remove(path, logger=None):
"""
remove a file/directory/symlink
if logger is defined, OSError are catched
and printed to logger.warn instead of being forwarded
as OSError
"""
if not path:
return
if not os.path.lexists(path):
if quiet:
err = 'File not found: {}'.format(path)
if logger:
logger.warn(err)
return
raise OSError("File not found: {}".format(path))
raise OSError(err)
if os.path.normpath(os.path.expanduser(path)) in NOREMOVE:
if quiet:
return
err = 'Dotdrop refuses to remove {}'.format(path)
if logger:
logger.warn(err)
return
LOG.err(err)
raise OSError(err)
if os.path.islink(path) or os.path.isfile(path):
os.unlink(path)
elif os.path.isdir(path):
rmtree(path)
else:
if quiet:
try:
if os.path.islink(path) or os.path.isfile(path):
os.unlink(path)
elif os.path.isdir(path):
rmtree(path)
else:
err = 'Unsupported file type for deletion: {}'.format(path)
raise OSError(err)
except Exception as e:
err = str(e)
if logger:
logger.warn(err)
return
raise OSError("Unsupported file type for deletion: {}".format(path))
raise OSError(err)
def samefile(path1, path2):