Merge "ci in huawei-pod1 do not run test case after tc074"
[yardstick.git] / yardstick / common / utils.py
index d639fb6..3c5895f 100644 (file)
 
 # yardstick comment: this is a modified copy of rally/rally/common/utils.py
 
+from __future__ import absolute_import
+from __future__ import print_function
+
+import errno
+import logging
 import os
+import subprocess
 import sys
+from functools import reduce
+
 import yaml
 from oslo_utils import importutils
+from oslo_serialization import jsonutils
 
 import yardstick
 
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+
 
 # Decorator for cli-args
 def cliargs(*args, **kwargs):
@@ -84,10 +96,51 @@ def get_para_from_yaml(file_path, args):
             value = reduce(func, args.split('.'), value)
 
             if value is None:
-                print 'parameter not found'
+                print('parameter not found')
                 return None
 
             return value
     else:
-        print 'file not exist'
+        print('file not exist')
         return None
+
+
+def makedirs(d):
+    try:
+        os.makedirs(d)
+    except OSError as e:
+        if e.errno != errno.EEXIST:
+            raise
+
+
+def execute_command(cmd):
+    exec_msg = "Executing command: '%s'" % cmd
+    logger.debug(exec_msg)
+
+    output = subprocess.check_output(cmd.split()).split(os.linesep)
+
+    return output
+
+
+def source_env(env_file):
+    p = subprocess.Popen(". %s; env" % env_file, stdout=subprocess.PIPE,
+                         shell=True)
+    output = p.communicate()[0]
+    env = dict((line.split('=', 1) for line in output.splitlines()))
+    os.environ.update(env)
+    return env
+
+
+def read_json_from_file(path):
+    with open(path, 'r') as f:
+        return jsonutils.load(f)
+
+
+def write_json_to_file(path, data, mode='w'):
+    with open(path, mode) as f:
+        jsonutils.dump(data, f)
+
+
+def write_file(path, data, mode='w'):
+    with open(path, mode) as f:
+        f.write(data)