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

refactor debug logging

This commit is contained in:
deadc0de6
2021-04-29 14:51:44 +02:00
parent d03bfaed6d
commit 8021a0e137
11 changed files with 198 additions and 305 deletions

View File

@@ -41,7 +41,7 @@ class CfgAggregator:
self.profile_key = profile_key self.profile_key = profile_key
self.debug = debug self.debug = debug
self.dry = dry self.dry = dry
self.log = Logger() self.log = Logger(debug=self.debug)
self._load() self._load()
######################################################## ########################################################
@@ -74,7 +74,7 @@ class CfgAggregator:
key = dotfile.key key = dotfile.key
ret = self.cfgyaml.add_dotfile_to_profile(key, self.profile_key) ret = self.cfgyaml.add_dotfile_to_profile(key, self.profile_key)
if ret and self.debug: if ret:
msg = 'new dotfile {} to profile {}' msg = 'new dotfile {} to profile {}'
self.log.dbg(msg.format(key, self.profile_key)) self.log.dbg(msg.format(key, self.profile_key))
@@ -196,8 +196,7 @@ class CfgAggregator:
"""create a new dotfile""" """create a new dotfile"""
# get a new dotfile with a unique key # get a new dotfile with a unique key
key = self._get_new_dotfile_key(dst) key = self._get_new_dotfile_key(dst)
if self.debug: self.log.dbg('new dotfile key: {}'.format(key))
self.log.dbg('new dotfile key: {}'.format(key))
# add the dotfile # add the dotfile
if not self.cfgyaml.add_dotfile(key, src, dst, link, chmod=chmod): if not self.cfgyaml.add_dotfile(key, src, dst, link, chmod=chmod):
return None return None
@@ -260,9 +259,9 @@ class CfgAggregator:
# patch actions in settings default_actions # patch actions in settings default_actions
self._patch_keys_to_objs([self.settings], self._patch_keys_to_objs([self.settings],
"default_actions", self._get_action_w_args) "default_actions", self._get_action_w_args)
if self.debug:
msg = 'default actions: {}'.format(self.settings.default_actions) msg = 'default actions: {}'.format(self.settings.default_actions)
self.log.dbg(msg) self.log.dbg(msg)
# patch trans_w/trans_r in dotfiles # patch trans_w/trans_r in dotfiles
self._patch_keys_to_objs(self.dotfiles, self._patch_keys_to_objs(self.dotfiles,
@@ -281,8 +280,7 @@ class CfgAggregator:
""" """
if not containers: if not containers:
return return
if self.debug: self.log.dbg('patching {} ...'.format(keys))
self.log.dbg('patching {} ...'.format(keys))
for container in containers: for container in containers:
objects = [] objects = []
okeys = getattr(container, keys) okeys = getattr(container, keys)
@@ -300,9 +298,6 @@ class CfgAggregator:
objects.append(obj) objects.append(obj)
if not islist: if not islist:
objects = objects[0] objects = objects[0]
# if self.debug:
# er = 'patching {}.{} with {}'
# self.log.dbg(er.format(c, keys, objects))
setattr(container, keys, objects) setattr(container, keys, objects)
######################################################## ########################################################
@@ -368,8 +363,7 @@ class CfgAggregator:
if self.dry: if self.dry:
return return
self.save() self.save()
if self.debug: self.log.dbg('reloading config')
self.log.dbg('reloading config')
olddebug = self.debug olddebug = self.debug
self.debug = False self.debug = False
self._load() self._load()
@@ -411,9 +405,8 @@ class CfgAggregator:
if len(fields) > 1: if len(fields) > 1:
# we have args # we have args
key, *args = fields key, *args = fields
if self.debug: msg = 'action with parm: {} and {}'
msg = 'action with parm: {} and {}' self.log.dbg(msg.format(key, args))
self.log.dbg(msg.format(key, args))
action = self._get_action(key).copy(args) action = self._get_action(key).copy(args)
else: else:
action = self._get_action(key) action = self._get_action(key)
@@ -426,9 +419,8 @@ class CfgAggregator:
if len(fields) > 1: if len(fields) > 1:
# we have args # we have args
key, *args = fields key, *args = fields
if self.debug: msg = 'trans with parm: {} and {}'
msg = 'trans with parm: {} and {}' self.log.dbg(msg.format(key, args))
self.log.dbg(msg.format(key, args))
trans = getter(key).copy(args) trans = getter(key).copy(args)
else: else:
trans = getter(key) trans = getter(key)

View File

@@ -106,7 +106,7 @@ class CfgYaml:
self._path = os.path.abspath(path) self._path = os.path.abspath(path)
self._profile = profile self._profile = profile
self._debug = debug self._debug = debug
self._log = Logger() self._log = Logger(debug=self._debug)
# config needs to be written # config needs to be written
self._dirty = False self._dirty = False
# indicates the config has been updated # indicates the config has been updated

View File

@@ -24,7 +24,7 @@ class Comparator:
""" """
self.diff_cmd = diff_cmd self.diff_cmd = diff_cmd
self.debug = debug self.debug = debug
self.log = Logger() self.log = Logger(debug=self.debug)
self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop self.ignore_missing_in_dotdrop = ignore_missing_in_dotdrop
def compare(self, local_path, deployed_path, ignore=[]): def compare(self, local_path, deployed_path, ignore=[]):
@@ -32,12 +32,11 @@ class Comparator:
deployed_path (destination file)""" deployed_path (destination file)"""
local_path = os.path.expanduser(local_path) local_path = os.path.expanduser(local_path)
deployed_path = os.path.expanduser(deployed_path) deployed_path = os.path.expanduser(deployed_path)
if self.debug: self.log.dbg('comparing {} and {}'.format(
self.log.dbg('comparing {} and {}'.format( local_path,
local_path, deployed_path,
deployed_path, ))
)) self.log.dbg('ignore pattern(s): {}'.format(ignore))
self.log.dbg('ignore pattern(s): {}'.format(ignore))
# test type of file # test type of file
if os.path.isdir(local_path) and not os.path.isdir(deployed_path): if os.path.isdir(local_path) and not os.path.isdir(deployed_path):
@@ -53,17 +52,13 @@ class Comparator:
# test content # test content
if not os.path.isdir(local_path): if not os.path.isdir(local_path):
if self.debug: self.log.dbg('{} is a file'.format(local_path))
self.log.dbg('{} is a file'.format(local_path))
if self.debug:
self.log.dbg('is file')
ret = self._comp_file(local_path, deployed_path, ignore) ret = self._comp_file(local_path, deployed_path, ignore)
if not ret: if not ret:
ret = self._comp_mode(local_path, deployed_path) ret = self._comp_mode(local_path, deployed_path)
return ret return ret
if self.debug: self.log.dbg('{} is a directory'.format(local_path))
self.log.dbg('{} is a directory'.format(local_path))
ret = self._comp_dir(local_path, deployed_path, ignore) ret = self._comp_dir(local_path, deployed_path, ignore)
if not ret: if not ret:
@@ -76,55 +71,49 @@ class Comparator:
deployed_mode = get_file_perm(deployed_path) deployed_mode = get_file_perm(deployed_path)
if local_mode == deployed_mode: if local_mode == deployed_mode:
return '' return ''
if self.debug: msg = 'mode differ {} ({:o}) and {} ({:o})'
msg = 'mode differ {} ({:o}) and {} ({:o})' self.log.dbg(msg.format(local_path, local_mode, deployed_path,
self.log.dbg(msg.format(local_path, local_mode, deployed_path, deployed_mode))
deployed_mode))
ret = 'modes differ for {} ({:o}) vs {:o}\n' ret = 'modes differ for {} ({:o}) vs {:o}\n'
return ret.format(deployed_path, deployed_mode, local_mode) return ret.format(deployed_path, deployed_mode, local_mode)
def _comp_file(self, local_path, deployed_path, ignore): def _comp_file(self, local_path, deployed_path, ignore):
"""compare a file""" """compare a file"""
if self.debug: self.log.dbg('compare file {} with {}'.format(
self.log.dbg('compare file {} with {}'.format( local_path,
local_path, deployed_path,
deployed_path, ))
))
if (self.ignore_missing_in_dotdrop and not if (self.ignore_missing_in_dotdrop and not
os.path.exists(local_path)) \ os.path.exists(local_path)) \
or must_ignore([local_path, deployed_path], ignore, or must_ignore([local_path, deployed_path], ignore,
debug=self.debug): debug=self.debug):
if self.debug: self.log.dbg('ignoring diff {} and {}'.format(
self.log.dbg('ignoring diff {} and {}'.format( local_path,
local_path, deployed_path,
deployed_path, ))
))
return '' return ''
return self._diff(local_path, deployed_path) return self._diff(local_path, deployed_path)
def _comp_dir(self, local_path, deployed_path, ignore): def _comp_dir(self, local_path, deployed_path, ignore):
"""compare a directory""" """compare a directory"""
if self.debug: self.log.dbg('compare directory {} with {}'.format(
self.log.dbg('compare directory {} with {}'.format( local_path,
local_path, deployed_path,
deployed_path, ))
))
if not os.path.exists(deployed_path): if not os.path.exists(deployed_path):
return '' return ''
if (self.ignore_missing_in_dotdrop and not if (self.ignore_missing_in_dotdrop and not
os.path.exists(local_path)) \ os.path.exists(local_path)) \
or must_ignore([local_path, deployed_path], ignore, or must_ignore([local_path, deployed_path], ignore,
debug=self.debug): debug=self.debug):
if self.debug: self.log.dbg('ignoring diff {} and {}'.format(
self.log.dbg('ignoring diff {} and {}'.format( local_path,
local_path, deployed_path,
deployed_path, ))
))
return '' return ''
if not os.path.isdir(deployed_path): if not os.path.isdir(deployed_path):
return '\"{}\" is a file\n'.format(deployed_path) return '\"{}\" is a file\n'.format(deployed_path)
if self.debug: self.log.dbg('compare {} and {}'.format(local_path, deployed_path))
self.log.dbg('compare {} and {}'.format(local_path, deployed_path))
ret = [] ret = []
comp = filecmp.dircmp(local_path, deployed_path) comp = filecmp.dircmp(local_path, deployed_path)

View File

@@ -48,8 +48,7 @@ def action_executor(o, actions, defactions, templater, post=False):
LOG.dry('would execute def-{}-action: {}'.format(s, LOG.dry('would execute def-{}-action: {}'.format(s,
action)) action))
continue continue
if o.debug: LOG.dbg('executing def-{}-action: {}'.format(s, action))
LOG.dbg('executing def-{}-action: {}'.format(s, action))
ret = action.execute(templater=templater, debug=o.debug) ret = action.execute(templater=templater, debug=o.debug)
if not ret: if not ret:
err = 'def-{}-action \"{}\" failed'.format(s, action.key) err = 'def-{}-action \"{}\" failed'.format(s, action.key)
@@ -61,8 +60,7 @@ def action_executor(o, actions, defactions, templater, post=False):
if o.dry: if o.dry:
LOG.dry('would execute {}-action: {}'.format(s, action)) LOG.dry('would execute {}-action: {}'.format(s, action))
continue continue
if o.debug: LOG.dbg('executing {}-action: {}'.format(s, action))
LOG.dbg('executing {}-action: {}'.format(s, action))
ret = action.execute(templater=templater, debug=o.debug) ret = action.execute(templater=templater, debug=o.debug)
if not ret: if not ret:
err = '{}-action \"{}\" failed'.format(s, action.key) err = '{}-action \"{}\" failed'.format(s, action.key)
@@ -108,8 +106,7 @@ def _dotfile_compare(o, dotfile, tmp):
t.add_tmp_vars(newvars=newvars) t.add_tmp_vars(newvars=newvars)
# dotfiles does not exist / not installed # dotfiles does not exist / not installed
if o.debug: LOG.dbg('comparing {}'.format(dotfile))
LOG.dbg('comparing {}'.format(dotfile))
src = dotfile.src src = dotfile.src
if not os.path.lexists(os.path.expanduser(dotfile.dst)): if not os.path.lexists(os.path.expanduser(dotfile.dst)):
@@ -120,8 +117,7 @@ def _dotfile_compare(o, dotfile, tmp):
# apply transformation # apply transformation
tmpsrc = None tmpsrc = None
if dotfile.trans_r: if dotfile.trans_r:
if o.debug: LOG.dbg('applying transformation before comparing')
LOG.dbg('applying transformation before comparing')
tmpsrc = apply_trans(o.dotpath, dotfile, t, debug=o.debug) tmpsrc = apply_trans(o.dotpath, dotfile, t, debug=o.debug)
if not tmpsrc: if not tmpsrc:
# could not apply trans # could not apply trans
@@ -132,10 +128,9 @@ def _dotfile_compare(o, dotfile, tmp):
asrc = os.path.join(o.dotpath, os.path.expanduser(src)) asrc = os.path.join(o.dotpath, os.path.expanduser(src))
adst = os.path.expanduser(dotfile.dst) adst = os.path.expanduser(dotfile.dst)
if os.path.samefile(asrc, adst): if os.path.samefile(asrc, adst):
if o.debug: line = '=> compare {}: diffing with \"{}\"'
line = '=> compare {}: diffing with \"{}\"' LOG.dbg(line.format(dotfile.key, dotfile.dst))
LOG.dbg(line.format(dotfile.key, dotfile.dst)) LOG.dbg('points to itself')
LOG.dbg('points to itself')
return True return True
ignores = list(set(o.compare_ignore + dotfile.cmpignore)) ignores = list(set(o.compare_ignore + dotfile.cmpignore))
@@ -179,10 +174,9 @@ def _dotfile_compare(o, dotfile, tmp):
LOG.emph(diff) LOG.emph(diff)
return False return False
# no difference # no difference
if o.debug: line = '=> compare {}: diffing with \"{}\"'
line = '=> compare {}: diffing with \"{}\"' LOG.dbg(line.format(dotfile.key, dotfile.dst))
LOG.dbg(line.format(dotfile.key, dotfile.dst)) LOG.dbg('same file')
LOG.dbg('same file')
return True return True
@@ -208,9 +202,8 @@ def _dotfile_install(o, dotfile, tmpdir=None):
pre_actions_exec = action_executor(o, preactions, defactions, pre_actions_exec = action_executor(o, preactions, defactions,
t, post=False) t, post=False)
if o.debug: LOG.dbg('installing dotfile: \"{}\"'.format(dotfile.key))
LOG.dbg('installing dotfile: \"{}\"'.format(dotfile.key)) LOG.dbg(dotfile.prt())
LOG.dbg(dotfile.prt())
ignores = list(set(o.install_ignore + dotfile.instignore)) ignores = list(set(o.install_ignore + dotfile.instignore))
ignores = patch_ignores(ignores, dotfile.dst, debug=o.debug) ignores = patch_ignores(ignores, dotfile.dst, debug=o.debug)
@@ -278,12 +271,10 @@ def _dotfile_install(o, dotfile, tmpdir=None):
# dotfile was NOT installed # dotfile was NOT installed
if o.install_force_action: if o.install_force_action:
# pre-actions # pre-actions
if o.debug: LOG.dbg('force pre action execution ...')
LOG.dbg('force pre action execution ...')
pre_actions_exec() pre_actions_exec()
# post-actions # post-actions
if o.debug: LOG.dbg('force post action execution ...')
LOG.dbg('force post action execution ...')
defactions = o.install_default_actions_post defactions = o.install_default_actions_post
postactions = dotfile.get_post_actions() postactions = dotfile.get_post_actions()
post_actions_exec = action_executor(o, postactions, defactions, post_actions_exec = action_executor(o, postactions, defactions,
@@ -320,8 +311,7 @@ def cmd_install(o):
installed = [] installed = []
# execute profile pre-action # execute profile pre-action
if o.debug: LOG.dbg('run {} profile pre actions'.format(len(pro_pre_actions)))
LOG.dbg('run {} profile pre actions'.format(len(pro_pre_actions)))
t = _get_templater(o) t = _get_templater(o)
ret, err = action_executor(o, pro_pre_actions, [], t, post=False)() ret, err = action_executor(o, pro_pre_actions, [], t, post=False)()
if not ret: if not ret:
@@ -330,8 +320,7 @@ def cmd_install(o):
# install each dotfile # install each dotfile
if o.workers > 1: if o.workers > 1:
# in parallel # in parallel
if o.debug: LOG.dbg('run with {} workers'.format(o.workers))
LOG.dbg('run with {} workers'.format(o.workers))
ex = futures.ThreadPoolExecutor(max_workers=o.workers) ex = futures.ThreadPoolExecutor(max_workers=o.workers)
wait_for = [] wait_for = []
@@ -359,15 +348,13 @@ def cmd_install(o):
# execute profile post-action # execute profile post-action
if len(installed) > 0 or o.install_force_action: if len(installed) > 0 or o.install_force_action:
if o.debug: msg = 'run {} profile post actions'
msg = 'run {} profile post actions' LOG.dbg(msg.format(len(pro_post_actions)))
LOG.dbg(msg.format(len(pro_post_actions)))
ret, err = action_executor(o, pro_post_actions, [], t, post=False)() ret, err = action_executor(o, pro_post_actions, [], t, post=False)()
if not ret: if not ret:
return False return False
if o.debug: LOG.dbg('install done: installed \"{}\"'.format(','.join(installed)))
LOG.dbg('install done: installed \"{}\"'.format(','.join(installed)))
if o.install_temporary: if o.install_temporary:
LOG.log('\ninstalled to tmp \"{}\".'.format(tmpdir)) LOG.log('\ninstalled to tmp \"{}\".'.format(tmpdir))
@@ -396,8 +383,7 @@ def cmd_compare(o, tmp):
cnt = 0 cnt = 0
if o.workers > 1: if o.workers > 1:
# in parallel # in parallel
if o.debug: LOG.dbg('run with {} workers'.format(o.workers))
LOG.dbg('run with {} workers'.format(o.workers))
ex = futures.ThreadPoolExecutor(max_workers=o.workers) ex = futures.ThreadPoolExecutor(max_workers=o.workers)
wait_for = [] wait_for = []
for dotfile in selected: for dotfile in selected:
@@ -440,12 +426,10 @@ def cmd_update(o):
if not paths: if not paths:
# update the entire profile # update the entire profile
if iskey: if iskey:
if o.debug: LOG.dbg('update by keys: {}'.format(paths))
LOG.dbg('update by keys: {}'.format(paths))
paths = [d.key for d in o.dotfiles] paths = [d.key for d in o.dotfiles]
else: else:
if o.debug: LOG.dbg('update by paths: {}'.format(paths))
LOG.dbg('update by paths: {}'.format(paths))
paths = [d.dst for d in o.dotfiles] paths = [d.dst for d in o.dotfiles]
msg = 'Update all dotfiles for profile \"{}\"'.format(o.profile) msg = 'Update all dotfiles for profile \"{}\"'.format(o.profile)
if o.safe and not LOG.ask(msg): if o.safe and not LOG.ask(msg):
@@ -456,14 +440,12 @@ def cmd_update(o):
LOG.log('\nno dotfile to update') LOG.log('\nno dotfile to update')
return True return True
if o.debug: LOG.dbg('dotfile to update: {}'.format(paths))
LOG.dbg('dotfile to update: {}'.format(paths))
# update each dotfile # update each dotfile
if o.workers > 1: if o.workers > 1:
# in parallel # in parallel
if o.debug: LOG.dbg('run with {} workers'.format(o.workers))
LOG.dbg('run with {} workers'.format(o.workers))
ex = futures.ThreadPoolExecutor(max_workers=o.workers) ex = futures.ThreadPoolExecutor(max_workers=o.workers)
wait_for = [] wait_for = []
for path in paths: for path in paths:
@@ -581,8 +563,7 @@ def cmd_remove(o):
if not paths: if not paths:
LOG.log('no dotfile to remove') LOG.log('no dotfile to remove')
return False return False
if o.debug: LOG.dbg('dotfile(s) to remove: {}'.format(','.join(paths)))
LOG.dbg('dotfile(s) to remove: {}'.format(','.join(paths)))
removed = [] removed = []
for key in paths: for key in paths:
@@ -608,8 +589,7 @@ def cmd_remove(o):
LOG.warn(msg.format(k)) LOG.warn(msg.format(k))
continue continue
if o.debug: LOG.dbg('removing {}'.format(key))
LOG.dbg('removing {}'.format(key))
# make sure is part of the profile # make sure is part of the profile
if dotfile.key not in [d.key for d in o.dotfiles]: if dotfile.key not in [d.key for d in o.dotfiles]:
@@ -624,8 +604,7 @@ def cmd_remove(o):
msg = 'Remove \"{}\" from all these profiles: {}'.format(k, pkeys) msg = 'Remove \"{}\" from all these profiles: {}'.format(k, pkeys)
if o.safe and not LOG.ask(msg): if o.safe and not LOG.ask(msg):
return False return False
if o.debug: LOG.dbg('remove dotfile: {}'.format(dotfile))
LOG.dbg('remove dotfile: {}'.format(dotfile))
for profile in profiles: for profile in profiles:
if not o.conf.del_dotfile_from_profile(dotfile, profile): if not o.conf.del_dotfile_from_profile(dotfile, profile):
@@ -736,8 +715,7 @@ def apply_trans(dotpath, dotfile, templater, debug=False):
src = dotfile.src src = dotfile.src
new_src = '{}.{}'.format(src, TRANS_SUFFIX) new_src = '{}.{}'.format(src, TRANS_SUFFIX)
trans = dotfile.trans_r trans = dotfile.trans_r
if debug: LOG.dbg('executing transformation: {}'.format(trans))
LOG.dbg('executing transformation: {}'.format(trans))
s = os.path.join(dotpath, src) s = os.path.join(dotpath, src)
temp = os.path.join(dotpath, new_src) temp = os.path.join(dotpath, new_src)
if not trans.transform(s, temp, templater=templater, debug=debug): if not trans.transform(s, temp, templater=templater, debug=debug):
@@ -774,6 +752,7 @@ def main():
return False return False
if o.debug: if o.debug:
LOG.debug = o.debug
LOG.dbg('\n\n') LOG.dbg('\n\n')
options_time = time.time() - t0 options_time = time.time() - t0
@@ -785,29 +764,25 @@ def main():
if o.cmd_profiles: if o.cmd_profiles:
# list existing profiles # list existing profiles
command = 'profiles' command = 'profiles'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
cmd_list_profiles(o) cmd_list_profiles(o)
elif o.cmd_files: elif o.cmd_files:
# list files for selected profile # list files for selected profile
command = 'files' command = 'files'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
cmd_files(o) cmd_files(o)
elif o.cmd_install: elif o.cmd_install:
# install the dotfiles stored in dotdrop # install the dotfiles stored in dotdrop
command = 'install' command = 'install'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
ret = cmd_install(o) ret = cmd_install(o)
elif o.cmd_compare: elif o.cmd_compare:
# compare local dotfiles with dotfiles stored in dotdrop # compare local dotfiles with dotfiles stored in dotdrop
command = 'compare' command = 'compare'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
tmp = get_tmpdir() tmp = get_tmpdir()
ret = cmd_compare(o, tmp) ret = cmd_compare(o, tmp)
# clean tmp directory # clean tmp directory
@@ -816,29 +791,25 @@ def main():
elif o.cmd_import: elif o.cmd_import:
# import dotfile(s) # import dotfile(s)
command = 'import' command = 'import'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
ret = cmd_importer(o) ret = cmd_importer(o)
elif o.cmd_update: elif o.cmd_update:
# update a dotfile # update a dotfile
command = 'update' command = 'update'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
ret = cmd_update(o) ret = cmd_update(o)
elif o.cmd_detail: elif o.cmd_detail:
# detail files # detail files
command = 'detail' command = 'detail'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
cmd_detail(o) cmd_detail(o)
elif o.cmd_remove: elif o.cmd_remove:
# remove dotfile # remove dotfile
command = 'remove' command = 'remove'
if o.debug: LOG.dbg('running cmd: {}'.format(command))
LOG.dbg('running cmd: {}'.format(command))
cmd_remove(o) cmd_remove(o)
except KeyboardInterrupt: except KeyboardInterrupt:
@@ -846,16 +817,14 @@ def main():
ret = False ret = False
cmd_time = time.time() - t0 cmd_time = time.time() - t0
if o.debug: LOG.dbg('done executing command \"{}\"'.format(command))
LOG.dbg('done executing command \"{}\"'.format(command)) LOG.dbg('options loaded in {}'.format(options_time))
LOG.dbg('options loaded in {}'.format(options_time)) LOG.dbg('command executed in {}'.format(cmd_time))
LOG.dbg('command executed in {}'.format(cmd_time))
if ret and o.conf.save(): if ret and o.conf.save():
LOG.log('config file updated') LOG.log('config file updated')
if o.debug: LOG.dbg('return {}'.format(ret))
LOG.dbg('return {}'.format(ret))
return ret return ret

View File

@@ -46,7 +46,7 @@ class Importer:
self.ignore = ignore self.ignore = ignore
self.umask = get_umask() self.umask = get_umask()
self.log = Logger() self.log = Logger(debug=self.debug)
def import_path(self, path, import_as=None, def import_path(self, path, import_as=None,
import_link=LinkTypes.NOLINK, import_mode=False): import_link=LinkTypes.NOLINK, import_mode=False):
@@ -57,8 +57,7 @@ class Importer:
0: ignored 0: ignored
-1: error -1: error
""" """
if self.debug: self.log.dbg('import {}'.format(path))
self.log.dbg('import {}'.format(path))
if not os.path.exists(path): if not os.path.exists(path):
self.log.err('\"{}\" does not exist, ignored!'.format(path)) self.log.err('\"{}\" does not exist, ignored!'.format(path))
return -1 return -1
@@ -100,8 +99,7 @@ class Importer:
src = src.rstrip(os.sep) src = src.rstrip(os.sep)
src = os.path.abspath(src) src = os.path.abspath(src)
src = strip_home(src) src = strip_home(src)
if self.debug: self.log.dbg('import src for {} as {}'.format(dst, src))
self.log.dbg('import src for {} as {}'.format(dst, src))
# with or without dot prefix # with or without dot prefix
strip = '.' + os.sep strip = '.' + os.sep
if self.keepdot: if self.keepdot:
@@ -121,8 +119,7 @@ class Importer:
if self._already_exists(src, dst): if self._already_exists(src, dst):
return -1 return -1
if self.debug: self.log.dbg('import dotfile: src:{} dst:{}'.format(src, dst))
self.log.dbg('import dotfile: src:{} dst:{}'.format(src, dst))
if not self._prepare_hierarchy(src, dst): if not self._prepare_hierarchy(src, dst):
return -1 return -1
@@ -140,12 +137,10 @@ class Importer:
# handle file mode # handle file mode
chmod = None chmod = None
dflperm = get_default_file_perms(dst, self.umask) dflperm = get_default_file_perms(dst, self.umask)
if self.debug: self.log.dbg('import mode: {}'.format(import_mode))
self.log.dbg('import mode: {}'.format(import_mode))
if import_mode or perm != dflperm: if import_mode or perm != dflperm:
if self.debug: msg = 'adopt mode {:o} (umask {:o})'
msg = 'adopt mode {:o} (umask {:o})' self.log.dbg(msg.format(perm, dflperm))
self.log.dbg(msg.format(perm, dflperm))
chmod = perm chmod = perm
# add file to config file # add file to config file
@@ -168,7 +163,7 @@ class Importer:
if os.path.exists(srcf): if os.path.exists(srcf):
if self.safe: if self.safe:
cmp = Comparator(debug=self.debug, cmp = Comparator(debug=self.debug,
diff_cmd=self.diff_cmd) diff_cmd=self.diff_cmd)
diff = cmp.compare(srcf, dst) diff = cmp.compare(srcf, dst)
if diff != '': if diff != '':
# files are different, dunno what to do # files are different, dunno what to do
@@ -178,8 +173,7 @@ class Importer:
msg = 'Dotfile \"{}\" already exists, overwrite?' msg = 'Dotfile \"{}\" already exists, overwrite?'
if not self.log.ask(msg.format(srcf)): if not self.log.ask(msg.format(srcf)):
return False return False
if self.debug: self.log.dbg('will overwrite existing file')
self.log.dbg('will overwrite existing file')
# create directory hierarchy # create directory hierarchy
if self.dry: if self.dry:
@@ -241,8 +235,7 @@ class Importer:
def _ignore(self, path): def _ignore(self, path):
if must_ignore([path], self.ignore, debug=self.debug): if must_ignore([path], self.ignore, debug=self.debug):
if self.debug: self.log.dbg('ignoring import of {}'.format(path))
self.log.dbg('ignoring import of {}'.format(path))
self.log.warn('{} ignored'.format(path)) self.log.warn('{} ignored'.format(path))
return True return True
return False return False

View File

@@ -57,7 +57,7 @@ class Installer:
# when using install_to_tmp for comparing # when using install_to_tmp for comparing
self.comparing = False self.comparing = False
self.log = Logger() self.log = Logger(debug=self.debug)
######################################################## ########################################################
# public methods # public methods
@@ -88,13 +88,11 @@ class Installer:
""" """
if not src or not dst: if not src or not dst:
# fake dotfile # fake dotfile
if self.debug: self.log.dbg('fake dotfile installed')
self.log.dbg('fake dotfile installed')
self._exec_pre_actions(actionexec) self._exec_pre_actions(actionexec)
return True, None return True, None
if self.debug: msg = 'installing \"{}\" to \"{}\" (link: {})'
msg = 'installing \"{}\" to \"{}\" (link: {})' self.log.dbg(msg.format(src, dst, str(linktype)))
self.log.dbg(msg.format(src, dst, str(linktype)))
src, dst, cont, err = self._check_paths(src, dst, chmod) src, dst, cont, err = self._check_paths(src, dst, chmod)
if not cont: if not cont:
return self._log_install(cont, err) return self._log_install(cont, err)
@@ -116,9 +114,8 @@ class Installer:
return self._log_install(r, err) return self._log_install(r, err)
isdir = os.path.isdir(src) isdir = os.path.isdir(src)
if self.debug: self.log.dbg('install {} to {}'.format(src, dst))
self.log.dbg('install {} to {}'.format(src, dst)) self.log.dbg('\"{}\" is a directory: {}'.format(src, isdir))
self.log.dbg('\"{}\" is a directory: {}'.format(src, isdir))
if linktype == LinkTypes.NOLINK: if linktype == LinkTypes.NOLINK:
# normal file # normal file
@@ -142,9 +139,8 @@ class Installer:
elif linktype == LinkTypes.LINK_CHILDREN: elif linktype == LinkTypes.LINK_CHILDREN:
# symlink direct children # symlink direct children
if not isdir: if not isdir:
if self.debug: msg = 'symlink children of {} to {}'
msg = 'symlink children of {} to {}' self.log.dbg(msg.format(src, dst))
self.log.dbg(msg.format(src, dst))
err = 'source dotfile is not a directory: {}'.format(src) err = 'source dotfile is not a directory: {}'.format(src)
r = False r = False
else: else:
@@ -153,8 +149,7 @@ class Installer:
is_template=is_template, is_template=is_template,
ignore=ignore) ignore=ignore)
if self.debug: self.log.dbg('before chmod: {} err:{}'.format(r, err))
self.log.dbg('before chmod: {} err:{}'.format(r, err))
if self.dry: if self.dry:
return self._log_install(r, err) return self._log_install(r, err)
@@ -202,8 +197,7 @@ class Installer:
return return
- success, error-if-any, dotfile-installed-path - success, error-if-any, dotfile-installed-path
""" """
if self.debug: self.log.dbg('tmp install {} (defined dst: {})'.format(src, dst))
self.log.dbg('tmp install {} (defined dst: {})'.format(src, dst))
src, dst, cont, err = self._check_paths(src, dst, chmod) src, dst, cont, err = self._check_paths(src, dst, chmod)
if not cont: if not cont:
return self._log_install(cont, err) return self._log_install(cont, err)
@@ -228,9 +222,8 @@ class Installer:
LinkTypes.NOLINK, LinkTypes.NOLINK,
is_template=is_template, is_template=is_template,
chmod=chmod, ignore=ignore) chmod=chmod, ignore=ignore)
if self.debug: if ret:
if ret: self.log.dbg('tmp installed in {}'.format(tmpdst))
self.log.dbg('tmp installed in {}'.format(tmpdst))
# restore flags # restore flags
self.dry = drysaved self.dry = drysaved
@@ -257,9 +250,8 @@ class Installer:
- False, 'aborted' : user aborted - False, 'aborted' : user aborted
""" """
if is_template: if is_template:
if self.debug: self.log.dbg('is a template')
self.log.dbg('is a template') self.log.dbg('install to {}'.format(self.workdir))
self.log.dbg('install to {}'.format(self.workdir))
tmp = self._pivot_path(dst, self.workdir, striphome=True) tmp = self._pivot_path(dst, self.workdir, striphome=True)
r, err = self.install(templater, src, tmp, r, err = self.install(templater, src, tmp,
LinkTypes.NOLINK, LinkTypes.NOLINK,
@@ -314,20 +306,17 @@ class Installer:
subdst = dsts[i] subdst = dsts[i]
if utils.must_ignore([subsrc, subdst], ignore, debug=self.debug): if utils.must_ignore([subsrc, subdst], ignore, debug=self.debug):
if self.debug: self.log.dbg(
self.log.dbg( 'ignoring install of {} to {}'.format(src, dst),
'ignoring install of {} to {}'.format(src, dst), )
)
continue continue
if self.debug: self.log.dbg('symlink child {} to {}'.format(subsrc, subdst))
self.log.dbg('symlink child {} to {}'.format(subsrc, subdst))
if is_template: if is_template:
if self.debug: self.log.dbg('child is a template')
self.log.dbg('child is a template') self.log.dbg('install to {} and symlink'
self.log.dbg('install to {} and symlink' .format(self.workdir))
.format(self.workdir))
tmp = self._pivot_path(subdst, self.workdir, striphome=True) tmp = self._pivot_path(subdst, self.workdir, striphome=True)
r, e = self.install(templater, subsrc, tmp, r, e = self.install(templater, subsrc, tmp,
LinkTypes.NOLINK, LinkTypes.NOLINK,
@@ -367,8 +356,7 @@ class Installer:
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):
msg = 'ignoring "{}", link already exists'.format(dst) msg = 'ignoring "{}", link already exists'.format(dst)
if self.debug: self.log.dbg(msg)
self.log.dbg(msg)
return False, None return False, None
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))
@@ -422,12 +410,11 @@ class Installer:
- False, None : ignored - False, None : ignored
- False, 'aborted' : user aborted - False, 'aborted' : user aborted
""" """
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)) self.log.dbg('ignore pattern: {}'.format(ignore))
self.log.dbg('ignore pattern: {}'.format(ignore)) self.log.dbg('is_template: {}'.format(is_template))
self.log.dbg('is_template: {}'.format(is_template)) self.log.dbg('no empty: {}'.format(noempty))
self.log.dbg('no empty: {}'.format(noempty))
# check no loop # check no loop
if utils.samefile(src, dst): if utils.samefile(src, dst):
@@ -435,8 +422,7 @@ class Installer:
return False, err return False, err
if utils.must_ignore([src, dst], ignore, debug=self.debug): if utils.must_ignore([src, dst], ignore, debug=self.debug):
if self.debug: self.log.dbg('ignoring install of {} to {}'.format(src, dst))
self.log.dbg('ignoring install of {} to {}'.format(src, dst))
return False, None return False, None
if utils.samefile(src, dst): if utils.samefile(src, dst):
@@ -461,8 +447,7 @@ class Installer:
templater.restore_vars(saved) templater.restore_vars(saved)
# test is empty # test is empty
if noempty and utils.content_empty(content): if noempty and utils.content_empty(content):
if self.debug: self.log.dbg('ignoring empty template: {}'.format(src))
self.log.dbg('ignoring empty template: {}'.format(src))
return False, None return False, None
if content is None: if content is None:
err = 'empty template {}'.format(src) err = 'empty template {}'.format(src)
@@ -490,8 +475,7 @@ class Installer:
- False, None : ignored - False, None : ignored
- False, 'aborted' : user aborted - False, 'aborted' : user aborted
""" """
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
ret = False, None ret = False, None
@@ -503,8 +487,7 @@ class Installer:
# 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 self.debug: self.log.dbg('deploy sub from {}: {}'.format(dst, entry))
self.log.dbg('deploy sub from {}: {}'.format(dst, entry))
if not os.path.isdir(f): if not os.path.isdir(f):
# is file # is file
res, err = self._copy_file(templater, f, res, err = self._copy_file(templater, f,
@@ -566,12 +549,10 @@ class Installer:
if self.diff: if self.diff:
if not self._is_different(src, dst, content=content): if not self._is_different(src, dst, content=content):
if self.debug: self.log.dbg('{} is the same'.format(dst))
self.log.dbg('{} is the same'.format(dst))
return False, None return False, None
if self.safe: if self.safe:
if self.debug: self.log.dbg('change detected for {}'.format(dst))
self.log.dbg('change detected for {}'.format(dst))
if self.showdiff: if self.showdiff:
# get diff # get diff
self._show_diff_before_write(src, dst, self._show_diff_before_write(src, dst,
@@ -588,8 +569,7 @@ class Installer:
r, e = self._exec_pre_actions(actionexec) r, e = self._exec_pre_actions(actionexec)
if not r: if not r:
return False, e return False, e
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)):
@@ -637,8 +617,7 @@ class Installer:
src = tmp src = tmp
r = utils.fastdiff(src, dst) r = utils.fastdiff(src, dst)
if r: if r:
if self.debug: self.log.dbg('content differ')
self.log.dbg('content differ')
return r return r
def _show_diff_before_write(self, src, dst, content=None): def _show_diff_before_write(self, src, dst, content=None):
@@ -668,16 +647,14 @@ 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):
if self.debug: self.log.dbg('no mkdir as \"create\" set to false in config')
self.log.dbg('no mkdir as \"create\" set to false in config')
return False return False
if os.path.exists(directory): if os.path.exists(directory):
return True return True
if self.dry: if self.dry:
self.log.dry('would mkdir -p {}'.format(directory)) self.log.dry('would mkdir -p {}'.format(directory))
return True return True
if self.debug: self.log.dbg('mkdir -p {}'.format(directory))
self.log.dbg('mkdir -p {}'.format(directory))
if not self.comparing: if not self.comparing:
self.log.sub('create directory {}'.format(directory)) self.log.sub('create directory {}'.format(directory))
@@ -694,15 +671,13 @@ class Installer:
def _pivot_path(self, path, newdir, striphome=False): def _pivot_path(self, path, newdir, striphome=False):
"""change path to be under newdir""" """change path to be under newdir"""
if self.debug: self.log.dbg('pivot new dir: \"{}\"'.format(newdir))
self.log.dbg('pivot new dir: \"{}\"'.format(newdir)) self.log.dbg('strip home: {}'.format(striphome))
self.log.dbg('strip home: {}'.format(striphome))
if striphome: if striphome:
path = utils.strip_home(path) path = utils.strip_home(path)
sub = path.lstrip(os.sep) sub = path.lstrip(os.sep)
new = os.path.join(newdir, sub) new = os.path.join(newdir, sub)
if self.debug: self.log.dbg('pivot \"{}\" to \"{}\"'.format(path, new))
self.log.dbg('pivot \"{}\" to \"{}\"'.format(path, new))
return new return new
def _exec_pre_actions(self, actionexec): def _exec_pre_actions(self, actionexec):
@@ -736,8 +711,7 @@ class Installer:
# check both path are valid # check both path are valid
if not dst or not src: if not dst or not src:
err = 'empty dst or src for {}'.format(src) err = 'empty dst or src for {}'.format(src)
if self.debug: self.log.dbg(err)
self.log.dbg(err)
return None, None, False, err return None, None, False, err
# normalize src and dst # normalize src and dst

View File

@@ -21,8 +21,8 @@ class Logger:
EMPH = '\033[33m' EMPH = '\033[33m'
BOLD = '\033[1m' BOLD = '\033[1m'
def __init__(self): def __init__(self, debug=False):
pass self.debug = debug
def log(self, string, end='\n', pre='', bold=False): def log(self, string, end='\n', pre='', bold=False):
cs = self._color(self.BLUE) cs = self._color(self.BLUE)
@@ -57,7 +57,9 @@ class Logger:
ce = self._color(self.RESET) ce = self._color(self.RESET)
sys.stderr.write('{}[WARN] {} {}{}'.format(cs, string, end, ce)) sys.stderr.write('{}[WARN] {} {}{}'.format(cs, string, end, ce))
def dbg(self, string): def dbg(self, string, force=False):
if not force and not self.debug:
return
frame = inspect.stack()[1] frame = inspect.stack()[1]
mod = inspect.getmodule(frame[0]).__name__ mod = inspect.getmodule(frame[0]).__name__
func = inspect.stack()[1][3] func = inspect.stack()[1][3]

View File

@@ -121,8 +121,8 @@ class Options(AttrMonitor):
self.args = docopt(USAGE, version=VERSION) self.args = docopt(USAGE, version=VERSION)
if args: if args:
self.args = args.copy() self.args = args.copy()
self.log = Logger()
self.debug = self.args['--verbose'] or ENV_DEBUG in os.environ self.debug = self.args['--verbose'] or ENV_DEBUG in os.environ
self.log = Logger(debug=self.debug)
self.dry = self.args['--dry'] self.dry = self.args['--dry']
if ENV_NODEBUG in os.environ: if ENV_NODEBUG in os.environ:
# force disabling debugs # force disabling debugs
@@ -131,13 +131,12 @@ class Options(AttrMonitor):
self.confpath = self._get_config_path() self.confpath = self._get_config_path()
if not self.confpath: if not self.confpath:
raise YamlException('no config file found') raise YamlException('no config file found')
if self.debug: self.log.dbg('#################################################')
self.log.dbg('#################################################') self.log.dbg('#################### DOTDROP ####################')
self.log.dbg('#################### DOTDROP ####################') self.log.dbg('#################################################')
self.log.dbg('#################################################') self.log.dbg('version: {}'.format(VERSION))
self.log.dbg('version: {}'.format(VERSION)) self.log.dbg('command: {}'.format(' '.join(sys.argv)))
self.log.dbg('command: {}'.format(' '.join(sys.argv))) self.log.dbg('config file: {}'.format(self.confpath))
self.log.dbg('config file: {}'.format(self.confpath))
self._read_config() self._read_config()
self._apply_args() self._apply_args()

View File

@@ -42,7 +42,7 @@ class Templategen:
""" """
self.base = base.rstrip(os.sep) self.base = base.rstrip(os.sep)
self.debug = debug self.debug = debug
self.log = Logger() self.log = Logger(debug=self.debug)
self.variables = {} self.variables = {}
loader1 = FileSystemLoader(self.base) loader1 = FileSystemLoader(self.base)
loader2 = FunctionLoader(self._template_loader) loader2 = FunctionLoader(self._template_loader)
@@ -66,18 +66,15 @@ class Templategen:
# adding header method # adding header method
self.env.globals['header'] = self._header self.env.globals['header'] = self._header
# adding helper methods # adding helper methods
if self.debug: self.log.dbg('load global functions:')
self.log.dbg('load global functions:')
self._load_funcs_to_dic(jhelpers, self.env.globals) self._load_funcs_to_dic(jhelpers, self.env.globals)
if func_file: if func_file:
for f in func_file: for f in func_file:
if self.debug: self.log.dbg('load custom functions from {}'.format(f))
self.log.dbg('load custom functions from {}'.format(f))
self._load_path_to_dic(f, self.env.globals) self._load_path_to_dic(f, self.env.globals)
if filter_file: if filter_file:
for f in filter_file: for f in filter_file:
if self.debug: self.log.dbg('load custom filters from {}'.format(f))
self.log.dbg('load custom filters from {}'.format(f))
self._load_path_to_dic(f, self.env.filters) self._load_path_to_dic(f, self.env.filters)
if self.debug: if self.debug:
self._debug_dict('template additional variables', variables) self._debug_dict('template additional variables', variables)
@@ -139,8 +136,7 @@ class Templategen:
return return
funcs = utils.get_module_functions(mod) funcs = utils.get_module_functions(mod)
for name, func in funcs: for name, func in funcs:
if self.debug: self.log.dbg('load function \"{}\"'.format(name))
self.log.dbg('load function \"{}\"'.format(name))
dic[name] = func dic[name] = func
def _header(self, prepend=''): def _header(self, prepend=''):
@@ -152,20 +148,16 @@ class Templategen:
try: try:
import magic import magic
filetype = magic.from_file(src, mime=True) filetype = magic.from_file(src, mime=True)
if self.debug: self.log.dbg('using \"magic\" for filetype identification')
self.log.dbg('using \"magic\" for filetype identification')
except ImportError: except ImportError:
# fallback # fallback
_, filetype = utils.run(['file', '-b', '--mime-type', src], _, filetype = utils.run(['file', '-b', '--mime-type', src],
debug=self.debug) debug=self.debug)
if self.debug: self.log.dbg('using \"file\" for filetype identification')
self.log.dbg('using \"file\" for filetype identification')
filetype = filetype.strip() filetype = filetype.strip()
istext = self._is_text(filetype) istext = self._is_text(filetype)
if self.debug: self.log.dbg('filetype \"{}\": {}'.format(src, filetype))
self.log.dbg('filetype \"{}\": {}'.format(src, filetype)) self.log.dbg('is text \"{}\": {}'.format(src, istext))
if self.debug:
self.log.dbg('is text \"{}\": {}'.format(src, istext))
if not istext: if not istext:
return self._handle_bin_file(src) return self._handle_bin_file(src)
return self._handle_text_file(src) return self._handle_text_file(src)

View File

@@ -52,7 +52,7 @@ class Updater:
debug=self.debug) debug=self.debug)
# save template vars # save template vars
self.tvars = self.templater.add_tmp_vars() self.tvars = self.templater.add_tmp_vars()
self.log = Logger() self.log = Logger(debug=self.debug)
def update_path(self, path): def update_path(self, path):
"""update the dotfile installed on path""" """update the dotfile installed on path"""
@@ -69,9 +69,8 @@ class Updater:
self.log.err(msg.format(dotfile.key)) self.log.err(msg.format(dotfile.key))
return False return False
if self.debug: msg = 'updating {} from path \"{}\"'
msg = 'updating {} from path \"{}\"' self.log.dbg(msg.format(dotfile, path))
self.log.dbg(msg.format(dotfile, path))
if not self._update(path, dotfile): if not self._update(path, dotfile):
return False return False
return True return True
@@ -81,8 +80,7 @@ class Updater:
dotfile = self.conf.get_dotfile(key) dotfile = self.conf.get_dotfile(key)
if not dotfile: if not dotfile:
return False return False
if self.debug: self.log.dbg('updating {} from key \"{}\"'.format(dotfile, key))
self.log.dbg('updating {} from key \"{}\"'.format(dotfile, key))
path = self.conf.path_to_dotfile_dst(dotfile.dst) path = self.conf.path_to_dotfile_dst(dotfile.dst)
return self._update(path, dotfile) return self._update(path, dotfile)
@@ -92,8 +90,7 @@ class Updater:
new_path = None new_path = None
ignores = list(set(self.ignore + dotfile.upignore)) ignores = list(set(self.ignore + dotfile.upignore))
self.ignores = patch_ignores(ignores, dotfile.dst, debug=self.debug) self.ignores = patch_ignores(ignores, dotfile.dst, debug=self.debug)
if self.debug: self.log.dbg('ignore pattern(s): {}'.format(self.ignores))
self.log.dbg('ignore pattern(s): {}'.format(self.ignores))
deployed_path = os.path.expanduser(path) deployed_path = os.path.expanduser(path)
local_path = os.path.join(self.dotpath, dotfile.src) local_path = os.path.join(self.dotpath, dotfile.src)
@@ -132,9 +129,8 @@ class Updater:
if deployed_mode != local_mode: if deployed_mode != local_mode:
# mirror rights # mirror rights
if self.debug: m = 'adopt mode {:o} for {}'
m = 'adopt mode {:o} for {}' self.log.dbg(m.format(deployed_mode, dotfile.key))
self.log.dbg(m.format(deployed_mode, dotfile.key))
r = self.conf.update_dotfile(dotfile.key, deployed_mode) r = self.conf.update_dotfile(dotfile.key, deployed_mode)
if r: if r:
ret = True ret = True
@@ -149,8 +145,7 @@ class Updater:
trans = dotfile.get_trans_w() trans = dotfile.get_trans_w()
if not trans: if not trans:
return path return path
if self.debug: self.log.dbg('executing write transformation {}'.format(trans))
self.log.dbg('executing write transformation {}'.format(trans))
tmp = get_unique_tmp_name() tmp = get_unique_tmp_name()
self.templater.restore_vars(self.tvars) self.templater.restore_vars(self.tvars)
newvars = dotfile.get_dotfile_variables() newvars = dotfile.get_dotfile_variables()
@@ -166,8 +161,7 @@ class Updater:
def _is_template(self, path): def _is_template(self, path):
if not Templategen.is_template(path, ignore=self.ignores): if not Templategen.is_template(path, ignore=self.ignores):
if self.debug: self.log.dbg('{} is NO template'.format(path))
self.log.dbg('{} is NO template'.format(path))
return False return False
self.log.warn('{} uses template, update manually'.format(path)) self.log.warn('{} uses template, update manually'.format(path))
return True return True
@@ -201,9 +195,8 @@ class Updater:
dstr = get_file_perm(dst) dstr = get_file_perm(dst)
if srcr == dstr: if srcr == dstr:
return return
if self.debug: msg = 'copy rights from {} ({:o}) to {} ({:o})'
msg = 'copy rights from {} ({:o}) to {} ({:o})' self.log.dbg(msg.format(src, srcr, dst, dstr))
self.log.dbg(msg.format(src, srcr, dst, dstr))
try: try:
mirror_file_rights(src, dst) mirror_file_rights(src, dst)
except OSError as e: except OSError as e:
@@ -214,15 +207,13 @@ class Updater:
if self._ignore([deployed_path, local_path]): if self._ignore([deployed_path, local_path]):
self.log.sub('\"{}\" ignored'.format(local_path)) self.log.sub('\"{}\" ignored'.format(local_path))
return True return True
if self.debug: self.log.dbg('update for file {} and {}'.format(
self.log.dbg('update for file {} and {}'.format( deployed_path,
deployed_path, local_path,
local_path, ))
))
if self._is_template(local_path): if self._is_template(local_path):
# dotfile is a template # dotfile is a template
if self.debug: self.log.dbg('{} is a template'.format(local_path))
self.log.dbg('{} is a template'.format(local_path))
if self.showpatch: if self.showpatch:
try: try:
self._show_patch(deployed_path, local_path) self._show_patch(deployed_path, local_path)
@@ -237,11 +228,10 @@ class Updater:
filecmp.cmp(deployed_path, local_path, shallow=False) and \ filecmp.cmp(deployed_path, local_path, shallow=False) and \
self._same_rights(deployed_path, local_path): self._same_rights(deployed_path, local_path):
# no difference # no difference
if self.debug: self.log.dbg('identical files: {} and {}'.format(
self.log.dbg('identical files: {} and {}'.format( deployed_path,
deployed_path, local_path,
local_path, ))
))
return True return True
if not self._overwrite(deployed_path, local_path): if not self._overwrite(deployed_path, local_path):
return False return False
@@ -252,8 +242,7 @@ class Updater:
local_path, local_path,
)) ))
else: else:
if self.debug: self.log.dbg('cp {} {}'.format(deployed_path, local_path))
self.log.dbg('cp {} {}'.format(deployed_path, local_path))
shutil.copyfile(deployed_path, local_path) shutil.copyfile(deployed_path, local_path)
self._mirror_rights(deployed_path, local_path) self._mirror_rights(deployed_path, local_path)
self.log.sub('\"{}\" updated'.format(local_path)) self.log.sub('\"{}\" updated'.format(local_path))
@@ -267,11 +256,10 @@ class Updater:
def _handle_dir(self, deployed_path, local_path, dotfile): def _handle_dir(self, deployed_path, local_path, dotfile):
"""sync path (local dir) and local_path (dotdrop dir path)""" """sync path (local dir) and local_path (dotdrop dir path)"""
if self.debug: self.log.dbg('handle update for dir {} to {}'.format(
self.log.dbg('handle update for dir {} to {}'.format( deployed_path,
deployed_path, local_path,
local_path, ))
))
# paths must be absolute (no tildes) # paths must be absolute (no tildes)
deployed_path = os.path.expanduser(deployed_path) deployed_path = os.path.expanduser(deployed_path)
local_path = os.path.expanduser(local_path) local_path = os.path.expanduser(local_path)
@@ -289,8 +277,7 @@ class Updater:
def _merge_dirs(self, diff, dotfile): def _merge_dirs(self, diff, dotfile):
"""Synchronize directories recursively.""" """Synchronize directories recursively."""
left, right = diff.left, diff.right left, right = diff.left, diff.right
if self.debug: self.log.dbg('sync dir {} to {}'.format(left, right))
self.log.dbg('sync dir {} to {}'.format(left, right))
if self._ignore([left, right]): if self._ignore([left, right]):
return True return True
@@ -312,8 +299,7 @@ class Updater:
if self.dry: if self.dry:
self.log.dry('would cp -r {} {}'.format(exist, new)) self.log.dry('would cp -r {} {}'.format(exist, new))
continue continue
if self.debug: self.log.dbg('cp -r {} {}'.format(exist, new))
self.log.dbg('cp -r {} {}'.format(exist, new))
# Newly created directory should be copied as is (for efficiency). # Newly created directory should be copied as is (for efficiency).
def ig(src, names): def ig(src, names):
@@ -349,8 +335,7 @@ class Updater:
if self.dry: if self.dry:
self.log.dry('would rm -r {}'.format(old)) self.log.dry('would rm -r {}'.format(old))
continue continue
if self.debug: self.log.dbg('rm -r {}'.format(old))
self.log.dbg('rm -r {}'.format(old))
if not self._confirm_rm_r(old): if not self._confirm_rm_r(old):
continue continue
removepath(old, logger=self.log) removepath(old, logger=self.log)
@@ -370,8 +355,7 @@ class Updater:
if self.dry: if self.dry:
self.log.dry('would cp {} {}'.format(fleft, fright)) self.log.dry('would cp {} {}'.format(fleft, fright))
continue continue
if self.debug: self.log.dbg('cp {} {}'.format(fleft, fright))
self.log.dbg('cp {} {}'.format(fleft, fright))
self._handle_file(fleft, fright, dotfile, compare=False) self._handle_file(fleft, fright, dotfile, compare=False)
# copy files that don't exist in dotdrop # copy files that don't exist in dotdrop
@@ -387,8 +371,7 @@ class Updater:
if self.dry: if self.dry:
self.log.dry('would cp {} {}'.format(exist, new)) self.log.dry('would cp {} {}'.format(exist, new))
continue continue
if self.debug: self.log.dbg('cp {} {}'.format(exist, new))
self.log.dbg('cp {} {}'.format(exist, new))
try: try:
shutil.copyfile(exist, new) shutil.copyfile(exist, new)
except OSError as e: except OSError as e:
@@ -412,8 +395,7 @@ class Updater:
if self.dry: if self.dry:
self.log.dry('would rm {}'.format(new)) self.log.dry('would rm {}'.format(new))
continue continue
if self.debug: self.log.dbg('rm {}'.format(new))
self.log.dbg('rm {}'.format(new))
removepath(new, logger=self.log) removepath(new, logger=self.log)
self.log.sub('\"{}\" removed'.format(new)) self.log.sub('\"{}\" removed'.format(new))
@@ -447,7 +429,6 @@ class Updater:
def _ignore(self, paths): def _ignore(self, paths):
if must_ignore(paths, self.ignores, debug=self.debug): if must_ignore(paths, self.ignores, debug=self.debug):
if self.debug: self.log.dbg('ignoring update for {}'.format(paths))
self.log.dbg('ignoring update for {}'.format(paths))
return True return True
return False return False

