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

improve error check on external commands

This commit is contained in:
deadc0de6
2018-09-28 14:08:33 +02:00
parent 1ea1e004e5
commit 8aabf57204
3 changed files with 22 additions and 12 deletions

View File

@@ -254,7 +254,7 @@ def importer(opts, conf, paths):
cnt = 0
for path in paths:
if not os.path.lexists(path):
LOG.err('\"{}\" does not exist, ignored !'.format(path))
LOG.err('\"{}\" does not exist, ignored!'.format(path))
continue
dst = path.rstrip(os.sep)
dst = os.path.abspath(dst)
@@ -269,8 +269,6 @@ def importer(opts, conf, paths):
# create a new dotfile
dotfile = Dotfile('', dst, src)
linkit = opts['link'] or opts['link_by_default']
retconf, new_dotfile = conf.new(dotfile, opts['profile'], linkit)
dotfile = new_dotfile
# prepare hierarchy for dotfile
srcf = os.path.join(CUR, opts['dotpath'], src)
@@ -279,17 +277,24 @@ def importer(opts, conf, paths):
if opts['dry']:
LOG.dry('would run: {}'.format(' '.join(cmd)))
else:
run(cmd, raw=False, debug=opts['debug'], checkerr=True)
r, _ = run(cmd, raw=False, debug=opts['debug'], checkerr=True)
if not r:
LOG.err('importing \"{}\" failed!'.format(path))
continue
cmd = ['cp', '-R', '-L', dst, srcf]
if opts['dry']:
LOG.dry('would run: {}'.format(' '.join(cmd)))
if linkit:
LOG.dry('would symlink {} to {}'.format(srcf, dst))
else:
run(cmd, raw=False, debug=opts['debug'], checkerr=True)
r, _ = run(cmd, raw=False, debug=opts['debug'], checkerr=True)
if not r:
LOG.err('importing \"{}\" failed!'.format(path))
continue
if linkit:
remove(dst)
os.symlink(srcf, dst)
retconf, dotfile = conf.new(dotfile, opts['profile'], linkit)
if retconf:
LOG.sub('\"{}\" imported'.format(path))
cnt += 1

View File

@@ -52,7 +52,8 @@ class Templategen:
def _handle_file(self, src):
"""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()
if self.debug:
self.log.dbg('\"{}\" filetype: {}'.format(src, filetype))

View File

@@ -25,20 +25,24 @@ def run(cmd, raw=True, debug=False, checkerr=False):
p = subprocess.Popen(cmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
out = p.stdout.readlines()
ret = p.returncode
out = p.stdout.readlines()
lines = ''.join([x.decode('utf-8', 'replace') for x in out])
if checkerr and ret != 0:
LOG.warn('cmd \"{}\" returned non zero ({}): {}'.format(ret, out))
c = ' '.join(cmd)
errl = lines.rstrip()
m = '\"{}\" returned non zero ({}): {}'.format(c, ret, errl)
LOG.err(m)
if raw:
return out
lines = [x.decode('utf-8', 'replace') for x in out]
return ''.join(lines)
return ret == 0, out
return ret == 0, lines
def diff(src, dst, raw=True, opts='', debug=False):
"""call unix diff to compare two files"""
cmd = 'diff -r {} \"{}\" \"{}\"'.format(opts, src, dst)
return run(shlex.split(cmd), raw=raw, debug=debug)
_, out = run(shlex.split(cmd), raw=raw, debug=debug)
return out
def get_tmpdir():