modify for better error detection since some programs write to stderr for non-error output

This commit is contained in:
brent s 2017-10-26 02:04:08 -04:00
parent 5ab91b01f7
commit ae118ee9ed

View File

@ -103,13 +103,17 @@ class Backup(object):
if self.args['dryrun']: if self.args['dryrun']:
return() # no-op return() # no-op
if stdoutfh: if stdoutfh:
_err = subprocess.run(cmd, stdout = stdoutfh, stderr = subprocess.PIPE).stderr.decode('utf-8').strip() _cmd = subprocess.run(cmd, stdout = stdoutfh, stderr = subprocess.PIPE)
else: else:
with open(os.devnull, 'w') as DEVNULL: _cmd = subprocess.run(cmd,
_err = subprocess.run(cmd, stdout = DEVNULL, stderr = subprocess.PIPE).stderr.decode('utf-8').strip() stdout = subprocess.PIPE,
if _err != '': stderr = subprocess.PIPE)
self.logger.error('STDERR: {0} ({1})'.format(_err.stderr.decode('utf-8'), _out = _cmd.stdout.decode('utf-8').strip()
' '.join(cmd))) _err = _cmd.stderr.decode('utf-8').strip()
_returncode = _cmd.returncode
if _returncode != 0:
self.logger.error('STDERR: ({1})\n{0}'.format(_err,
' '.join(cmd)))
return() return()
def createRepo(self): def createRepo(self):
@ -351,11 +355,12 @@ class Backup(object):
stderr = subprocess.PIPE) stderr = subprocess.PIPE)
_stdout = [i.strip() for i in _out.stdout.decode('utf-8').splitlines()] _stdout = [i.strip() for i in _out.stdout.decode('utf-8').splitlines()]
_stderr = _out.stderr.decode('utf-8').strip() _stderr = _out.stderr.decode('utf-8').strip()
_returncode = _out.returncode
output[r] = _stdout output[r] = _stdout
self.logger.debug('[{0}]: (RESULT) {1}'.format(r, self.logger.debug('[{0}]: (RESULT) {1}'.format(r,
'\n'.join(_stdout))) '\n'.join(_stdout)))
if _stderr != '': if _returncode != 0:
self.logger.error('[{0}]: STDERR: {1} ({2})'.format(r, self.logger.error('[{0}]: STDERR: ({2}) ({1})'.format(r,
_stderr, _stderr,
' '.join(_cmd))) ' '.join(_cmd)))
del(_env['BORG_PASSPHRASE']) del(_env['BORG_PASSPHRASE'])