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

fix chmod on user aborted

This commit is contained in:
deadc0de6
2020-11-17 21:24:26 +01:00
parent 15e2a9c26d
commit 541c6310fb

View File

@@ -147,7 +147,13 @@ class Installer:
template=template) template=template)
# handle chmod # handle chmod
if r and not err and chmod and not self.dry: # - on success (r, not err)
# - no change (not r, not err)
# but not when
# - error (not r, err)
# - aborted (not r, err)
if (r or (not r and not err)) \
and chmod and not self.dry:
dstperms = utils.get_file_perm(dst) dstperms = utils.get_file_perm(dst)
if dstperms != chmod: if dstperms != chmod:
# apply mode # apply mode
@@ -218,7 +224,15 @@ class Installer:
######################################################## ########################################################
def _link(self, templater, src, dst, actionexec=None, template=True): def _link(self, templater, src, dst, actionexec=None, template=True):
"""install link:link""" """
install link:link
return
- True, None : success
- False, error_msg : error
- False, None : ignored
- False, 'aborted' : user aborted
"""
if template and Templategen.is_template(src): if template and Templategen.is_template(src):
if self.debug: if self.debug:
self.log.dbg('is a template') self.log.dbg('is a template')
@@ -236,7 +250,15 @@ class Installer:
def _link_children(self, templater, src, dst, def _link_children(self, templater, src, dst,
actionexec=None, template=True): actionexec=None, template=True):
"""install link:link_children""" """
install link:link_children
return
- True, None : success
- False, error_msg : error
- False, None : ignored
- False, 'aborted' : user aborted
"""
parent = os.path.join(self.base, src) parent = os.path.join(self.base, src)
if not os.path.lexists(dst): if not os.path.lexists(dst):
self.log.sub('creating directory "{}"'.format(dst)) self.log.sub('creating directory "{}"'.format(dst))
@@ -249,8 +271,7 @@ class Installer:
]).format(dst) ]).format(dst)
if self.safe and not self.log.ask(msg): if self.safe and not self.log.ask(msg):
err = 'ignoring "{}", not installed'.format(dst) return False, 'aborted'
return False, err
os.unlink(dst) os.unlink(dst)
self._create_dirs(dst) self._create_dirs(dst)
@@ -305,7 +326,8 @@ class Installer:
return return
- True, None : success - True, None : success
- False, error_msg : error - False, error_msg : error
- False, None, ignored - False, None : ignored
- False, 'aborted' : user aborted
""" """
overwrite = not self.safe overwrite = not self.safe
if os.path.lexists(dst): if os.path.lexists(dst):
@@ -321,8 +343,7 @@ class Installer:
self._show_diff_before_write(src, dst) self._show_diff_before_write(src, dst)
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):
err = 'ignoring "{}", link was not created'.format(dst) return False, 'aborted'
return False, err
overwrite = True overwrite = True
try: try:
utils.removepath(dst) utils.removepath(dst)
@@ -343,8 +364,7 @@ class Installer:
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):
err = 'ignoring "{}", link was not created'.format(dst) return False, 'aborted'
return False, err
try: try:
utils.removepath(dst) utils.removepath(dst)
except OSError as e: except OSError as e:
@@ -358,7 +378,15 @@ class Installer:
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], template=True, ignore=[], template=True,
chmod=None): chmod=None):
"""install src to dst when is a file""" """
install src to dst when is a file
return
- True, None : success
- False, error_msg : error
- False, None : ignored
- False, 'aborted' : user aborted
"""
if self.debug: if self.debug:
self.log.dbg('deploy file: {}'.format(src)) self.log.dbg('deploy file: {}'.format(src))
self.log.dbg('ignore empty: {}'.format(noempty)) self.log.dbg('ignore empty: {}'.format(noempty))
@@ -411,29 +439,23 @@ class Installer:
actionexec=actionexec, actionexec=actionexec,
template=template, template=template,
chmod=chmod) chmod=chmod)
if ret and not err:
# build return values
if ret < 0:
# error
return False, err
if ret > 0:
# already exists
if self.debug:
self.log.dbg('ignoring {}'.format(dst))
return False, None
if ret == 0:
# success
if not self.dry and not self.comparing: if not self.dry and not self.comparing:
self.log.sub('install {} to {}'.format(src, dst)) self.log.sub('install {} to {}'.format(src, dst))
return True, None return ret, err
# error
err = 'installing {} to {}'.format(src, dst)
return False, err
def _copy_dir(self, templater, src, dst, def _copy_dir(self, templater, src, dst,
actionexec=None, noempty=False, actionexec=None, noempty=False,
ignore=[], template=True, chmod=None): ignore=[], template=True, chmod=None):
"""install src to dst when is a directory""" """
install src to dst when is a directory
return
- True, None : success
- False, error_msg : error
- False, None : ignored
- False, 'aborted' : user aborted
"""
if self.debug: if self.debug:
self.log.dbg('deploy dir {}'.format(src)) self.log.dbg('deploy dir {}'.format(src))
# default to nothing installed and no error # default to nothing installed and no error
@@ -488,16 +510,19 @@ class Installer:
chmod=None): chmod=None):
""" """
copy dotfile / write content to file copy dotfile / write content to file
return 0, None: for success,
1, None: when already exists
-1, err: when error
content is always empty if template is False content is always empty if template is False
and is to be ignored and is to be ignored
return
- True, None : success
- False, error_msg : error
- False, None : ignored
- False, 'aborted' : user aborted
""" """
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, None return True, None
if os.path.lexists(dst): if os.path.lexists(dst):
try: try:
@@ -506,7 +531,7 @@ class Installer:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
# broken symlink # broken symlink
err = 'broken symlink {}'.format(dst) err = 'broken symlink {}'.format(dst)
return -1, err return False, err
src_mode = chmod src_mode = chmod
if not src_mode: if not src_mode:
@@ -517,7 +542,7 @@ class Installer:
src_mode=src_mode): src_mode=src_mode):
if self.debug: if self.debug:
self.log.dbg('{} is the same'.format(dst)) self.log.dbg('{} is the same'.format(dst))
return 1, None return False, 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))
@@ -526,25 +551,24 @@ class Installer:
self._show_diff_before_write(src, dst, self._show_diff_before_write(src, dst,
content=content) content=content)
if not self.log.ask('Overwrite \"{}\"'.format(dst)): if not self.log.ask('Overwrite \"{}\"'.format(dst)):
self.log.warn('ignoring {}'.format(dst)) return False, 'aborted'
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):
err = 'creating directory for {}'.format(dst) err = 'creating directory for {}'.format(dst)
return -1, err return False, err
r, e = self._exec_pre_actions(actionexec) r, e = self._exec_pre_actions(actionexec)
if not r: if not r:
return -1, e return False, e
if self.debug: if self.debug:
self.log.dbg('install file to \"{}\"'.format(dst)) self.log.dbg('install file 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, None return False, 'aborted'
if template: if template:
# write content the file # write content the file
@@ -554,17 +578,17 @@ class Installer:
shutil.copymode(src, dst) shutil.copymode(src, dst)
except NotADirectoryError as e: except NotADirectoryError as e:
err = 'opening dest file: {}'.format(e) err = 'opening dest file: {}'.format(e)
return -1, err return False, err
except Exception as e: except Exception as e:
return -1, str(e) return False, str(e)
else: else:
# copy file # copy file
try: try:
shutil.copyfile(src, dst) shutil.copyfile(src, dst)
shutil.copymode(src, dst) shutil.copymode(src, dst)
except Exception as e: except Exception as e:
return -1, str(e) return False, str(e)
return 0, None return True, None
######################################################## ########################################################
# helpers # helpers