1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 19:09:44 +00:00

fix update return value

This commit is contained in:
deadc0de6
2024-08-21 21:21:44 +02:00
committed by deadc0de
parent a617b95c1d
commit abdfa5896c
6 changed files with 43 additions and 25 deletions

View File

@@ -169,11 +169,13 @@ def _dotfile_compare(opts, dotfile, tmp):
if tmpsrc: if tmpsrc:
tmpsrc = os.path.join(opts.dotpath, tmpsrc) tmpsrc = os.path.join(opts.dotpath, tmpsrc)
if os.path.exists(tmpsrc): if os.path.exists(tmpsrc):
removepath(tmpsrc, LOG) # ignore error
removepath(tmpsrc, logger=LOG)
# clean tmp template dotfile if any # clean tmp template dotfile if any
if insttmp and os.path.exists(insttmp): if insttmp and os.path.exists(insttmp):
removepath(insttmp, LOG) # ignore error
removepath(insttmp, logger=LOG)
if diff != '': if diff != '':
# print diff results # print diff results
@@ -258,7 +260,8 @@ def _dotfile_install(opts, dotfile, tmpdir=None):
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):
removepath(tmp, LOG) # ignore error
removepath(tmp, logger=LOG)
# check result of installation # check result of installation
if ret: if ret:
@@ -322,6 +325,7 @@ def cmd_install(opts):
for root, _, files in os.walk(opts.workdir): for root, _, files in os.walk(opts.workdir):
for file in files: for file in files:
fpath = os.path.join(root, file) fpath = os.path.join(root, file)
# ignore error
removepath(fpath, logger=LOG) removepath(fpath, logger=LOG)
# execute profile pre-action # execute profile pre-action
@@ -721,7 +725,7 @@ def cmd_remove(opts):
# remove dotfile from dotpath # remove dotfile from dotpath
dtpath = os.path.join(opts.dotpath, dotfile.src) dtpath = os.path.join(opts.dotpath, dotfile.src)
removepath(dtpath, LOG) removepath(dtpath, logger=LOG)
# remove empty directory # remove empty directory
parent = os.path.dirname(dtpath) parent = os.path.dirname(dtpath)
# remove any empty parent up to dotpath # remove any empty parent up to dotpath
@@ -730,7 +734,8 @@ def cmd_remove(opts):
msg = f'Remove empty dir \"{parent}\"' msg = f'Remove empty dir \"{parent}\"'
if opts.safe and not LOG.ask(msg): if opts.safe and not LOG.ask(msg):
break break
removepath(parent, LOG) if not removepath(parent, logger=LOG):
LOG.warn(f'unable to remove {parent}')
parent = os.path.dirname(parent) parent = os.path.dirname(parent)
removed.append(dotfile) removed.append(dotfile)
@@ -833,7 +838,8 @@ def apply_install_trans(dotpath, dotfile, templater, debug=False):
msg += f'failed for {dotfile.key}' msg += f'failed for {dotfile.key}'
LOG.err(msg) LOG.err(msg)
if new_src and os.path.exists(new_src): if new_src and os.path.exists(new_src):
removepath(new_src, LOG) # ignore error
removepath(new_src, logger=LOG)
return None return None
return new_src return new_src
@@ -873,7 +879,8 @@ def _exec_command(opts):
tmp = get_tmpdir() tmp = get_tmpdir()
ret = cmd_compare(opts, tmp) ret = cmd_compare(opts, tmp)
# clean tmp directory # clean tmp directory
removepath(tmp, LOG) # ignore any error
removepath(tmp, logger=LOG)
elif opts.cmd_import: elif opts.cmd_import:
# import dotfile(s) # import dotfile(s)

View File

@@ -328,6 +328,7 @@ class Importer:
msg = f'transformation \"{trans.key}\" failed for {path}' msg = f'transformation \"{trans.key}\" failed for {path}'
self.log.err(msg) self.log.err(msg)
if os.path.exists(tmp): if os.path.exists(tmp):
# ignore error
removepath(tmp, logger=self.log) removepath(tmp, logger=self.log)
return None return None
return tmp return tmp

View File

@@ -700,8 +700,9 @@ class Installer:
if self.safe: if self.safe:
if not self.log.ask(f'remove unmanaged \"{path}\"'): if not self.log.ask(f'remove unmanaged \"{path}\"'):
return return
self.log.dbg(f'removing not managed: {path}') self.log.dbg(f'removing unmanaged file \"{path}\"')
removepath(path, logger=self.log) if not removepath(path, logger=self.log):
self.log.warn('unable to remove {path}')
@classmethod @classmethod
def _write_content_to_file(cls, content, src, dst): def _write_content_to_file(cls, content, src, dst):
@@ -825,6 +826,7 @@ class Installer:
if ret: if ret:
self.log.dbg('content differ') self.log.dbg('content differ')
if content: if content:
# ignore error
removepath(tmp) removepath(tmp)
return ret return ret
@@ -841,6 +843,7 @@ class Installer:
diff = diffit(modified=src, original=dst, diff = diffit(modified=src, original=dst,
diff_cmd=self.diff_cmd) diff_cmd=self.diff_cmd)
if tmp: if tmp:
# ignore error
removepath(tmp, logger=self.log) removepath(tmp, logger=self.log)
if diff: if diff:

