<commit_before># 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.

import abc
import functools
import sys
import uuid

from oslo.config import cfg

from rally.benchmark import base
from rally.benchmark import utils
from rally import consts
from rally import exceptions
from rally.objects import endpoint
from rally.openstack.common.gettextutils import _
from rally.openstack.common import log as logging
from rally import utils as rutils


LOG = logging.getLogger(__name__)


def _run_scenario_once(args):
    i, cls, method_name, admin, user, kwargs = args

    LOG.info("ITER: %s START" % i)

    # TODO(boris-42): remove context
    scenario = cls(context={},
                   admin_clients=utils.create_openstack_clients(admin),
                   clients=utils.create_openstack_clients(user))

    try:
        scenario_output = None
        with rutils.Timer() as timer:
            scenario_output = getattr(scenario, method_name)(**kwargs)
        error = None
    except Exception as e:
        error = utils.format_exc(e)
        if cfg.CONF.debug:
            LOG.exception(e)
    finally:
        status = "Error %s: %s" % tuple(error[0:2]) if error else "OK"
        LOG.info("ITER: %(i)s END: %(status)s" % {"i": i, "status": status})

        return {"time": timer.duration() - scenario.idle_time(),
                "idle_time": scenario.idle_time(),
                "error": error,
                "scenario_output": scenario_output,
                "atomic_actions_time": scenario.atomic_actions_time()}


class UserGenerator(object):
    """Context class for generating temporary users/tenants for benchmarks."""

    def __init__(self, admin_clients):
        self.users = []
        self.keystone_client = \
            utils.create_openstack_clients(admin_clients)["keystone"]

    def _create_user(self, user_id, tenant_id):
        pattern = "%(tenant_id)s_user_%(uid)d"
        name = pattern % {"tenant_id": tenant_id, "uid": user_id}
        email = <EMAIL>" % name
        return self.keystone_client.users.create(name, "password",
                                                 email, tenant_id)

    def _create_tenant(self, run_id, i):
        pattern = "temp_%(run_id)s_tenant_%(iter)i"
        return self.keystone_client.tenants.create(pattern % {"run_id": run_id,
                                                              "iter": i})

    def create_users_and_tenants(self, tenants, users_per_tenant):
        run_id = str(uuid.uuid4())
        auth_url = self.keystone_client.auth_url
        self.tenants = [self._create_tenant(run_id, i)
                        for i in range(tenants)]

        self.users = []
        endpoints = []
        for tenant in self.tenants:
            for user_id in range(users_per_tenant):
                user = self._create_user(user_id, tenant.id)
                self.users.append(user)
                endpoints.append(endpoint.Endpoint(
                    auth_url, user.name, "password", tenant.name,
                    consts.EndpointPermission.USER))
        return endpoints

    def _delete_users_and_tenants(self):
        for user in self.users:
            try:
                user.delete()
            except Exception:
                LOG.info("Failed to delete user: %s" % user.name)

        for tenant in self.tenants:
            try:
                tenant.delete()
            except Exception:
                LOG.info("Failed to delete tenant: %s" % tenant.name)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self._delete_users_and_tenants()

        if exc_type:
            LOG.debug(_("Failed to generate temporary users."),
                      exc_info=(exc_type, exc_value, exc_traceback))
        else:
            LOG.debug(_("Completed deleting temporary users and tenants."))


