From 1ea1e004e5b77a0a11f56f4dea949bcd77112c13 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 28 Sep 2018 13:51:57 +0200 Subject: [PATCH] adding return code of external command for #60 --- dotdrop/dotdrop.py | 4 ++-- dotdrop/utils.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 6619587..f92b49d 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -279,14 +279,14 @@ def importer(opts, conf, paths): if opts['dry']: LOG.dry('would run: {}'.format(' '.join(cmd))) else: - run(cmd, raw=False, debug=opts['debug']) + run(cmd, raw=False, debug=opts['debug'], checkerr=True) 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']) + run(cmd, raw=False, debug=opts['debug'], checkerr=True) if linkit: remove(dst) os.symlink(srcf, dst) diff --git a/dotdrop/utils.py b/dotdrop/utils.py index 4ad928d..4491427 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -18,15 +18,20 @@ from dotdrop.version import __version__ as VERSION LOG = Logger() -def run(cmd, raw=True, debug=False): +def run(cmd, raw=True, debug=False, checkerr=False): """run a command in the shell (expects a list)""" if debug: LOG.dbg('exec: {}'.format(' '.join(cmd))) p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + p.wait() + out = p.stdout.readlines() + ret = p.returncode + if checkerr and ret != 0: + LOG.warn('cmd \"{}\" returned non zero ({}): {}'.format(ret, out)) if raw: - return p.stdout.readlines() - lines = [x.decode('utf-8', 'replace') for x in p.stdout.readlines()] + return out + lines = [x.decode('utf-8', 'replace') for x in out] return ''.join(lines)