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

f-string refactoring

This commit is contained in:
deadc0de6
2022-08-28 22:10:20 +02:00
parent 66880c0d31
commit df65f3a126
11 changed files with 206 additions and 212 deletions

View File

@@ -61,7 +61,7 @@ class Updater:
"""update the dotfile installed on path"""
path = os.path.expanduser(path)
if not os.path.lexists(path):
self.log.err('\"{}\" does not exist!'.format(path))
self.log.err(f'\"{path}\" does not exist!')
return False
dotfiles = self.conf.get_dotfile_by_dst(path,
profile_key=self.profile_key)
@@ -69,12 +69,12 @@ class Updater:
return False
for dotfile in dotfiles:
if not dotfile:
msg = 'invalid dotfile for update: {}'
self.log.err(msg.format(dotfile.key))
err = f'invalid dotfile for update: {dotfile.key}'
self.log.err(err)
return False
msg = 'updating {} from path \"{}\"'
self.log.dbg(msg.format(dotfile, path))
msg = f'updating {dotfile} from path \"{path}\"'
self.log.dbg(msg)
if not self._update(path, dotfile):
return False
return True
@@ -83,11 +83,11 @@ class Updater:
"""update the dotfile referenced by key"""
dotfile = self.conf.get_dotfile(key, profile_key=self.profile_key)
if not dotfile:
self.log.dbg('no such dotfile: \"{}\"'.format(key))
msg = 'invalid dotfile for update: {}'
self.log.err(msg.format(key))
self.log.dbg(f'no such dotfile: \"{key}\"')
msg = f'invalid dotfile for update: {key}'
self.log.err(msg)
return False
self.log.dbg('updating {} from key \"{}\"'.format(dotfile, key))
self.log.dbg(f'updating {dotfile} from key \"{key}\"')
path = self.conf.path_to_dotfile_dst(dotfile.dst)
return self._update(path, dotfile)
@@ -97,27 +97,27 @@ class Updater:
new_path = None
ignores = list(set(self.ignore + dotfile.upignore))
self.ignores = patch_ignores(ignores, dotfile.dst, debug=self.debug)
self.log.dbg('ignore pattern(s): {}'.format(self.ignores))
self.log.dbg(f'ignore pattern(s): {self.ignores}')
deployed_path = os.path.expanduser(path)
local_path = os.path.join(self.dotpath, dotfile.src)
local_path = os.path.expanduser(local_path)
if not os.path.exists(deployed_path):
msg = '\"{}\" does not exist'
self.log.err(msg.format(deployed_path))
msg = f'\"{deployed_path}\" does not exist'
self.log.err(msg)
return False
if not os.path.exists(local_path):
msg = '\"{}\" does not exist, import it first'
self.log.err(msg.format(local_path))
msg = f'\"{local_path}\" does not exist, import it first'
self.log.err(msg)
return False
ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
dotfile.ignore_missing_in_dotdrop
if (ignore_missing_in_dotdrop and not os.path.exists(local_path)) or \
self._ignore([deployed_path, local_path]):
self.log.sub('\"{}\" ignored'.format(dotfile.key))
self.log.sub(f'\"{dotfile.key}\" ignored')
return True
# apply write transformation if any
new_path = self._apply_trans_w(deployed_path, dotfile)
@@ -136,8 +136,8 @@ class Updater:
if deployed_mode != local_mode:
# mirror rights
msg = 'adopt mode {:o} for {}'
self.log.dbg(msg.format(deployed_mode, dotfile.key))
msg = f'adopt mode {deployed_mode:o} for {dotfile.key}'
self.log.dbg(msg)
if self.conf.update_dotfile(dotfile.key, deployed_mode):
ret = True
@@ -151,26 +151,26 @@ class Updater:
trans = dotfile.get_trans_w()
if not trans:
return path
self.log.dbg('executing write transformation {}'.format(trans))
self.log.dbg(f'executing write transformation {trans}')
tmp = get_unique_tmp_name()
self.templater.restore_vars(self.tvars)
newvars = dotfile.get_dotfile_variables()
self.templater.add_tmp_vars(newvars=newvars)
if not trans.transform(path, tmp, templater=self.templater,
debug=self.debug):
msg = 'transformation \"{}\" failed for {}'
self.log.err(msg.format(trans.key, dotfile.key))
if os.path.exists(tmp):
removepath(tmp, logger=self.log)
err = f'transformation \"{trans.key}\" failed for {dotfile.key}'
self.log.err(err)
return None
return tmp
def _is_template(self, path):
if not Templategen.is_template(path, ignore=self.ignores,
debug=self.debug):
self.log.dbg('{} is NO template'.format(path))
self.log.dbg(f'{path} is NO template')
return False
self.log.warn('{} uses template, update manually'.format(path))
self.log.warn(f'{path} uses template, update manually')
return True
def _show_patch(self, fpath, tpath):
@@ -179,7 +179,8 @@ class Updater:
tmp = write_to_tmpfile(content)
mirror_file_rights(tpath, tmp)
cmds = ['diff', '-u', tmp, fpath, '|', 'patch', tpath]
self.log.warn('try patching with: \"{}\"'.format(' '.join(cmds)))
cmdss = ' '.join(cmds)
self.log.warn(f'try patching with: \"{cmdss}\"')
return False
def _resolve_template(self, tpath):
@@ -202,8 +203,8 @@ class Updater:
dstr = get_file_perm(dst)
if srcr == dstr:
return
msg = 'copy rights from {} ({:o}) to {} ({:o})'
self.log.dbg(msg.format(src, srcr, dst, dstr))
msg = f'copy rights from {src} ({srcr:o}) to {dst} ({dstr:o})'
self.log.dbg(msg)
try:
mirror_file_rights(src, dst)
except OSError as exc:
@@ -212,67 +213,49 @@ class Updater:
def _handle_file(self, deployed_path, local_path, compare=True):
"""sync path (deployed file) and local_path (dotdrop dotfile path)"""
if self._ignore([deployed_path, local_path]):
self.log.sub('\"{}\" ignored'.format(local_path))
self.log.sub(f'\"{local_path}\" ignored')
return True
self.log.dbg('update for file {} and {}'.format(
deployed_path,
local_path,
))
self.log.dbg(f'update for file {deployed_path} and {local_path}')
if self._is_template(local_path):
# dotfile is a template
self.log.dbg('{} is a template'.format(local_path))
self.log.dbg(f'{local_path} is a template')
if self.showpatch:
try:
self._show_patch(deployed_path, local_path)
except UndefinedException as exc:
msg = 'unable to show patch for {}: {}'.format(
deployed_path,
exc,
)
msg = f'unable to show patch for {deployed_path}: {exc}'
self.log.warn(msg)
return False
if compare and \
filecmp.cmp(deployed_path, local_path, shallow=False) and \
self._same_rights(deployed_path, local_path):
# no difference
self.log.dbg('identical files: {} and {}'.format(
deployed_path,
local_path,
))
self.log.dbg(f'identical files: {deployed_path} and {local_path}')
return True
if not self._overwrite(deployed_path, local_path):
return False
try:
if self.dry:
self.log.dry('would cp {} {}'.format(
deployed_path,
local_path,
))
self.log.dry(f'would cp {deployed_path} {local_path}')
else:
self.log.dbg('cp {} {}'.format(deployed_path, local_path))
self.log.dbg(f'cp {deployed_path} {local_path}')
shutil.copyfile(deployed_path, local_path)
self._mirror_rights(deployed_path, local_path)
self.log.sub('\"{}\" updated'.format(local_path))
self.log.sub(f'\"{local_path}\" updated')
except IOError as exc:
self.log.warn('{} update failed, do manually: {}'.format(
deployed_path,
exc
))
self.log.warn(f'{deployed_path} update failed, do manually: {exc}')
return False
return True
def _handle_dir(self, deployed_path, local_path, dotfile):
"""sync path (local dir) and local_path (dotdrop dir path)"""
self.log.dbg('handle update for dir {} to {}'.format(
deployed_path,
local_path,
))
self.log.dbg(f'handle update for dir {deployed_path} to {local_path}')
# paths must be absolute (no tildes)
deployed_path = os.path.expanduser(deployed_path)
local_path = os.path.expanduser(local_path)
if self._ignore([deployed_path, local_path]):
self.log.sub('\"{}\" ignored'.format(local_path))
self.log.sub(f'\"{local_path}\" ignored')
return True
# find the differences
diff = filecmp.dircmp(deployed_path, local_path, ignore=None)
@@ -293,12 +276,12 @@ class Updater:
new = os.path.join(right, toadd)
if (ignore_missing_in_dotdrop and not os.path.exists(new)) or \
self._ignore([exist, new]):
self.log.sub('\"{}\" ignored'.format(exist))
self.log.sub(f'\"{exist}\" ignored')
continue
if self.dry:
self.log.dry('would cp -r {} {}'.format(exist, new))
self.log.dry(f'would cp -r {exist} {new}')
continue
self.log.dbg('cp -r {} {}'.format(exist, new))
self.log.dbg(f'cp -r {exist} {new}')
# Newly created directory should be copied as is (for efficiency).
def ign(src, names):
@@ -318,10 +301,10 @@ class Updater:
try:
shutil.copytree(exist, new, ignore=ign)
except OSError as exc:
msg = 'error copying dir {}'.format(exist)
self.log.err('{}: {}'.format(msg, exc))
msg = f'error copying dir {exist}'
self.log.err(f'{msg}: {exc}')
continue
self.log.sub('\"{}\" dir added'.format(new))
self.log.sub(f'\"{new}\" dir added')
def _merge_dirs_remove_right_only(self, diff, left, right,
ignore_missing_in_dotdrop):
@@ -334,13 +317,13 @@ class Updater:
if self._ignore([old]):
continue
if self.dry:
self.log.dry('would rm -r {}'.format(old))
self.log.dry(f'would rm -r {old}')
continue
self.log.dbg('rm -r {}'.format(old))
self.log.dbg(f'rm -r {old}')
if not self._confirm_rm_r(old):
continue
removepath(old, logger=self.log)
self.log.sub('\"{}\" dir removed'.format(old))
self.log.sub(f'\"{old}\" dir removed')
# handle files diff
# sync files that exist in both but are different
@@ -354,9 +337,9 @@ class Updater:
self._ignore([fleft, fright]):
continue
if self.dry:
self.log.dry('would cp {} {}'.format(fleft, fright))
self.log.dry(f'would cp {fleft} {fright}')
continue
self.log.dbg('cp {} {}'.format(fleft, fright))
self.log.dbg(f'cp {fleft} {fright}')
self._handle_file(fleft, fright, compare=False)
def _merge_dirs_copy_left_only(self, diff, left, right,
@@ -372,18 +355,18 @@ class Updater:
self._ignore([exist, new]):
continue
if self.dry:
self.log.dry('would cp {} {}'.format(exist, new))
self.log.dry(f'would cp {exist} {new}')
continue
self.log.dbg('cp {} {}'.format(exist, new))
self.log.dbg(f'cp {exist} {new}')
try:
shutil.copyfile(exist, new)
except OSError as exc:
msg = 'error copying file {}'.format(exist)
self.log.err('{}: {}'.format(msg, exc))
msg = f'error copying file {exist}'
self.log.err(f'{msg}: {exc}')
continue
self._mirror_rights(exist, new)
self.log.sub('\"{}\" added'.format(new))
self.log.sub(f'\"{new}\" added')
def _merge_dirs_remove_right_only_2(self, diff, right):
"""remove files that don't exist in deployed version"""
@@ -397,16 +380,16 @@ class Updater:
if self._ignore([new]):
continue
if self.dry:
self.log.dry('would rm {}'.format(new))
self.log.dry(f'would rm {new}')
continue
self.log.dbg('rm {}'.format(new))
self.log.dbg(f'rm {new}')
removepath(new, logger=self.log)
self.log.sub('\"{}\" removed'.format(new))
self.log.sub(f'\"{new}\" removed')
def _merge_dirs(self, diff, dotfile):
"""Synchronize directories recursively."""
left, right = diff.left, diff.right
self.log.dbg('sync dir {} to {}'.format(left, right))
self.log.dbg(f'sync dir {left} to {right}')
if self._ignore([left, right]):
return True
@@ -437,20 +420,20 @@ class Updater:
def _overwrite(self, src, dst):
"""ask for overwritting"""
msg = 'Overwrite \"{}\" with \"{}\"?'.format(dst, src)
msg = f'Overwrite \"{dst}\" with \"{src}\"?'
if self.safe and not self.log.ask(msg):
return False
return True
def _confirm_rm_r(self, directory):
"""ask for rm -r directory"""
msg = 'Recursively remove \"{}\"?'.format(directory)
msg = f'Recursively remove \"{directory}\"?'
if self.safe and not self.log.ask(msg):
return False
return True
def _ignore(self, paths):
if must_ignore(paths, self.ignores, debug=self.debug):
self.log.dbg('ignoring update for {}'.format(paths))
self.log.dbg(f'ignoring update for {paths}')
return True
return False