class ResourceCleaner(object):
    """Context class for resource cleanup (both admin and non-admin)."""

    def __init__(self, admin=None, users=None):
        self.admin = admin
        self.users = users

    def _cleanup_users_resources(self):
        if not self.users:
            return

        for user in map(utils.create_openstack_clients, self.users):
            methods = [
                functools.partial(utils.delete_nova_resources, user["nova"]),
                functools.partial(utils.delete_glance_resources,
                                  user["glance"], user["keystone"]),
                functools.partial(utils.delete_cinder_resources,
                                  user["cinder"])
            ]

            for method in methods:
                try:
                    method()
                except Exception as e:
                    LOG.debug(_("Not all resources were cleaned."),
                              exc_info=sys.exc_info())
                    LOG.warning(_('Unable to fully cleanup the cloud: \n%s') %<commit_msg>Updates _cleanup_users_resources to use imap instead of map<commit_after># 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.

import abc
import functools
import itertools
import sys
import uuid

from oslo.config import cfg

from rally.benchmark import base
from rally.benchmark import utils
from rally import consts
from rally import exceptions
from rally.objects import endpoint
from rally.openstack.common.gettextutils import _
from rally.openstack.common import log as logging
from rally import utils as rutils


LOG = logging.getLogger(__name__)


def _run_scenario_once(args):
    i, cls, method_name, admin, user, kwargs = args

    LOG.info("ITER: %s START" % i)

    # TODO(boris-42): remove context
    scenario = cls(context={},
                   admin_clients=utils.create_openstack_clients(admin),
                   clients=utils.create_openstack_clients(user))

    try:
        scenario_output = None
        with rutils.Timer() as timer:
            scenario_output = getattr(scenario, method_name)(**kwargs)
        error = None
    except Exception as e:
        error = utils.format_exc(e)
        if cfg.CONF.debug:
            LOG.exception(e)
    finally:
        status = "Error %s: %s" % tuple(error[0:2]) if error else "OK"
        LOG.info("ITER: %(i)s END: %(status)s" % {"i": i, "status": status})

        return {"time": timer.duration() - scenario.idle_time(),
                "idle_time": scenario.idle_time(),
                "error": error,
                "scenario_output": scenario_output,
                "atomic_actions_time": scenario.atomic_actions_time()}


class UserGenerator(object):
    """Context class for generating temporary users/tenants for benchmarks."""

    def __init__(self, admin_clients):
        self.users = []
        self.keystone_client = \
            utils.create_openstack_clients(admin_clients)["keystone"]

    def _create_user(self, user_id, tenant_id):
        pattern = "%(tenant_id)s_user_%(uid)d"
        name = pattern % {"tenant_id": tenant_id, "uid": user_id}
        email = <EMAIL>" % name
        return self.keystone_client.users.create(name, "password",
                                                 email, tenant_id)

    def _create_tenant(self, run_id, i):
        pattern = "temp_%(run_id)s_tenant_%(iter)i"
        return self.keystone_client.tenants.create(pattern % {"run_id": run_id,
                                                              "iter": i})

    def create_users_and_tenants(self, tenants, users_per_tenant):
        run_id = str(uuid.uuid4())
        auth_url = self.keystone_client.auth_url
        self.tenants = [self._create_tenant(run_id, i)
                        for i in range(tenants)]

        self.users = []
        endpoints = []
        for tenant in self.tenants:
            for user_id in range(users_per_tenant):
                user = self._create_user(user_id, tenant.id)
                self.users.append(user)
                endpoints.append(endpoint.Endpoint(
                    auth_url, user.name, "password", tenant.name,
                    consts.EndpointPermission.USER))
        return endpoints

    def _delete_users_and_tenants(self):
        for user in self.users:
            try:
                user.delete()
            except Exception:
                LOG.info("Failed to delete user: %s" % user.name)

        for tenant in self.tenants:
            try:
                tenant.delete()
            except Exception:
                LOG.info("Failed to delete tenant: %s" % tenant.name)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self._delete_users_and_tenants()

        if exc_type:
            LOG.debug(_("Failed to generate temporary users."),
                      exc_info=(exc_type, exc_value, exc_traceback))
        else:
            LOG.debug(_("Completed deleting temporary users and tenants."))


class ResourceCleaner(object):
    """Context class for resource cleanup (both admin and non-admin)."""

    def __init__(self, admin=None, users=None):
        self.admin = admin
        self.users = users

    def _cleanup_users_resources(self):
        if not self.users:
            return

        for user in itertools.imap(utils.create_openstack_clients, self.users):
            methods = [
                functools.partial(utils.delete_nova_resources, user["nova"]),
                functools.partial(utils.delete_glance_resources,
                                  user["glance"], user["keystone"]),
                functools.partial(utils.delete_cinder_resources,
                                  user["cinder"])
            ]

            for method in methods:
                try:
                    method()
                except Exception as e:
                    LOG.debug(_("Not all resources were cleaned."),
                              exc_info=sys.exc_info())
                    LOG.warning(_('Unable to fully cleanup the cloud: \n%s') %
{'fraction_non_alphanumeric': 0.09458972046889089, 'fraction_numerical': 0.002344454463480613, 'mean_word_length': 2.114574557708509, 'pattern_counts': {'":': 22, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6686752', 'n_tokens_mistral': 3099, 'n_tokens_neox': 3014, 'n_words': 827}