From cb55f6dbfe4867e4d232f51b5a32764f82d2399b Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Sat, 17 Sep 2016 23:15:18 -0400 Subject: add prototype of dovetail tool JIRA: DOVETAIL-12 Change-Id: I1ee120ed8199589e1e7cbce9cbb55036e9e5f7ea Signed-off-by: Leo Wang --- scripts/utils/__init__.py | 0 scripts/utils/dovetail_logger.py | 55 +++++++++++++++++++++++++++++++++++++++ scripts/utils/dovetail_utils.py | 56 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 scripts/utils/__init__.py create mode 100644 scripts/utils/dovetail_logger.py create mode 100644 scripts/utils/dovetail_utils.py (limited to 'scripts/utils') diff --git a/scripts/utils/__init__.py b/scripts/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/scripts/utils/dovetail_logger.py b/scripts/utils/dovetail_logger.py new file mode 100644 index 00000000..7335c0ae --- /dev/null +++ b/scripts/utils/dovetail_logger.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Logging levels: +# Level Numeric value +# CRITICAL 50 +# ERROR 40 +# WARNING 30 +# INFO 20 +# DEBUG 10 +# NOTSET 0 +# +# Usage: +# import dovetail_logger as dl +# logger = dl.Logger("script_name").getLogger() +# logger.info("message to be shown with - INFO - ") +# logger.debug("message to be shown with - DEBUG -") + +import logging +import os + +class Logger: + def __init__(self, logger_name): + + CI_DEBUG = os.getenv('CI_DEBUG') + + self.logger = logging.getLogger(logger_name) + self.logger.propagate = 0 + self.logger.setLevel(logging.DEBUG) + + ch = logging.StreamHandler() + formatter = logging.Formatter('%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') + ch.setFormatter(formatter) + if CI_DEBUG is not None and CI_DEBUG.lower() == "true": + ch.setLevel(logging.DEBUG) + else: + ch.setLevel(logging.INFO) + self.logger.addHandler(ch) + + if not os.path.exists('/home/opnfv/dovetail/results/'): + os.makedirs('/home/opnfv/dovetail/results/') + hdlr = logging.FileHandler('/home/opnfv/dovetail/results/dovetail.log') + hdlr.setFormatter(formatter) + hdlr.setLevel(logging.DEBUG) + self.logger.addHandler(hdlr) + + def getLogger(self): + return self.logger + diff --git a/scripts/utils/dovetail_utils.py b/scripts/utils/dovetail_utils.py new file mode 100644 index 00000000..f79c6fc4 --- /dev/null +++ b/scripts/utils/dovetail_utils.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# valentin.boucher@orange.com +# grakiss.wanglei@huawei.com +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# + +import sys +import subprocess + +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) + 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) + 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) + if exit_on_error: + sys.exit(1) + + return returncode, output[0].strip() + -- cgit 1.2.3-korg