View File

@@ -99,7 +99,7 @@ class Uninstaller:
def _remove_path(self, path): def _remove_path(self, path):
"""remove a file""" """remove a file"""
try: try:
removepath(path, self.log) removepath(path)
except OSError as exc: except OSError as exc:
err = f'removing \"{path}\" failed: {exc}' err = f'removing \"{path}\" failed: {exc}'
return False, err return False, err

View File

@@ -151,6 +151,7 @@ class Updater:
# clean temporary files # clean temporary files
if new_path != deployed_path and os.path.exists(new_path): if new_path != deployed_path and os.path.exists(new_path):
# ignore error
removepath(new_path, logger=self.log) removepath(new_path, logger=self.log)
return ret return ret
@@ -167,6 +168,7 @@ class Updater:
if not trans.transform(path, tmp, templater=self.templater, if not trans.transform(path, tmp, templater=self.templater,
debug=self.debug): debug=self.debug):
if os.path.exists(tmp): if os.path.exists(tmp):
# ignore error
removepath(tmp, logger=self.log) removepath(tmp, logger=self.log)
err = f'transformation \"{trans.key}\" failed for {dotfile.key}' err = f'transformation \"{trans.key}\" failed for {dotfile.key}'
self.log.err(err) self.log.err(err)
@@ -259,6 +261,7 @@ class Updater:
def _handle_dir(self, deployed_path, local_path, def _handle_dir(self, deployed_path, local_path,
dotfile, ignores): dotfile, ignores):
"""sync path (local dir) and local_path (dotdrop dir path)""" """sync path (local dir) and local_path (dotdrop dir path)"""
ret = True
self.log.dbg(f'handle update for dir {deployed_path} to {local_path}') self.log.dbg(f'handle update for dir {deployed_path} to {local_path}')
# get absolute paths # get absolute paths
@@ -282,7 +285,11 @@ class Updater:
self.log.dbg(f'rm -r {path}') self.log.dbg(f'rm -r {path}')
if not self._confirm_rm_r(path): if not self._confirm_rm_r(path):
continue continue
removepath(path, logger=self.log) if not removepath(path, logger=self.log):
msg = f'unable to remove {path}, do manually'
self.log.warn(msg)
ret = False
continue
self.log.sub(f'\"{path}\" removed') self.log.sub(f'\"{path}\" removed')
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \ ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
@@ -306,6 +313,7 @@ class Updater:
msg = f'{srcpath} update right only failed' msg = f'{srcpath} update right only failed'
msg += f', do manually: {exc}' msg += f', do manually: {exc}'
self.log.warn(msg) self.log.warn(msg)
ret = False
continue continue
self.log.sub(f'\"{dstpath}\" updated') self.log.sub(f'\"{dstpath}\" updated')
@@ -332,10 +340,10 @@ class Updater:
except IOError as exc: except IOError as exc:
msg = f'{srcpath} update common failed, do manually: {exc}' msg = f'{srcpath} update common failed, do manually: {exc}'
self.log.warn(msg) self.log.warn(msg)
ret = False
continue continue
self.log.sub(f'\"{dstpath}\" content updated') self.log.sub(f'\"{dstpath}\" content updated')
return ret
return True
def _overwrite(self, src, dst): def _overwrite(self, src, dst):
"""ask for overwritting""" """ask for overwritting"""

View File

@@ -153,26 +153,24 @@ def get_unique_tmp_name():
return os.path.join(tmpdir, unique) return os.path.join(tmpdir, unique)
def removepath(path, logger=None): def removepath(path, logger=None): # TODO
""" """
remove a file/directory/symlink remove a file/directory/symlink
if logger is defined, OSError are catched raises OSError in case of error
and printed to logger.warn instead of being forwarded unless logger is defined.
as OSError
When logger is defined, a logger.warn
is printed and a boolean returned
""" """
if not path: if not path:
return return True
if not os.path.lexists(path): if not os.path.lexists(path):
err = f'File not found: {path}' return True
if logger:
logger.warn(err)
return
raise OSError(err)
if os.path.normpath(os.path.expanduser(path)) in NOREMOVE: if os.path.normpath(os.path.expanduser(path)) in NOREMOVE:
err = f'Dotdrop refuses to remove {path}' err = f'Dotdrop refuses to remove {path}'
if logger: if logger:
logger.warn(err) logger.warn(err)
return return False
LOG.err(err) LOG.err(err)
raise OSError(err) raise OSError(err)
if logger: if logger:
@@ -189,8 +187,9 @@ def removepath(path, logger=None):
err = str(exc) err = str(exc)
if logger: if logger:
logger.warn(err) logger.warn(err)
return return False
raise OSError(err) from exc raise OSError(err) from exc
return True
def samefile(path1, path2): def samefile(path1, path2):