summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dovetail/container.py4
-rw-r--r--dovetail/utils/dovetail_utils.py67
2 files changed, 35 insertions, 36 deletions
diff --git a/dovetail/container.py b/dovetail/container.py
index 62070442..59fc0d8a 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