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

better log for debugging

This commit is contained in:
deadc0de6
2018-07-17 08:37:37 +02:00
parent 2243ddfc64
commit c8a37c1302
4 changed files with 43 additions and 28 deletions

View File

@@ -94,9 +94,11 @@ def install(opts, conf):
if opts['dry']:
LOG.dry('would execute action: {}'.format(action))
else:
LOG.dbg('executing pre action {}'.format(action))
if opts['debug']:
LOG.dbg('executing pre action {}'.format(action))
action.execute()
LOG.dbg('installing {}'.format(dotfile))
if opts['debug']:
LOG.dbg('installing {}'.format(dotfile))
if hasattr(dotfile, 'link') and dotfile.link:
r = inst.link(dotfile.src, dotfile.dst)
else:
@@ -120,7 +122,8 @@ def install(opts, conf):
if opts['dry']:
LOG.dry('would execute action: {}'.format(action))
else:
LOG.dbg('executing post action {}'.format(action))
if opts['debug']:
LOG.dbg('executing post action {}'.format(action))
action.execute()
installed.extend(r)
LOG.log('\n{} dotfile(s) installed.'.format(len(installed)))
@@ -134,7 +137,8 @@ def apply_trans(opts, dotfile):
new_src = '{}.{}'.format(src, TRANS_SUFFIX)
err = False
for trans in dotfile.trans:
LOG.dbg('executing transformation {}'.format(trans))
if opts['debug']:
LOG.dbg('executing transformation {}'.format(trans))
s = os.path.join(opts['dotpath'], src)
temp = os.path.join(opts['dotpath'], new_src)
if not trans.transform(s, temp):
@@ -178,7 +182,8 @@ def compare(opts, conf, tmp, focus=None):
return ret
for dotfile in selected:
LOG.dbg('comparing {}'.format(dotfile))
if opts['debug']:
LOG.dbg('comparing {}'.format(dotfile))
src = dotfile.src
tmpsrc = None
if dotfile.trans:
@@ -194,9 +199,10 @@ def compare(opts, conf, tmp, focus=None):
if os.path.exists(tmpsrc):
remove(tmpsrc)
if same:
LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
dotfile.dst))
LOG.dbg('same file')
if opts['debug']:
LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
dotfile.dst))
LOG.dbg('same file')
else:
LOG.log('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
dotfile.dst))
@@ -352,9 +358,9 @@ def main():
opts['link'] = args['--link']
opts['debug'] = args['--verbose']
LOG.debug = opts['debug']
LOG.dbg('config file: {}'.format(args['--cfg']))
LOG.dbg('opts: {}'.format(opts))
if opts['debug']:
LOG.dbg('config file: {}'.format(args['--cfg']))
LOG.dbg('opts: {}'.format(opts))
if opts['banner'] and not args['--no-banner']:
header()

View File

