Remove obsolete Functest utils
[functest.git] / functest / utils / functest_utils.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 from __future__ import print_function
13 import logging
14 import subprocess
15 import sys
16 import yaml
17
18 LOGGER = logging.getLogger(__name__)
19
20
21 def execute_command_raise(cmd, info=False, error_msg="",
22                           verbose=True, output_file=None):
23     ret = execute_command(cmd, info, error_msg, verbose, output_file)
24     if ret != 0:
25         raise Exception(error_msg)
26
27
28 def execute_command(cmd, info=False, error_msg="",
29                     verbose=True, output_file=None):
30     if not error_msg:
31         error_msg = ("The command '%s' failed." % cmd)
32     msg_exec = ("Executing command: '%s'" % cmd)
33     if verbose:
34         if info:
35             LOGGER.info(msg_exec)
36         else:
37             LOGGER.debug(msg_exec)
38     popen = subprocess.Popen(
39         cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
40     if output_file:
41         ofd = open(output_file, "w")
42     for line in iter(popen.stdout.readline, b''):
43         if output_file:
44             ofd.write(line)
45         else:
46             line = line.replace('\n', '')
47             print (line)
48             sys.stdout.flush()
49     if output_file:
50         ofd.close()
51     popen.stdout.close()
52     returncode = popen.wait()
53     if returncode != 0:
54         if verbose:
55             LOGGER.error(error_msg)
56
57     return returncode
58
59
60 def get_parameter_from_yaml(parameter, yfile):
61     """
62     Returns the value of a given parameter in file.yaml
63     parameter must be given in string format with dots
64     Example: general.openstack.image_name
65     """
66     with open(yfile) as yfd:
67         file_yaml = yaml.safe_load(yfd)
68     value = file_yaml
69     for element in parameter.split("."):
70         value = value.get(element)
71         if value is None:
72             raise ValueError("The parameter %s is not defined in"
73                              " %s" % (parameter, yfile))
74     return value