From ce17214c0717999ba08cd8f094499f2e4efb0712 Mon Sep 17 00:00:00 2001 From: "Ryan.RCS" Date: Fri, 16 Dec 2016 10:06:19 +0000 Subject: updated the process flow of function exec_cmd separate the log expressions to other functions removed statements that not necessary output error detail info in exception Change-Id: I721611d61dee83eb5f40ee954d433073114e231a JIRA: DOVETAIL-161 Signed-off-by: Ryan.RCS --- dovetail/container.py | 4 +-- dovetail/utils/dovetail_utils.py | 67 ++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/dovetail/container.py b/dovetail/container.py index 87174727..15e78818 100644 --- a/dovetail/container.py +++ b/dovetail/container.py @@ -20,10 +20,10 @@ class Container: logger = None - def __init__(cls): + def __init__(self): pass - def __str__(cls): + def __str__(self): pass @classmethod diff --git a/dovetail/utils/dovetail_utils.py b/dovetail/utils/dovetail_utils.py index 2d5f0dd1..1c68b7f3 100644 --- a/dovetail/utils/dovetail_utils.py +++ b/dovetail/utils/dovetail_utils.py @@ -15,23 +15,34 @@ import subprocess from collections import Mapping, Set, Sequence +def exec_log(verbose, logger, msg, level, flush=False): + if not verbose: + return + + if logger: + if level == 'info': + logger.info(msg) + elif level == 'error': + logger.error(msg) + elif level == 'debug': + logger.debug(msg) + else: + pass + else: + print(msg) + if flush: + sys.stdout.flush() + + def exec_cmd(cmd, logger=None, exit_on_error=True, info=False, - error_msg="", verbose=True): - if not error_msg: - error_msg = ("The command '%s' failed." % cmd) + err_msg="", verbose=True): + msg_err = ("The command '%s' failed." % cmd) if not err_msg else err_msg msg_exec = ("Executing command: '%s'" % cmd) - if verbose: - if logger: - if info: - logger.info(msg_exec) - else: - logger.debug(msg_exec) - else: - print(msg_exec) - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) + level = 'info' if info else 'debug' + exec_log(verbose, logger, msg_exec, level) - # show progress bar + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) seconds = 0 while p.poll() is None: seconds += 1 @@ -39,29 +50,17 @@ def exec_cmd(cmd, logger=None, exit_on_error=True, info=False, show_progress_bar(seconds) time.sleep(1) - output = p.communicate() - for line in output[0].strip().split('\n'): - line = line.replace('\n', '') - if logger: - if info: - logger.info(line) - else: - logger.debug(line) - else: - print (line) - sys.stdout.flush() - - returncode = p.returncode - if returncode != 0: - if verbose: - if logger: - logger.error(error_msg) - else: - print(error_msg) + (stdout, stderr) = p.communicate() + if p.returncode == 0: + for line in stdout.strip().splitlines(): + exec_log(verbose, logger, line, level, True) + else: + exec_log(verbose, logger, stderr, 'error') + exec_log(verbose, logger, msg_err, 'error') if exit_on_error: sys.exit(1) - return returncode, output[0].strip() + return p.returncode, stdout.strip() # walkthrough the object, yield path and value -- cgit 1.2.3-korg