text: # Copyright 2013: Mirantis Inc.
  # All Rights Reserved.
  #
  #    Licensed under the Apache License, Version 2.0 (the "License"); you may
  #    not use this file except in compliance with the License. You may obtain
  #    a copy of the License at
  #
  #         http://www.apache.org/licenses/LICENSE-2.0
  #
  #    Unless required by applicable law or agreed to in writing, software
  #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  #    License for the specific language governing permissions and limitations
  #    under the License.
  
  # 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 keystoneauth1 import identity
  from keystoneauth1 import session
  from neutronclient.v2_0 import client
  from oslo_utils import importutils
  
  import yardstick
  
  logger = logging.getLogger(__name__)
  logger.setLevel(logging.DEBUG)
  
  
  # Decorator for cli-args
  def cliargs(*args, **kwargs):
      def _decorator(func):
          func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
          return func
      return _decorator
  
  
  def itersubclasses(cls, _seen=None):
      """Generator over all subclasses of a given class in depth first order."""
  
      if not isinstance(cls, type):
          raise TypeError("itersubclasses must be called with "
                          "new-style classes, not %.100r" % cls)
      _seen = _seen or set()
      try:
          subs = cls.__subclasses__()
      except TypeError:   # fails only when cls is type
          subs = cls.__subclasses__(cls)
      for sub in subs:
          if sub not in _seen:
              _seen.add(sub)
              yield sub
              for sub in itersubclasses(sub, _seen):
                  yield sub
  
  
  def try_append_module(name, modules):
      if name not in modules:
          modules[name] = importutils.import_module(name)
  
  
  def import_modules_from_package(package):
      """Import modules from package and append into sys.modules
  
      :param: package - Full package name. For example: rally.deploy.engines
      """
      path = [os.path.dirname(yardstick.__file__), ".."] + package.split(".")
      path = os.path.join(*path)
      for root, dirs, files in os.walk(path):
          for filename in files:
              if filename.startswith("__") or not filename.endswith(".py"):
                  continue
              new_package = ".".join(root.split(os.sep)).split("....")[1]
              module_name = "%s.%s" % (new_package, filename[:-3])
              try_append_module(module_name, sys.modules)
  
  
  def get_para_from_yaml(file_path, args):
  
      def func(a, b):
          if a is None:
              return None
          return a.get(b)
  
      if os.path.exists(file_path):
          with open(file_path) as f:
              value = yaml.safe_load(f)
              value = reduce(func, args.split('.'), value)
  
              if value is None:
                  print('parameter not found')
                  return None
  
              return value
      else:
          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 get_openstack_session():
      auth = identity.Password(auth_url=os.environ.get('OS_AUTH_URL'),
                               username=os.environ.get('OS_USERNAME'),
                               password=os.environ.get('OS_PASSWORD'),
                               tenant_name=os.environ.get('OS_TENANT_NAME'))
      return session.Session(auth=auth)
  
  
  def get_neutron_client():
      sess = get_openstack_session()
      neutron_client = client.Client(session=sess)
      return neutron_client
  