@@ -26,7 +26,7 @@ class Installer:
self.debug = debug
self.diff = diff
self.comparing = False
self.log = Logger(debug=self.debug)
self.log = Logger()
def install(self, templater, profile, src, dst):
"""install the src to dst using a template"""
@@ -36,7 +36,8 @@ class Installer:
# symlink loop
self.log.err('dotfile points to itself: {}'.format(dst))
return []
self.log.dbg('install {} to {}'.format(src, dst))
if self.debug:
self.log.dbg('install {} to {}'.format(src, dst))
if os.path.isdir(src):
return self._handle_dir(templater, profile, src, dst)
return self._handle_file(templater, profile, src, dst)
@@ -47,7 +48,8 @@ class Installer:
dst = os.path.join(self.base, os.path.expanduser(dst))
if os.path.lexists(dst):
if os.path.realpath(dst) == os.path.realpath(src):
self.log.dbg('ignoring "{}", link exists'.format(dst))
if self.debug:
self.log.dbg('ignoring "{}", link exists'.format(dst))
return []
if self.dry:
self.log.dry('would remove {} and link to {}'.format(dst, src))
@@ -75,7 +77,8 @@ class Installer:
def _handle_file(self, templater, profile, src, dst):
"""install src to dst when is a file"""
self.log.dbg('generate template for {}'.format(src))
if self.debug:
self.log.dbg('generate template for {}'.format(src))
if utils.samefile(src, dst):
# symlink loop
self.log.err('dotfile points to itself: {}'.format(dst))
@@ -93,7 +96,8 @@ class Installer:
self.log.err('installing \"{}\" to \"{}\"'.format(src, dst))
return []
if ret > 0:
self.log.dbg('ignoring \"{}\", same content'.format(dst))
if self.debug:
self.log.dbg('ignoring \"{}\", same content'.format(dst))
return []
if ret == 0:
if not self.dry and not self.comparing:
@@ -136,7 +140,8 @@ class Installer:
if os.path.lexists(dst):
samerights = os.stat(dst).st_mode == rights
if self.diff and self._fake_diff(dst, content) and samerights:
self.log.dbg('{} is the same'.format(dst))
if self.debug:
self.log.dbg('{} is the same'.format(dst))
return 1
if self.safe and not self.log.ask('Overwrite \"{}\"'.format(dst)):
self.log.warn('ignoring \"{}\", already present'.format(dst))
@@ -147,7 +152,8 @@ class Installer:
if not self._create_dirs(base):
self.log.err('creating directory for \"{}\"'.format(dst))
return -1
self.log.dbg('write content to {}'.format(dst))
if self.debug:
self.log.dbg('write content to {}'.format(dst))
try:
with open(dst, 'wb') as f:
f.write(content)
@@ -166,7 +172,8 @@ class Installer:
if self.dry:
self.log.dry('would mkdir -p {}'.format(directory))
return True
self.log.dbg('mkdir -p {}'.format(directory))
if self.debug:
self.log.dbg('mkdir -p {}'.format(directory))
os.makedirs(directory)
return os.path.exists(directory)
@@ -200,7 +207,8 @@ class Installer:
# normalize src and dst
src = os.path.expanduser(src)
dst = os.path.expanduser(dst)
self.log.dbg('comparing {} and {}'.format(src, dst))
if self.debug:
self.log.dbg('comparing {} and {}'.format(src, dst))
if not os.path.lexists(dst):
# destination dotfile does not exist
retval = False, '\"{}\" does not exist on local\n'.format(dst)
@@ -209,7 +217,8 @@ class Installer:
ret, tmpdst = self._install_to_temp(templater, profile,
src, dst, tmpdir)
if ret:
self.log.dbg('diffing {} and {}'.format(tmpdst, dst))
if self.debug:
self.log.dbg('diffing {} and {}'.format(tmpdst, dst))
diff = utils.diff(tmpdst, dst, raw=False, opts=opts)
if diff == '':
retval = True, ''

View File

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

View File

@@ -35,7 +35,7 @@ class Templategen:
variable_end_string=VAR_END,
comment_start_string=COMMENT_START,
comment_end_string=COMMENT_END)
self.log = Logger(debug=debug)
self.log = Logger()
def generate(self, src, profile):
if not os.path.exists(src):
@@ -46,9 +46,11 @@ class Templategen:
"""generate the file content from template"""
filetype = utils.run(['file', '-b', src], raw=False, debug=self.debug)
filetype = filetype.strip()
self.log.dbg('\"{}\" filetype: {}'.format(src, filetype))
if self.debug:
self.log.dbg('\"{}\" filetype: {}'.format(src, filetype))
istext = 'text' in filetype
self.log.dbg('\"{}\" is text: {}'.format(src, istext))
if self.debug:
self.log.dbg('\"{}\" is text: {}'.format(src, istext))
if not istext:
return self._handle_bin_file(src, profile)
return self._handle_text_file(src, profile)