View File

@@ -37,7 +37,7 @@ NOREMOVE = [os.path.normpath(p) for p in DONOTDELETE]
def run(cmd, debug=False): def run(cmd, debug=False):
"""run a command (expects a list)""" """run a command (expects a list)"""
if debug: if debug:
LOG.dbg('exec: {}'.format(' '.join(cmd))) LOG.dbg('exec: {}'.format(' '.join(cmd)), force=True)
p = subprocess.Popen(cmd, shell=False, p = subprocess.Popen(cmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = p.communicate() out, _ = p.communicate()
@@ -61,10 +61,10 @@ def shell(cmd, debug=False):
returns True|False, output returns True|False, output
""" """
if debug: if debug:
LOG.dbg('shell exec: \"{}\"'.format(cmd)) LOG.dbg('shell exec: \"{}\"'.format(cmd), force=True)
ret, out = subprocess.getstatusoutput(cmd) ret, out = subprocess.getstatusoutput(cmd)
if debug: if debug:
LOG.dbg('shell result ({}): {}'.format(ret, out)) LOG.dbg('shell result ({}): {}'.format(ret, out), force=True)
return ret == 0, out return ret == 0, out
@@ -203,7 +203,8 @@ def must_ignore(paths, ignores, debug=False):
if not ignores: if not ignores:
return False return False
if debug: if debug:
LOG.dbg('must ignore? \"{}\" against {}'.format(paths, ignores)) LOG.dbg('must ignore? \"{}\" against {}'.format(paths, ignores),
force=True)
ignored_negative, ignored = categorize( ignored_negative, ignored = categorize(
lambda ign: ign.startswith('!'), ignores) lambda ign: ign.startswith('!'), ignores)
for p in paths: for p in paths:
@@ -212,7 +213,7 @@ def must_ignore(paths, ignores, debug=False):
for i in ignored: for i in ignored:
if fnmatch.fnmatch(p, i): if fnmatch.fnmatch(p, i):
if debug: if debug:
LOG.dbg('ignore \"{}\" match: {}'.format(i, p)) LOG.dbg('ignore \"{}\" match: {}'.format(i, p), force=True)
ignore_matches.append(p) ignore_matches.append(p)
# Then remove any matches that actually shouldn't be ignored # Then remove any matches that actually shouldn't be ignored
for ni in ignored_negative: for ni in ignored_negative:
@@ -220,7 +221,8 @@ def must_ignore(paths, ignores, debug=False):
ni = ni[1:] ni = ni[1:]
if fnmatch.fnmatch(p, ni): if fnmatch.fnmatch(p, ni):
if debug: if debug:
LOG.dbg('negative ignore \"{}\" match: {}'.format(ni, p)) LOG.dbg('negative ignore \"{}\" match: {}'.format(ni, p),
force=True)
try: try:
ignore_matches.remove(p) ignore_matches.remove(p)
except ValueError: except ValueError:
@@ -232,7 +234,7 @@ def must_ignore(paths, ignores, debug=False):
if ignore_matches: if ignore_matches:
return True return True
if debug: if debug:
LOG.dbg('NOT ignoring {}'.format(paths)) LOG.dbg('NOT ignoring {}'.format(paths), force=True)
return False return False
@@ -249,7 +251,7 @@ def patch_ignores(ignores, prefix, debug=False):
"""allow relative ignore pattern""" """allow relative ignore pattern"""
new = [] new = []
if debug: if debug:
LOG.dbg('ignores before patching: {}'.format(ignores)) LOG.dbg('ignores before patching: {}'.format(ignores), force=True)
for ignore in ignores: for ignore in ignores:
negative = ignore.startswith('!') negative = ignore.startswith('!')
if negative: if negative:
@@ -277,7 +279,7 @@ def patch_ignores(ignores, prefix, debug=False):
else: else:
new.append(path) new.append(path)
if debug: if debug:
LOG.dbg('ignores after patching: {}'.format(new)) LOG.dbg('ignores after patching: {}'.format(new), force=True)
return new return new
@@ -375,7 +377,7 @@ def get_file_perm(path):
def chmod(path, mode, debug=False): def chmod(path, mode, debug=False):
if debug: if debug:
LOG.dbg('chmod {} {}'.format(oct(mode), path)) LOG.dbg('chmod {} {}'.format(oct(mode), path), force=True)
os.chmod(path, mode) os.chmod(path, mode)
return get_file_perm(path) == mode return get_file_perm(path) == mode