mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-09 04:49:17 +00:00
better log for debugging
This commit is contained in:
@@ -94,9 +94,11 @@ def install(opts, conf):
|
|||||||
if opts['dry']:
|
if opts['dry']:
|
||||||
LOG.dry('would execute action: {}'.format(action))
|
LOG.dry('would execute action: {}'.format(action))
|
||||||
else:
|
else:
|
||||||
LOG.dbg('executing pre action {}'.format(action))
|
if opts['debug']:
|
||||||
|
LOG.dbg('executing pre action {}'.format(action))
|
||||||
action.execute()
|
action.execute()
|
||||||
LOG.dbg('installing {}'.format(dotfile))
|
if opts['debug']:
|
||||||
|
LOG.dbg('installing {}'.format(dotfile))
|
||||||
if hasattr(dotfile, 'link') and dotfile.link:
|
if hasattr(dotfile, 'link') and dotfile.link:
|
||||||
r = inst.link(dotfile.src, dotfile.dst)
|
r = inst.link(dotfile.src, dotfile.dst)
|
||||||
else:
|
else:
|
||||||
@@ -120,7 +122,8 @@ def install(opts, conf):
|
|||||||
if opts['dry']:
|
if opts['dry']:
|
||||||
LOG.dry('would execute action: {}'.format(action))
|
LOG.dry('would execute action: {}'.format(action))
|
||||||
else:
|
else:
|
||||||
LOG.dbg('executing post action {}'.format(action))
|
if opts['debug']:
|
||||||
|
LOG.dbg('executing post action {}'.format(action))
|
||||||
action.execute()
|
action.execute()
|
||||||
installed.extend(r)
|
installed.extend(r)
|
||||||
LOG.log('\n{} dotfile(s) installed.'.format(len(installed)))
|
LOG.log('\n{} dotfile(s) installed.'.format(len(installed)))
|
||||||
@@ -134,7 +137,8 @@ def apply_trans(opts, dotfile):
|
|||||||
new_src = '{}.{}'.format(src, TRANS_SUFFIX)
|
new_src = '{}.{}'.format(src, TRANS_SUFFIX)
|
||||||
err = False
|
err = False
|
||||||
for trans in dotfile.trans:
|
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)
|
s = os.path.join(opts['dotpath'], src)
|
||||||
temp = os.path.join(opts['dotpath'], new_src)
|
temp = os.path.join(opts['dotpath'], new_src)
|
||||||
if not trans.transform(s, temp):
|
if not trans.transform(s, temp):
|
||||||
@@ -178,7 +182,8 @@ def compare(opts, conf, tmp, focus=None):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
for dotfile in selected:
|
for dotfile in selected:
|
||||||
LOG.dbg('comparing {}'.format(dotfile))
|
if opts['debug']:
|
||||||
|
LOG.dbg('comparing {}'.format(dotfile))
|
||||||
src = dotfile.src
|
src = dotfile.src
|
||||||
tmpsrc = None
|
tmpsrc = None
|
||||||
if dotfile.trans:
|
if dotfile.trans:
|
||||||
@@ -194,9 +199,10 @@ def compare(opts, conf, tmp, focus=None):
|
|||||||
if os.path.exists(tmpsrc):
|
if os.path.exists(tmpsrc):
|
||||||
remove(tmpsrc)
|
remove(tmpsrc)
|
||||||
if same:
|
if same:
|
||||||
LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
|
if opts['debug']:
|
||||||
dotfile.dst))
|
LOG.dbg('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
|
||||||
LOG.dbg('same file')
|
dotfile.dst))
|
||||||
|
LOG.dbg('same file')
|
||||||
else:
|
else:
|
||||||
LOG.log('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
|
LOG.log('diffing \"{}\" VS \"{}\"'.format(dotfile.key,
|
||||||
dotfile.dst))
|
dotfile.dst))
|
||||||
@@ -352,9 +358,9 @@ def main():
|
|||||||
opts['link'] = args['--link']
|
opts['link'] = args['--link']
|
||||||
opts['debug'] = args['--verbose']
|
opts['debug'] = args['--verbose']
|
||||||
|
|
||||||
LOG.debug = opts['debug']
|
if opts['debug']:
|
||||||
LOG.dbg('config file: {}'.format(args['--cfg']))
|
LOG.dbg('config file: {}'.format(args['--cfg']))
|
||||||
LOG.dbg('opts: {}'.format(opts))
|
LOG.dbg('opts: {}'.format(opts))
|
||||||
|
|
||||||
if opts['banner'] and not args['--no-banner']:
|
if opts['banner'] and not args['--no-banner']:
|
||||||
header()
|
header()
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class Installer:
|
|||||||
self.debug = debug
|
self.debug = debug
|
||||||
self.diff = diff
|
self.diff = diff
|
||||||
self.comparing = False
|
self.comparing = False
|
||||||
self.log = Logger(debug=self.debug)
|
self.log = Logger()
|
||||||
|
|
||||||
def install(self, templater, profile, src, dst):
|
def install(self, templater, profile, src, dst):
|
||||||
"""install the src to dst using a template"""
|
"""install the src to dst using a template"""
|
||||||
@@ -36,7 +36,8 @@ class Installer:
|
|||||||
# symlink loop
|
# symlink loop
|
||||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
self.log.err('dotfile points to itself: {}'.format(dst))
|
||||||
return []
|
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):
|
if os.path.isdir(src):
|
||||||
return self._handle_dir(templater, profile, src, dst)
|
return self._handle_dir(templater, profile, src, dst)
|
||||||
return self._handle_file(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))
|
dst = os.path.join(self.base, os.path.expanduser(dst))
|
||||||
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):
|
||||||
self.log.dbg('ignoring "{}", link exists'.format(dst))
|
if self.debug:
|
||||||
|
self.log.dbg('ignoring "{}", link exists'.format(dst))
|
||||||
return []
|
return []
|
||||||
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))
|
||||||
@@ -75,7 +77,8 @@ class Installer:
|
|||||||
|
|
||||||
def _handle_file(self, templater, profile, src, dst):
|
def _handle_file(self, templater, profile, src, dst):
|
||||||
"""install src to dst when is a file"""
|
"""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):
|
if utils.samefile(src, dst):
|
||||||
# symlink loop
|
# symlink loop
|
||||||
self.log.err('dotfile points to itself: {}'.format(dst))
|
self.log.err('dotfile points to itself: {}'.format(dst))
|
||||||
@@ -93,7 +96,8 @@ class Installer:
|
|||||||
self.log.err('installing \"{}\" to \"{}\"'.format(src, dst))
|
self.log.err('installing \"{}\" to \"{}\"'.format(src, dst))
|
||||||
return []
|
return []
|
||||||
if ret > 0:
|
if ret > 0:
|
||||||
self.log.dbg('ignoring \"{}\", same content'.format(dst))
|
if self.debug:
|
||||||
|
self.log.dbg('ignoring \"{}\", same content'.format(dst))
|
||||||
return []
|
return []
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
if not self.dry and not self.comparing:
|
if not self.dry and not self.comparing:
|
||||||
@@ -136,7 +140,8 @@ class Installer:
|
|||||||
if os.path.lexists(dst):
|
if os.path.lexists(dst):
|
||||||
samerights = os.stat(dst).st_mode == rights
|
samerights = os.stat(dst).st_mode == rights
|
||||||
if self.diff and self._fake_diff(dst, content) and samerights:
|
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
|
return 1
|
||||||
if self.safe and not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
if self.safe and not self.log.ask('Overwrite \"{}\"'.format(dst)):
|
||||||
self.log.warn('ignoring \"{}\", already present'.format(dst))
|
self.log.warn('ignoring \"{}\", already present'.format(dst))
|
||||||
@@ -147,7 +152,8 @@ class Installer:
|
|||||||
if not self._create_dirs(base):
|
if not self._create_dirs(base):
|
||||||
self.log.err('creating directory for \"{}\"'.format(dst))
|
self.log.err('creating directory for \"{}\"'.format(dst))
|
||||||
return -1
|
return -1
|
||||||
self.log.dbg('write content to {}'.format(dst))
|
if self.debug:
|
||||||
|
self.log.dbg('write content to {}'.format(dst))
|
||||||
try:
|
try:
|
||||||
with open(dst, 'wb') as f:
|
with open(dst, 'wb') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
@@ -166,7 +172,8 @@ class Installer:
|
|||||||
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
|
||||||
self.log.dbg('mkdir -p {}'.format(directory))
|
if self.debug:
|
||||||
|
self.log.dbg('mkdir -p {}'.format(directory))
|
||||||
os.makedirs(directory)
|
os.makedirs(directory)
|
||||||
return os.path.exists(directory)
|
return os.path.exists(directory)
|
||||||
|
|
||||||
@@ -200,7 +207,8 @@ class Installer:
|
|||||||
# normalize src and dst
|
# normalize src and dst
|
||||||
src = os.path.expanduser(src)
|
src = os.path.expanduser(src)
|
||||||
dst = os.path.expanduser(dst)
|
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):
|
if not os.path.lexists(dst):
|
||||||
# destination dotfile does not exist
|
# destination dotfile does not exist
|
||||||
retval = False, '\"{}\" does not exist on local\n'.format(dst)
|
retval = False, '\"{}\" does not exist on local\n'.format(dst)
|
||||||
@@ -209,7 +217,8 @@ class Installer:
|
|||||||
ret, tmpdst = self._install_to_temp(templater, profile,
|
ret, tmpdst = self._install_to_temp(templater, profile,
|
||||||
src, dst, tmpdir)
|
src, dst, tmpdir)
|
||||||
if ret:
|
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)
|
diff = utils.diff(tmpdst, dst, raw=False, opts=opts)
|
||||||
if diff == '':
|
if diff == '':
|
||||||
retval = True, ''
|
retval = True, ''
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ class Logger:
|
|||||||
RESET = '\033[0m'
|
RESET = '\033[0m'
|
||||||
EMPH = '\033[33m'
|
EMPH = '\033[33m'
|
||||||
|
|
||||||
def __init__(self, debug=False):
|
def __init__(self):
|
||||||
self.debug = debug
|
pass
|
||||||
|
|
||||||
def log(self, string, end='\n', pre=''):
|
def log(self, string, end='\n', pre=''):
|
||||||
cs = self._color(self.BLUE)
|
cs = self._color(self.BLUE)
|
||||||
@@ -48,8 +48,6 @@ class Logger:
|
|||||||
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):
|
||||||
if 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]
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class Templategen:
|
|||||||
variable_end_string=VAR_END,
|
variable_end_string=VAR_END,
|
||||||
comment_start_string=COMMENT_START,
|
comment_start_string=COMMENT_START,
|
||||||
comment_end_string=COMMENT_END)
|
comment_end_string=COMMENT_END)
|
||||||
self.log = Logger(debug=debug)
|
self.log = Logger()
|
||||||
|
|
||||||
def generate(self, src, profile):
|
def generate(self, src, profile):
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
@@ -46,9 +46,11 @@ class Templategen:
|
|||||||
"""generate the file content from template"""
|
"""generate the file content from template"""
|
||||||
filetype = utils.run(['file', '-b', src], raw=False, debug=self.debug)
|
filetype = utils.run(['file', '-b', src], raw=False, debug=self.debug)
|
||||||
filetype = filetype.strip()
|
filetype = filetype.strip()
|
||||||
self.log.dbg('\"{}\" filetype: {}'.format(src, filetype))
|
if self.debug:
|
||||||
|
self.log.dbg('\"{}\" filetype: {}'.format(src, filetype))
|
||||||
istext = 'text' in 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:
|
if not istext:
|
||||||
return self._handle_bin_file(src, profile)
|
return self._handle_bin_file(src, profile)
|
||||||
return self._handle_text_file(src, profile)
|
return self._handle_text_file(src, profile)
|
||||||
|
|||||||
Reference in New Issue
Block a user