<commit_before>import os
import re
import glob
import ssl
import time
import json

from xml.etree import ElementTree

from setup_app import paths
from setup_app.static import AppType, InstallOption, SetupProfiles
from setup_app.utils import base
from setup_app.config import Config
from setup_app.utils.properties_utils import propertiesUtils
from setup_app.installers.jetty import JettyInstaller

class CasaInstaller(JettyInstaller):

    def __init__(self):
        setattr(base.current_app, self.__class__.__name__, self)
        self.service_name = 'casa'
        self.app_type = AppType.SERVICE
        self.install_type = InstallOption.OPTONAL
        self.install_var = 'installCasa'
        self.register_progess()

        self.source_files = [
                (os.path.join(Config.distGluuFolder, 'casa.war'), Config.maven_root + '/maven/org/gluu/casa/{0}/casa-{0}.war'.format(Config.oxVersion))
                ]

        self.templates_folder = os.path.join(Config.templateFolder, 'casa')
        self.output_folder = os.path.join(Config.outputFolder, 'casa')
        self.ldif = os.path.join(Config.outputFolder, 'casa/casa.ldif')
        self.ldif_scripts = os.path.join(Config.outputFolder, 'casa/scripts.ldif')
        self.smtp_configuration_json = os.path.join(Config.outputFolder, 'casa/smtp_configuration.json')
        self.pylib_folder = os.path.join(Config.gluuOptPythonFolder, 'libs')
        self.casa_jetty_dir = os.path.join(self.jetty_base, 'casa')
        self.casa_jks_fn = os.path.join(Config.certFolder, self.get_keystore_fn('casa-keys'))
        self.casa_alias = 'casa_sig_ec256'

    def install(self):

        if not os.path.exists(self.pylib_folder):
            self.run([paths.cmd_mkdir , '-p', self.pylib_folder])

        self.run([paths.cmd_chmod , 'g+w', self.pylib_folder])
        self.logIt("Copying casa.war into jetty webapps folder...")
        self.installJettyService(self.jetty_app_configuration['casa'])

        jettyServiceWebapps = os.path.join(self.casa_jetty_dir, 'webapps')
        self.copyFile(self.source_files[0][0], jettyServiceWebapps)
        self.war_for_jetty10(os.path.join(jettyServiceWebapps, os.path.basename(self.source_files[0][0])))
        jettyServiceOxAuthCustomLibsPath = os.path.join(self.jetty_base,
                                                        "oxauth",
                                                        "custom/libs"
                                                        )

        twillo_package = base.determine_package(os.path.join(Config.distGluuFolder, 'twilio-*.jar'))
        self.copyFile(twillo_package, jettyServiceOxAuthCustomLibsPath)

        jsmpp_package = base.determine_package(os.path.join(Config.distGluuFolder, 'jsmpp-*.jar'))
        self.copyFile(jsmpp_package, jettyServiceOxAuthCustomLibsPath)

        if Config.profile == SetupProfiles.DISA_STIG:
            lib_user_group = '{}:{}'.format(base.current_app.OxauthInstaller.service_name.lower(), Config.gluu_group)
        else:
            lib_user_group = '{}:{}'.format(Config.jetty_user, Config.gluu_group)

        self.run([paths.cmd_chown, '-R', lib_user_group, jettyServiceOxAuthCustomLibsPath])

        if not base.argsp.dummy:

            #Adding twilio jar path to oxauth.xml
            oxauth_xml_fn = os.path.join(self.jetty_base,  'oxauth/webapps/oxauth.xml')

            extra_classpath_list = [
                        './custom/libs/{}'.format(os.path.basename(twillo_package)),
                        './custom/libs/{}'.format(os.path.basename(jsmpp_package)),
                        ]

            self.add_extra_class(','.join(extra_classpath_list), oxauth_xml_fn)

        self.enable('casa')

    def copy_static(self):

        for script_fn in glob.glob(os.path.join(Config.staticFolder, 'casa/scripts/*.*')):
            self.run(['cp', script_fn, self.pylib_folder])

    def render_import_templates(self, import_script=True):

        Config.templateRenderingDict['oxd_protocol'] = 'https'
        scripts_template = os.path.join(self.templates_folder, os.path.basename(self.ldif_scripts))
        extensions = base.find_script_names(scripts_template)
        self.prepare_base64_extension_scripts(extensions=extensions)

        ldif_files = (self.ldif, self.ldif_scripts)
        for tmp in ldif_files:
            self.renderTemplateInOut(tmp, self.templates_folder, self.output_folder)

        self.renderTemplateInOut(self.smtp_configuration_json, self.templates_folder, self.output_folder)

        if import_script:
            self.dbUtils.import_ldif(ldif_files)

    def create_folders(self):
        for path_name in ('static', 'plugins'):
            path = os.path.join(self.casa_jetty_dir, path_name)
            if not os.path.exists(path):
                self.run([paths.cmd_mkdir, '-p', path])
            self.run([paths.cmd_chown, '-R', 'jetty:jetty', path])

    def generate_configuration(self):
        if not Config.get('casa_jks_pass'):
            Config.casa_jks_pass = self.getPW()

        self.logIt("Generating casa keys", pbar=self.service_name)

        self.run([Config.cmd_keytool, '-genkeypair',
                    '-alias', self.casa_alias,
                    '-keyalg', 'ec',
                    '-groupname', 'secp256r1',
                    '-sigalg', 'SHA256withECDSA',
                    '-validity', '365',
                    '-storetype', Config.default_store_type,
                    '-keystore', self.casa_jks_fn,
                    '-storepass', Config.casa_jks_pass,
                    '-dname', 'CN=CASA CA Certificate'
                ])

    def update_backend(self):
        smtp_configuration = base.readJsonFile(self.smtp_configuration_json)
        self.dbUtils.set_configuration('oxSmtpConfiguration', json.dumps(smtp_configuration, indent=2).encode('utf-8'))
<commit_msg>fix: unnecessary .encode('utf-8') has been removed;<commit_after>import os
import re
import glob
import ssl
import time
import json

from xml.etree import ElementTree

from setup_app import paths
from setup_app.static import AppType, InstallOption, SetupProfiles
from setup_app.utils import base
from setup_app.config import Config
from setup_app.utils.properties_utils import propertiesUtils
from setup_app.installers.jetty import JettyInstaller

class CasaInstaller(JettyInstaller):

    def __init__(self):
        setattr(base.current_app, self.__class__.__name__, self)
        self.service_name = 'casa'
        self.app_type = AppType.SERVICE
        self.install_type = InstallOption.OPTONAL
        self.install_var = 'installCasa'
        self.register_progess()

        self.source_files = [
                (os.path.join(Config.distGluuFolder, 'casa.war'), Config.maven_root + '/maven/org/gluu/casa/{0}/casa-{0}.war'.format(Config.oxVersion))
                ]

        self.templates_folder = os.path.join(Config.templateFolder, 'casa')
        self.output_folder = os.path.join(Config.outputFolder, 'casa')
        self.ldif = os.path.join(Config.outputFolder, 'casa/casa.ldif')
        self.ldif_scripts = os.path.join(Config.outputFolder, 'casa/scripts.ldif')
        self.smtp_configuration_json = os.path.join(Config.outputFolder, 'casa/smtp_configuration.json')
        self.pylib_folder = os.path.join(Config.gluuOptPythonFolder, 'libs')
        self.casa_jetty_dir = os.path.join(self.jetty_base, 'casa')
        self.casa_jks_fn = os.path.join(Config.certFolder, self.get_keystore_fn('casa-keys'))
        self.casa_alias = 'casa_sig_ec256'

    def install(self):

        if not os.path.exists(self.pylib_folder):
            self.run([paths.cmd_mkdir , '-p', self.pylib_folder])

        self.run([paths.cmd_chmod , 'g+w', self.pylib_folder])
        self.logIt("Copying casa.war into jetty webapps folder...")
        self.installJettyService(self.jetty_app_configuration['casa'])

        jettyServiceWebapps = os.path.join(self.casa_jetty_dir, 'webapps')
        self.copyFile(self.source_files[0][0], jettyServiceWebapps)
        self.war_for_jetty10(os.path.join(jettyServiceWebapps, os.path.basename(self.source_files[0][0])))
        jettyServiceOxAuthCustomLibsPath = os.path.join(self.jetty_base,
                                                        "oxauth",
                                                        "custom/libs"
                                                        )

        twillo_package = base.determine_package(os.path.join(Config.distGluuFolder, 'twilio-*.jar'))
        self.copyFile(twillo_package, jettyServiceOxAuthCustomLibsPath)

        jsmpp_package = base.determine_package(os.path.join(Config.distGluuFolder, 'jsmpp-*.jar'))
        self.copyFile(jsmpp_package, jettyServiceOxAuthCustomLibsPath)

        if Config.profile == SetupProfiles.DISA_STIG:
            lib_user_group = '{}:{}'.format(base.current_app.OxauthInstaller.service_name.lower(), Config.gluu_group)
        else:
            lib_user_group = '{}:{}'.format(Config.jetty_user, Config.gluu_group)

        self.run([paths.cmd_chown, '-R', lib_user_group, jettyServiceOxAuthCustomLibsPath])

        if not base.argsp.dummy:

            #Adding twilio jar path to oxauth.xml
            oxauth_xml_fn = os.path.join(self.jetty_base,  'oxauth/webapps/oxauth.xml')

            extra_classpath_list = [
                        './custom/libs/{}'.format(os.path.basename(twillo_package)),
                        './custom/libs/{}'.format(os.path.basename(jsmpp_package)),
                        ]

            self.add_extra_class(','.join(extra_classpath_list), oxauth_xml_fn)

        self.enable('casa')

    def copy_static(self):

        for script_fn in glob.glob(os.path.join(Config.staticFolder, 'casa/scripts/*.*')):
            self.run(['cp', script_fn, self.pylib_folder])

    def render_import_templates(self, import_script=True):

        Config.templateRenderingDict['oxd_protocol'] = 'https'
        scripts_template = os.path.join(self.templates_folder, os.path.basename(self.ldif_scripts))
        extensions = base.find_script_names(scripts_template)
        self.prepare_base64_extension_scripts(extensions=extensions)

        ldif_files = (self.ldif, self.ldif_scripts)
        for tmp in ldif_files:
            self.renderTemplateInOut(tmp, self.templates_folder, self.output_folder)

        self.renderTemplateInOut(self.smtp_configuration_json, self.templates_folder, self.output_folder)

        if import_script:
            self.dbUtils.import_ldif(ldif_files)

    def create_folders(self):
        for path_name in ('static', 'plugins'):
            path = os.path.join(self.casa_jetty_dir, path_name)
            if not os.path.exists(path):
                self.run([paths.cmd_mkdir, '-p', path])
            self.run([paths.cmd_chown, '-R', 'jetty:jetty', path])

    def generate_configuration(self):
        if not Config.get('casa_jks_pass'):
            Config.casa_jks_pass = self.getPW()

        self.logIt("Generating casa keys", pbar=self.service_name)

        self.run([Config.cmd_keytool, '-genkeypair',
                    '-alias', self.casa_alias,
                    '-keyalg', 'ec',
                    '-groupname', 'secp256r1',
                    '-sigalg', 'SHA256withECDSA',
                    '-validity', '365',
                    '-storetype', Config.default_store_type,
                    '-keystore', self.casa_jks_fn,
                    '-storepass', Config.casa_jks_pass,
                    '-dname', 'CN=CASA CA Certificate'
                ])

    def update_backend(self):
        smtp_configuration = base.readJsonFile(self.smtp_configuration_json)
        self.dbUtils.set_configuration('oxSmtpConfiguration', json.dumps(smtp_configuration, indent=2))
<|endoftext|>


<commit_before>import re

from toposort import toposort_flatten

from .cloudformation import Cloudformation
from .utils import template, load_file, LazyTemplateMapping
from .deploy import Deploy

class Stack(object):
    def __init__(self, name, template, parameters=None,
                 dependencies=None, repo_url=None):
        self.name = name
        self.repo_url = repo_url
        self.template = template
        self.parameters = parameters or {}
        self.dependencies = dependencies or []

    def build(self, *args, **kwargs):
        return BuiltStack(self, *args, **kwargs)

class BuiltStack(Stack):
    def __init__(self, stack, stage, environment, variables):
        self.stage = stage
        self.environment = environment

        self.name = self._build_name(stack.name)

        self.parameters = LazyTemplateMapping(
            stack.parameters,
            variables,
            stage=self.stage,
            environment=self.environment)

        self.template = stack.template
        self.dependencies = stack.dependencies

        self._template_body = None

        self.status = None
        self.outputs = None
        self.resources = None

    @property
    def template_body(self):
        if self._template_body is None:
            self._template_body = self._load_template()

        return self._template_body

    def _build_name(self, name):
        return template(name, {},
                        stage=self.stage, environment=self.environment)

    def _load_template(self):
        template_body = load_file(self.template)

        environment_variables = [
            p for p in self.parameters.keys() if p.startswith('EnvVar')
        ]

        def param_to_env(name):
            s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
            s2 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1)
            return s2.upper().replace('ENV_VAR_', '')

        return template(template_body, {
            "environment_variables": environment_variables,
            "param_to_env": param_to_env,
        })

    def update_info(self, stack_info):
        for attr in ['status', 'outputs', 'resources']:
            setattr(self, attr, stack_info[attr])

class StackPlan(object):
    def __init__(self, stacks, stage, environment, variables, apps,
                 logger=None, profile_name=None):
        self.log = logger or (lambda *args, **kwargs: None)
        self.profile_name = profile_name
        self.cfn = Cloudformation(variables['aws_region'], logger=self.log,
                                  profile_name=profile_name)

        self.stage = stage
        self.environment = environment
        self.apps = sorted(flatten_stack_groups(stacks, apps))
        self._stacks = stacks

        self.stack_context = variables.copy()
        if 'stacks' in variables:
            raise ValueError("'stacks' is a reserved variable name")
        self.stack_context['stacks'] = {}

    def stacks(self, apps=None, with_dependencies=False):
        apps = sorted(flatten_stack_groups(self._stacks, apps or self.apps))
        return get_stacks(self._stacks, apps or self.apps, with_dependencies)

    def dependant_stacks(self):
        return get_stacks(
            self._stacks,
            get_dependants(self._stacks, self.apps),
        )

    def build_stack(self, stack):
        return stack.build(self.stage, self.environment, self.stack_context)

    def get_value(self, path):
        vars_dict = self.stack_context
        for key in path.split('.'):
            try:
                vars_dict = vars_dict[key]
            except TypeError:
                vars_dict = getattr(vars_dict, key)
        return vars_dict

    def info(self, apps=None, with_aws=True):
        stacks = self.stacks(apps, with_dependencies=True)
        self.log('Gathering info about %s stacks',
                 ', '.join(s[0] for s in stacks))

        for name, stack in stacks:
            built_stack = self.build_stack(stack)
            self.stack_context['stacks'][name] = built_stack

            if with_aws:
                stack_info = self.cfn.describe_stack(built_stack)
                self.stack_context['stacks'][name].update_info(stack_info)

        return self.stack_context['stacks']

    def create(self, create_dependencies=True):
        self.log('Creating %s', ', '.join(self.apps))

        stacks = self.stacks(with_dependencies=True)
        self.log('Will run %s stacks', ', '.join(s[0] for s in stacks))

        for name, stack in stacks:
            built_stack = self.build_stack(stack)
            self.stack_context['stacks'][name] = built_stack

            if create_dependencies or name in self.apps:
                stack_info = self.cfn.create_stack(built_stack)
            else:
                stack_info = self.cfn.describe_stack(built_stack)
                if not stack_info:
                    self.log("Dependency %s doesn't exists, can't continue",
                             built_stack.name)
                    return False

            self.stack_context['stacks'][name].update_info(stack_info)

        return True

    def delete(self):
        self.log('Deleting %s', ', '.join(self.apps))

        stacks = self.dependant_stacks()
        self.log('Will check %s stacks', ', '.join(s[0] for s in stacks))

        for name, stack in stacks:
            built_stack = self.build_stack(stack)
            status = self.cfn.describe_stack(built_stack)
            if name in self.apps:
                self.cfn.delete_stack(built_stack)
            elif status and name not in self.apps:
                self.log("Dependant stack %s exists, can't continue",
                         built_stack.name)
                return False

        return True

    def get_deploy(self, repository_path=None):
        if len(self.apps) != 1:
            raise StandardError("Can only deploy a single app at a time")
        stack_info = self.info(with_aws=False)[self.apps[0]]

        return Deploy(
            stack_info.parameters['ApplicationName'],
            stack_info.parameters['EnvironmentName'],
            repository_path,
            region=self.stack_context['aws_region'],
            logger=self.log,
            profile_name=self.profile_name,
        )

    @classmethod
    def from_ctx(cls, ctx, **kwargs):
        return cls(
            stacks=kwargs.get('stacks', ctx.stacks),
            stage=kwargs.get('stage', ctx.stage),
            environment=kwargs.get('environment', ctx.environment),
            variables=kwargs.get('variables', ctx.variables),
            apps=kwargs.get('apps', ctx.apps),
            logger=kwargs.get('logger', ctx.log),
            profile_name=kwargs.get('profile_name'),
        )

def get_stacks(stacks, names, with_dependencies=False):
    if with_dependencies:
        names = get_dependencies(stacks, names)

    return [(name, stacks[name]) for name in names]

def flatten_stack_groups(stacks, names):
    names = set(names)
    for name in names.copy():
        if isinstance(stacks[name], list):
            names.remove(name)
            names = names.union(flatten_stack_groups(stacks, stacks[name]))

    return names

def get_dependencies(stacks, names):
    return toposort_flatten(_get_dependencies(stacks, names))

def get_dependants(stacks, names):
    return toposort_flatten(_get_dependants(stacks, names))

def _get_dependencies(stacks, names):
    deps = {}
    for name in names:
        dependencies = stacks[name].dependencies
        deps[name] = set(dependencies)
        for dep in dependencies:
            deps.update(_get_dependencies(stacks, [dep]))

    return deps

def _get_dependants(stacks, names):
    reversed_deps = _reversed_deps(stacks, names)

    return dict((name, set(reversed_deps.get(name, []))) for name in names)

def _reversed_deps(stacks, names):
    reversed_deps = {}
    for stack in filter(lambda s: isinstance(stacks[s], Stack), stacks):
        for dep in stacks[stack].dependencies:
            if dep in reversed_deps:
                reversed_deps[dep].append(stack)
            else:
                reversed_deps[dep] = [stack]

    return reversed_deps
<commit_msg>Print a message when .info encounters a missing stack<commit_after>import re

from toposort import toposort_flatten

from .cloudformation import Cloudformation
from .utils import template, load_file, LazyTemplateMapping
from .deploy import Deploy

class Stack(object):
    def __init__(self, name, template, parameters=None,
                 dependencies=None, repo_url=None):
        self.name = name
        self.repo_url = repo_url
        self.template = template
        self.parameters = parameters or {}
        self.dependencies = dependencies or []

    def build(self, *args, **kwargs):
        return BuiltStack(self, *args, **kwargs)

class BuiltStack(Stack):
    def __init__(self, stack, stage, environment, variables):
        self.stage = stage
        self.environment = environment

        self.name = self._build_name(stack.name)

        self.parameters = LazyTemplateMapping(
            stack.parameters,
            variables,
            stage=self.stage,
            environment=self.environment)

        self.template = stack.template
        self.dependencies = stack.dependencies

        self._template_body = None

        self.status = None
        self.outputs = None
        self.resources = None

    @property
    def template_body(self):
        if self._template_body is None:
            self._template_body = self._load_template()

        return self._template_body

    def _build_name(self, name):
        return template(name, {},
                        stage=self.stage, environment=self.environment)

    def _load_template(self):
        template_body = load_file(self.template)

        environment_variables = [
            p for p in self.parameters.keys() if p.startswith('EnvVar')
        ]

        def param_to_env(name):
            s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
            s2 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1)
            return s2.upper().replace('ENV_VAR_', '')

        return template(template_body, {
            "environment_variables": environment_variables,
            "param_to_env": param_to_env,
        })

    def update_info(self, stack_info):
        for attr in ['status', 'outputs', 'resources']:
            setattr(self, attr, stack_info[attr])

class StackPlan(object):
    def __init__(self, stacks, stage, environment, variables, apps,
                 logger=None, profile_name=None):
        self.log = logger or (lambda *args, **kwargs: None)
        self.profile_name = profile_name
        self.cfn = Cloudformation(variables['aws_region'], logger=self.log,
                                  profile_name=profile_name)

        self.stage = stage
        self.environment = environment
        self.apps = sorted(flatten_stack_groups(stacks, apps))
        self._stacks = stacks

        self.stack_context = variables.copy()
        if 'stacks' in variables:
            raise ValueError("'stacks' is a reserved variable name")
        self.stack_context['stacks'] = {}

    def stacks(self, apps=None, with_dependencies=False):
        apps = sorted(flatten_stack_groups(self._stacks, apps or self.apps))
        return get_stacks(self._stacks, apps or self.apps, with_dependencies)

    def dependant_stacks(self):
        return get_stacks(
            self._stacks,
            get_dependants(self._stacks, self.apps),
        )

    def build_stack(self, stack):
        return stack.build(self.stage, self.environment, self.stack_context)

    def get_value(self, path):
        vars_dict = self.stack_context
        for key in path.split('.'):
            try:
                vars_dict = vars_dict[key]
            except TypeError:
                vars_dict = getattr(vars_dict, key)
        return vars_dict

    def info(self, apps=None, with_aws=True):
        stacks = self.stacks(apps, with_dependencies=True)
        self.log('Gathering info about %s stacks',
                 ', '.join(s[0] for s in stacks))

        for name, stack in stacks:
            built_stack = self.build_stack(stack)
            self.stack_context['stacks'][name] = built_stack

            if with_aws:
                stack_info = self.cfn.describe_stack(built_stack)
                if stack_info:
                    self.stack_context['stacks'][name].update_info(stack_info)
                else:
                    self.log('Stack [%s] does not exist', name)

        return self.stack_context['stacks']

    def create(self, create_dependencies=True):
        self.log('Creating %s', ', '.join(self.apps))

        stacks = self.stacks(with_dependencies=True)
        self.log('Will run %s stacks', ', '.join(s[0] for s in stacks))

        for name, stack in stacks:
            built_stack = self.build_stack(stack)
            self.stack_context['stacks'][name] = built_stack

            if create_dependencies or name in self.apps:
                stack_info = self.cfn.create_stack(built_stack)
            else:
                stack_info = self.cfn.describe_stack(built_stack)
                if not stack_info:
                    self.log("Dependency %s doesn't exists, can't continue",
                             built_stack.name)
                    return False

            self.stack_context['stacks'][name].update_info(stack_info)

        return True

    def delete(self):
        self.log('Deleting %s', ', '.join(self.apps))

        stacks = self.dependant_stacks()
        self.log('Will check %s stacks', ', '.join(s[0] for s in stacks))

        for name, stack in stacks:
            built_stack = self.build_stack(stack)
            status = self.cfn.describe_stack(built_stack)
            if name in self.apps:
                self.cfn.delete_stack(built_stack)
            elif status and name not in self.apps:
                self.log("Dependant stack %s exists, can't continue",
                         built_stack.name)
                return False

        return True

    def get_deploy(self, repository_path=None):
        if len(self.apps) != 1:
            raise StandardError("Can only deploy a single app at a time")
        stack_info = self.info(with_aws=False)[self.apps[0]]

        return Deploy(
            stack_info.parameters['ApplicationName'],
            stack_info.parameters['EnvironmentName'],
            repository_path,
            region=self.stack_context['aws_region'],
            logger=self.log,
            profile_name=self.profile_name,
        )

    @classmethod
    def from_ctx(cls, ctx, **kwargs):
        return cls(
            stacks=kwargs.get('stacks', ctx.stacks),
            stage=kwargs.get('stage', ctx.stage),
            environment=kwargs.get('environment', ctx.environment),
            variables=kwargs.get('variables', ctx.variables),
            apps=kwargs.get('apps', ctx.apps),
            logger=kwargs.get('logger', ctx.log),
            profile_name=kwargs.get('profile_name'),
        )

def get_stacks(stacks, names, with_dependencies=False):
    if with_dependencies:
        names = get_dependencies(stacks, names)

    return [(name, stacks[name]) for name in names]

def flatten_stack_groups(stacks, names):
    names = set(names)
    for name in names.copy():
        if isinstance(stacks[name], list):
            names.remove(name)
            names = names.union(flatten_stack_groups(stacks, stacks[name]))

    return names

def get_dependencies(stacks, names):
    return toposort_flatten(_get_dependencies(stacks, names))

def get_dependants(stacks, names):
    return toposort_flatten(_get_dependants(stacks, names))

def _get_dependencies(stacks, names):
    deps = {}
    for name in names:
        dependencies = stacks[name].dependencies
        deps[name] = set(dependencies)
        for dep in dependencies:
            deps.update(_get_dependencies(stacks, [dep]))

    return deps

def _get_dependants(stacks, names):
    reversed_deps = _reversed_deps(stacks, names)

    return dict((name, set(reversed_deps.get(name, []))) for name in names)

def _reversed_deps(stacks, names):
    reversed_deps = {}
    for stack in filter(lambda s: isinstance(stacks[s], Stack), stacks):
        for dep in stacks[stack].dependencies:
            if dep in reversed_deps:
                reversed_deps[dep].append(stack)
            else:
                reversed_deps[dep] = [stack]

    return reversed_deps
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Copyright 2014 Alvaro Lopez Garcia
#
# 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.

from __future__ import print_function

import sys

import atrope
from atrope import exception
import atrope.image_list.manager
from atrope import utils

from oslo_config import cfg
from six.moves import input

CONF = cfg.CONF

def add_command_parsers(subparsers):
    CommandImageListAdd(subparsers)
    CommandImageListIndex(subparsers)
    CommandImageListFetch(subparsers)
    CommandImageListCache(subparsers)
    CommandDispatch(subparsers)
    CommandVersion(subparsers)

command_opt = cfg.SubCommandOpt('command',
                                title='Commands',
                                help='Show available commands.',
                                handler=add_command_parsers)

CONF.register_cli_opt(command_opt)

class Command(object):
    def __init__(self, parser, name, cmd_help):
        self.name = name
        self.cmd_help = cmd_help
        self.parser = parser.add_parser(name, help=cmd_help)
        self.parser.set_defaults(func=self.run)

    def run(self):
        raise NotImplementedError("Method must me overriden on subclass")

class CommandImageListAdd(Command):
    def __init__(self, parser, name="imagelist-add",
                 cmd_help="Add a list to the configured image lists"):
        super(CommandImageListAdd, self).__init__(parser, name, cmd_help)

        self.parser.add_argument("list_id",
                                 default=None,
                                 nargs='?',
                                 help="List ID to add.")

        self.parser.add_argument("-u",
                                 "--url",
                                 dest="url",
                                 metavar="URL",
                                 default="",
                                 help="List URL")

        self.parser.add_argument("-o",
                                 "--endorser_dn",
                                 dest="endorser_dn",
                                 metavar="DN",
                                 default="",
                                 help="Endorser's DN")

        self.parser.add_argument("-c",
                                 "--endorser_ca",
                                 dest="endorser_ca",
                                 metavar="CA DN",
                                 default="",
                                 help="Endorser's CA DN")

        self.parser.add_argument("-t",
                                 "--token",
                                 dest="token",
                                 metavar="TOKEN",
                                 default="",
                                 help="Access token to use")

        self.parser.add_argument("-p",
                                 "--prefix",
                                 dest="prefix",
                                 metavar="PREFIX",
                                 default="",
                                 help="If set images names will be prefixed "
                                      "with this.")

        self.parser.add_argument("-P",
                                 "--project",
                                 dest="project",
                                 metavar="PROJECT",
                                 default="",
                                 help="OpenStack project to which these "
                                      "images will be associated.")

        self.parser.add_argument("-f",
                                 "--force",
                                 dest="force",
                                 default=False,
                                 action="store_true",
                                 help="Overwrite list if it exists")

        group = self.parser.add_mutually_exclusive_group()
        group.add_argument("-e",
                           "--enabled",
                           dest="enabled",
                           default=True,
                           action="store_true",
                           help="Add list as enabled (default)")

        group.add_argument("-d",
                           "--disabled",
                           dest="enabled",
                           action="store_false",
                           help="Add list as disabled")

    def _get_values(self, url, token, prefix, enabled, endorser, project):
        def get_endorser(default={}):
            print("Enter endorser details.")
            dn = input("\tEndorser DN [%s]: " %
                       default.get("dn", "")) or default.get("dn")
            if not dn:
                return {}

            ca = input("\tEndorser CA [%s]: " %
                       default.get("ca", "")) or default.get("ca")
            if not ca:
                print("CA cannot be empty, try again.")
                return get_endorser(default={"dn": dn})

            return {"dn": dn, "ca": ca}

        def get_str(msg, mandatory=False, default=""):
            identifier = input("%s [%s]: " % (msg, default)) or default
            if not identifier and mandatory:
                raise exception.MissingMandatoryFieldImageList(field=msg)
            return identifier

        def get_list(msg, mandatory=False):
            print(msg)
            print("\t(one value per line, empty to finish)")
            ret = []
            el = None
            while True:
                el = input("\timage id: ")
                if not el:
                    break
                ret.append(el)

            if not ret and mandatory:
                raise exception.MissingMandatoryFieldImageList(field=msg)
            return ret

        def print_image_list(identifier, url, token, prefix, enabled,
                             endorser, subscribed_images, project):
            d = {
                "identifier": identifier,
                "url": url,
                "token": token,
                "prefix": prefix,
                "enabled": enabled,
                "endorser dn": endorser.get("dn"),
                "endorser ca": endorser.get("ca"),
                "subscribed images": subscribed_images,
                "project": project,
            }
            utils.print_dict(d)

        identifier = ""
        while True:
            identifier = get_str("list id", mandatory=True, default=identifier)
            url = get_str("list URL", default=url)
            enabled = utils.yn_question(default=enabled)
            endorser = get_endorser(default=endorser)

            token = get_str("auth token", default=token)

            prefix = get_str("prefix", default=prefix)

            project = get_str("project", default="")

            subscribed_images = get_list("subscribed images")

            print_image_list(identifier, url, token, prefix, enabled,
                             endorser, subscribed_images, project)
            msg = "Is the information above correct?"
            correct = utils.yn_question(msg=msg)
            if correct:
                break
            print("\nOK, lets try again")

        return (identifier, url, enabled, endorser,
                token, prefix, subscribed_images, project)

    def run(self):
        identifier = CONF.command.list_id
        url = CONF.command.url
        token = CONF.command.token
        prefix = CONF.command.prefix
        enabled = CONF.command.enabled
        force = CONF.command.force
        endorser_dn = CONF.command.endorser_dn
        endorser_ca = CONF.command.endorser_ca
        project = CONF.command.project
        if (endorser_dn == "") == (endorser_ca == ""):
            if CONF.command.endorser_dn:
                endorser = {"dn": endorser_dn,
                            "ca": endorser_ca}
            else:
                endorser = {}

        if identifier is None:
            self.add_interative(url, token, prefix, enabled, endorser,
                                project, force)
        else:
            self.add_non_interactive(identifier, url, token, prefix, enabled,
                                     endorser, project, force)

    def add_non_interactive(self, identifier, url, token, prefix, enabled,
                            endorser, project, force):
        image_list = atrope.image_list.hepix.HepixImageListSource(
            identifier,
            url,
            enabled=enabled,
            endorser=endorser,
            token=token,
            prefix=prefix,
            project=project
        )

        manager = atrope.image_list.manager.YamlImageListManager()
        manager.add_image_list_source(image_list, force=force)
        manager.write_image_list_sources()

    def add_interative(self, url, token, prefix, enabled, endorser,
                       project, force):
        print("Adding image list, enter the following details (Ctr+C to exit)")
        (identifier, url, enabled, endorser, token, prefix,
         subscribed_images, project) = self._get_values(url, token, prefix,
                                                        enabled, endorser,
                                                        project)
        image_list = atrope.image_list.hepix.HepixImageListSource(
            identifier,
            url,
            enabled=enabled,
            endorser=endorser,
            token=token,
            prefix=prefix,
            subscribed_images=subscribed_images
        )

        manager = atrope.image_list.manager.YamlImageListManager()
        try:
            manager.add_image_list_source(image_list, force=force)
            manager.write_image_list_sources()
        except exception.DuplicatedImageList:
            msg = "Image with id '%s' already in index, update?" % identifier
            force = utils.yn_question(msg)
            if force:
                manager.add_image_list_source(image_list, force=True)
                manager.write_image_list_sources()

class CommandImageListIndex(Command):
    def __init__(self, parser, name="imagelist-index",
                 cmd_help="Show the configured image lists"):
        super(CommandImageListIndex, self).__init__(parser, name, cmd_help)

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        # TODO(aloga): wrap the fields, since the output is huge
        fields = ["name", "url", "enabled", "endorser"]
        objs = []
        for lst in manager.lists.values():
            d = {}
            for f in fields:
                d[f] = getattr(lst, f)
            objs.append(d)
        utils.print_list(objs, fields)

class CommandImageListFetch(Command):
    def __init__(self, parser, name="imagelist-fetch",
                 cmd_help="Fetch and verify the configured image lists."):
        super(CommandImageListFetch, self).__init__(parser, name, cmd_help)

        self.parser.add_argument("-c",
                                 "--contents",
                                 dest="contents",
                                 default=False,
                                 action="store_true",
                                 help="Show the list contents")

        self.parser.add_argument("list",
                                 default=None,
                                 nargs='?',
                                 help="Image list to fetch.")

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        if CONF.command.list is not None:
            lists = [manager.fetch_list(CONF.command.list)]
        else:
            lists = manager.fetch_lists()

        for lst in lists:
            lst.print_list(contents=CONF.command.contents)

class CommandImageListCache(Command):
    def __init__(self, parser, name="cache-sync",
                 cmd_help="Sync cache directory."):
        super(CommandImageListCache, self).__init__(parser, name, cmd_help)

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        manager.sync_cache()

class CommandDispatch(Command):
    def __init__(self, parser, name="dispatch",
                 cmd_help="Dispatch (help TBD)."):
        super(CommandDispatch, self).__init__(parser, name, cmd_help)

        self.parser.add_argument("-n",
                                 "--no-sync",
                                 dest="sync",
                                 default=True,
                                 action="store_false",
                                 help="Do not sync image list with "
                                      "dispatched data")

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        manager.sync_cache()
        manager.dispatch(CONF.command.sync)

class CommandVersion(Command):
    def __init__(self, parser, name="version",
                 cmd_help="Show verison and exit."):
        super(CommandVersion, self).__init__(parser, name, cmd_help)

    def run(self):
        print(atrope.__version__)

class CommandManager(object):
    def execute(self):
        try:
            CONF.command.func()
        except exception.AtropeException as e:
            print("ERROR: %s" % e, file=sys.stderr)
            sys.exit(1)
        except KeyboardInterrupt:
            print("\nExiting...", file=sys.stderr)
            sys.exit(0)
<commit_msg>Remove CommandImageListAdd command<commit_after># -*- coding: utf-8 -*-

# Copyright 2014 Alvaro Lopez Garcia
#
# 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.

from __future__ import print_function

import sys

import atrope
from atrope import exception
import atrope.image_list.manager
from atrope import utils

from oslo_config import cfg

CONF = cfg.CONF

def add_command_parsers(subparsers):
    CommandImageListIndex(subparsers)
    CommandImageListFetch(subparsers)
    CommandImageListCache(subparsers)
    CommandDispatch(subparsers)
    CommandVersion(subparsers)

command_opt = cfg.SubCommandOpt('command',
                                title='Commands',
                                help='Show available commands.',
                                handler=add_command_parsers)

CONF.register_cli_opt(command_opt)

class Command(object):
    def __init__(self, parser, name, cmd_help):
        self.name = name
        self.cmd_help = cmd_help
        self.parser = parser.add_parser(name, help=cmd_help)
        self.parser.set_defaults(func=self.run)

    def run(self):
        raise NotImplementedError("Method must me overriden on subclass")

class CommandImageListIndex(Command):
    def __init__(self, parser, name="imagelist-index",
                 cmd_help="Show the configured image lists"):
        super(CommandImageListIndex, self).__init__(parser, name, cmd_help)

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        # TODO(aloga): wrap the fields, since the output is huge
        fields = ["name", "url", "enabled", "endorser"]
        objs = []
        for lst in manager.lists.values():
            d = {}
            for f in fields:
                d[f] = getattr(lst, f)
            objs.append(d)
        utils.print_list(objs, fields)

class CommandImageListFetch(Command):
    def __init__(self, parser, name="imagelist-fetch",
                 cmd_help="Fetch and verify the configured image lists."):
        super(CommandImageListFetch, self).__init__(parser, name, cmd_help)

        self.parser.add_argument("-c",
                                 "--contents",
                                 dest="contents",
                                 default=False,
                                 action="store_true",
                                 help="Show the list contents")

        self.parser.add_argument("list",
                                 default=None,
                                 nargs='?',
                                 help="Image list to fetch.")

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        if CONF.command.list is not None:
            lists = [manager.fetch_list(CONF.command.list)]
        else:
            lists = manager.fetch_lists()

        for lst in lists:
            lst.print_list(contents=CONF.command.contents)

class CommandImageListCache(Command):
    def __init__(self, parser, name="cache-sync",
                 cmd_help="Sync cache directory."):
        super(CommandImageListCache, self).__init__(parser, name, cmd_help)

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        manager.sync_cache()

class CommandDispatch(Command):
    def __init__(self, parser, name="dispatch",
                 cmd_help="Dispatch (help TBD)."):
        super(CommandDispatch, self).__init__(parser, name, cmd_help)

        self.parser.add_argument("-n",
                                 "--no-sync",
                                 dest="sync",
                                 default=True,
                                 action="store_false",
                                 help="Do not sync image list with "
                                      "dispatched data")

    def run(self):
        manager = atrope.image_list.manager.YamlImageListManager()
        manager.sync_cache()
        manager.dispatch(CONF.command.sync)

class CommandVersion(Command):
    def __init__(self, parser, name="version",
                 cmd_help="Show verison and exit."):
        super(CommandVersion, self).__init__(parser, name, cmd_help)

    def run(self):
        print(atrope.__version__)

class CommandManager(object):
    def execute(self):
        try:
            CONF.command.func()
        except exception.AtropeException as e:
            print("ERROR: %s" % e, file=sys.stderr)
            sys.exit(1)
        except KeyboardInterrupt:
            print("\nExiting...", file=sys.stderr)
            sys.exit(0)
<|endoftext|>


<commit_before>import numpy as np

FNFP_PARAM_NAMES = ['fnfp_si', 'fnfp_sd', 'fnfp_anapole', 'fnfp_magdip', 'fnfp_elecdip',
                    'fnfp_LS', 'fnfp_f1', 'fnfp_f2', 'fnfp_f3', 'fnfp_si_massless', 'fnfp_sd_massless',
                    'fnfp_anapole_massless', 'fnfp_magdip_massless', 'fnfp_elecdip_massless', 'fnfp_LS_massless',
                    'fnfp_f1_massless', 'fnfp_f2_massless', 'fnfp_f3_massless']

#SMD: these are defined such that they give of order 0.01 events on LUX 2013:
PAR_NORMS = {
    'sigma_si':1.e-47,
    'sigma_sd':1.e-42,
    'sigma_anapole':1.e-40,
    'sigma_magdip':1.e-38,
    'sigma_elecdip':1.e-44,
    'sigma_LS':1.e-44,
    'sigma_f1':1.e-47,
    'sigma_f2':1.e-42,
    'sigma_f3':1.e-41,
    'sigma_si_massless':1.e-48,
    'sigma_sd_massless':1.e-43,
    'sigma_anapole_massless':1.e-45,
    'sigma_magdip_massless':1.e-39,
    'sigma_elecdip_massless':1.e-45,
    'sigma_LS_massless':1.e-42,
    'sigma_f1_massless':1.e-48,
    'sigma_f2_massless':1.e-43,
    'sigma_f3_massless':1.e-42
    }

PAR_NORM_EXPONENTS = {
    'sigma_si': np.log10(PAR_NORMS['sigma_si']),
    'sigma_sd': np.log10(PAR_NORMS['sigma_sd']),
    'sigma_anapole': np.log10(PAR_NORMS['sigma_anapole']),
    'sigma_magdip': np.log10(PAR_NORMS['sigma_magdip']),
    'sigma_elecdip': np.log10(PAR_NORMS['sigma_elecdip']),
    'sigma_LS': np.log10(PAR_NORMS['sigma_LS']),
    'sigma_f1': np.log10(PAR_NORMS['sigma_f1']),
    'sigma_f2': np.log10(PAR_NORMS['sigma_f2']),
    'sigma_f3': np.log10(PAR_NORMS['sigma_f3']),
    'sigma_si_massless': np.log10(PAR_NORMS['sigma_si_massless']),
    'sigma_sd_massless': np.log10(PAR_NORMS['sigma_sd_massless']),
    'sigma_anapole_massless': np.log10(PAR_NORMS['sigma_anapole_massless']),
    'sigma_magdip_massless': np.log10(PAR_NORMS['sigma_magdip_massless']),
    'sigma_elecdip_massless': np.log10(PAR_NORMS['sigma_elecdip_massless']),
    'sigma_LS_massless': np.log10(PAR_NORMS['sigma_LS_massless']),
    'sigma_f1_massless': np.log10(PAR_NORMS['sigma_f1_massless']),
    'sigma_f2_massless': np.log10(PAR_NORMS['sigma_f2_massless']),
    'sigma_f3_massless': np.log10(PAR_NORMS['sigma_f3_massless']),
    }

PARAM_TEX = {
    'mass': r'$m_\chi$ [GeV]',
    'sigma_si': r'$\sigma_{{p}}^{{SI}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_si']),
    'sigma_sd': r'$\sigma_{{p}}^{{SD}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_sd']),
    'sigma_anapole': r'$\sigma_{{p}}^{{ana}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_anapole']),
    'sigma_magdip': r'$\sigma_{{p}}^{{MD}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_magdip']),
    'sigma_elecdip': r'$\sigma_{{p}}^{{ED}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_elecdip']),
    'sigma_LS': r'$\sigma_{{p}}^{{LS}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_LS']),
    'sigma_f1': r'$\sigma_{{p}}^{{f_1}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f1']),
    'sigma_f2': r'$\sigma_{{p}}^{{f_2}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f2']),
    'sigma_f3': r'$\sigma_{{p}}^{{f_3}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f3']),
    'sigma_si_massless': r'$\sigma_{{p}}^{{milli}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_si_massless']),
    'sigma_sd_massless': r'$\sigma_{{p}}^{{SD, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_sd_massless']),
    'sigma_anapole_massless': r'$\sigma_{{p}}^{{ana, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_anapole_massless']),
    'sigma_magdip_massless': r'$\sigma_{{p}}^{{MD, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_magdip_massless']),
    'sigma_elecdip_massless': r'$\sigma_{{p}}^{{ED, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_elecdip_massless']),
    'sigma_LS_massless': r'$\sigma_{{p}}^{{LS, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_LS_massless']),
    'sigma_f1_massless': r'$\sigma_{{p}}^{{f_1, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f1_massless']),
    'sigma_f2_massless': r'$\sigma_{{p}}^{{f_2, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f2_massless']),
    'sigma_f3_massless': r'$\sigma_{{p}}^{{f_3, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f3_massless']),
    'fnfp_si': r'$f_n/f_p$',
    'fnfp_sd': r'$f_n/f_p$',
    'fnfp_anapole': r'$f_n/f_p$',
    'fnfp_magdip': r'$f_n/f_p$',
    'fnfp_elecdip': r'$f_n/f_p$',
    'fnfp_LS': r'$f_n/f_p$',
    'fnfp_f1': r'$f_n/f_p$',
    'fnfp_f2': r'$f_n/f_p$',
    'fnfp_f3': r'$f_n/f_p$',
    'fnfp_si_massless': r'$f_n/f_p$',
    'fnfp_sd_massless': r'$f_n/f_p$',
    'fnfp_anapole_massless': r'$f_n/f_p$',
    'fnfp_magdip_massless': r'$f_n/f_p$',
    'fnfp_elecdip_massless': r'$f_n/f_p$',
    'fnfp_LS_massless': r'$f_n/f_p$',
    'fnfp_f1_massless': r'$f_n/f_p$',
    'fnfp_f2_massless': r'$f_n/f_p$',
    'fnfp_f3_massless':r'$f_n/f_p$',
    }

MODELNAME_COLOR = {
    'SI_Higgs': 'k',
    'Millicharge': 'Magenta',
    'SD_fu': 'DarkBlue',
    'Anapole': 'BlueViolet',
    'Mag.dip.heavy': 'DarkGreen',
    'Mag.dip.light': 'Maroon',
    'Elec.dip.heavy': 'LightGreen',
    'Elec.dip.light': 'Red',
    'f1': 'k',
    'f2_flavor-universal': 'DarkBlue',
    'f2_Higgs': 'blue',
    'f3_flavor-universal': 'Maroon',
    'f3_Higgs': 'Red',
    'LS': 'Magenta'
    }

MODELNAME_TEX = {
    'SI_Higgs': 'SI Higgs',
    'Millicharge': 'Millicharge',
    'SD_fu': 'SD flavor-univ.',
    'Anapole': 'Anapole',
    'Mag.dip.heavy': 'Mag. dip. heavy',
    'Mag.dip.light': 'Mag. dip. light',
    'Elec.dip.heavy': 'Elec. dip. heavy',
    'Elec.dip.light': 'Elec. dip. light',
    'f1': '$f_1$',
    'f2_flavor-universal': '$f_2$ flavor-univ.',
    'f2_Higgs': '$f_2$, Higgs',
    'f3_flavor-universal': '$f_3$ flavor-univ.',
    'f3_Higgs': '$f_3$, Higgs',
    'LS': '$L \cdot S$'
    }
<commit_msg>hiding imports for rtd some more<commit_after>try:
    import numpy as np
except ImportError:
    np = None

FNFP_PARAM_NAMES = ['fnfp_si', 'fnfp_sd', 'fnfp_anapole', 'fnfp_magdip', 'fnfp_elecdip',
                    'fnfp_LS', 'fnfp_f1', 'fnfp_f2', 'fnfp_f3', 'fnfp_si_massless', 'fnfp_sd_massless',
                    'fnfp_anapole_massless', 'fnfp_magdip_massless', 'fnfp_elecdip_massless', 'fnfp_LS_massless',
                    'fnfp_f1_massless', 'fnfp_f2_massless', 'fnfp_f3_massless']

#SMD: these are defined such that they give of order 0.01 events on LUX 2013:
PAR_NORMS = {
    'sigma_si':1.e-47,
    'sigma_sd':1.e-42,
    'sigma_anapole':1.e-40,
    'sigma_magdip':1.e-38,
    'sigma_elecdip':1.e-44,
    'sigma_LS':1.e-44,
    'sigma_f1':1.e-47,
    'sigma_f2':1.e-42,
    'sigma_f3':1.e-41,
    'sigma_si_massless':1.e-48,
    'sigma_sd_massless':1.e-43,
    'sigma_anapole_massless':1.e-45,
    'sigma_magdip_massless':1.e-39,
    'sigma_elecdip_massless':1.e-45,
    'sigma_LS_massless':1.e-42,
    'sigma_f1_massless':1.e-48,
    'sigma_f2_massless':1.e-43,
    'sigma_f3_massless':1.e-42
    }

PAR_NORM_EXPONENTS = {
    'sigma_si': np.log10(PAR_NORMS['sigma_si']),
    'sigma_sd': np.log10(PAR_NORMS['sigma_sd']),
    'sigma_anapole': np.log10(PAR_NORMS['sigma_anapole']),
    'sigma_magdip': np.log10(PAR_NORMS['sigma_magdip']),
    'sigma_elecdip': np.log10(PAR_NORMS['sigma_elecdip']),
    'sigma_LS': np.log10(PAR_NORMS['sigma_LS']),
    'sigma_f1': np.log10(PAR_NORMS['sigma_f1']),
    'sigma_f2': np.log10(PAR_NORMS['sigma_f2']),
    'sigma_f3': np.log10(PAR_NORMS['sigma_f3']),
    'sigma_si_massless': np.log10(PAR_NORMS['sigma_si_massless']),
    'sigma_sd_massless': np.log10(PAR_NORMS['sigma_sd_massless']),
    'sigma_anapole_massless': np.log10(PAR_NORMS['sigma_anapole_massless']),
    'sigma_magdip_massless': np.log10(PAR_NORMS['sigma_magdip_massless']),
    'sigma_elecdip_massless': np.log10(PAR_NORMS['sigma_elecdip_massless']),
    'sigma_LS_massless': np.log10(PAR_NORMS['sigma_LS_massless']),
    'sigma_f1_massless': np.log10(PAR_NORMS['sigma_f1_massless']),
    'sigma_f2_massless': np.log10(PAR_NORMS['sigma_f2_massless']),
    'sigma_f3_massless': np.log10(PAR_NORMS['sigma_f3_massless']),
    }

PARAM_TEX = {
    'mass': r'$m_\chi$ [GeV]',
    'sigma_si': r'$\sigma_{{p}}^{{SI}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_si']),
    'sigma_sd': r'$\sigma_{{p}}^{{SD}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_sd']),
    'sigma_anapole': r'$\sigma_{{p}}^{{ana}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_anapole']),
    'sigma_magdip': r'$\sigma_{{p}}^{{MD}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_magdip']),
    'sigma_elecdip': r'$\sigma_{{p}}^{{ED}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_elecdip']),
    'sigma_LS': r'$\sigma_{{p}}^{{LS}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_LS']),
    'sigma_f1': r'$\sigma_{{p}}^{{f_1}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f1']),
    'sigma_f2': r'$\sigma_{{p}}^{{f_2}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f2']),
    'sigma_f3': r'$\sigma_{{p}}^{{f_3}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f3']),
    'sigma_si_massless': r'$\sigma_{{p}}^{{milli}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_si_massless']),
    'sigma_sd_massless': r'$\sigma_{{p}}^{{SD, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_sd_massless']),
    'sigma_anapole_massless': r'$\sigma_{{p}}^{{ana, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_anapole_massless']),
    'sigma_magdip_massless': r'$\sigma_{{p}}^{{MD, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_magdip_massless']),
    'sigma_elecdip_massless': r'$\sigma_{{p}}^{{ED, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_elecdip_massless']),
    'sigma_LS_massless': r'$\sigma_{{p}}^{{LS, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_LS_massless']),
    'sigma_f1_massless': r'$\sigma_{{p}}^{{f_1, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f1_massless']),
    'sigma_f2_massless': r'$\sigma_{{p}}^{{f_2, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f2_massless']),
    'sigma_f3_massless': r'$\sigma_{{p}}^{{f_3, light}}$ [$10^{{{:.0f}}}$ cm$^2$]'.format(PAR_NORM_EXPONENTS['sigma_f3_massless']),
    'fnfp_si': r'$f_n/f_p$',
    'fnfp_sd': r'$f_n/f_p$',
    'fnfp_anapole': r'$f_n/f_p$',
    'fnfp_magdip': r'$f_n/f_p$',
    'fnfp_elecdip': r'$f_n/f_p$',
    'fnfp_LS': r'$f_n/f_p$',
    'fnfp_f1': r'$f_n/f_p$',
    'fnfp_f2': r'$f_n/f_p$',
    'fnfp_f3': r'$f_n/f_p$',
    'fnfp_si_massless': r'$f_n/f_p$',
    'fnfp_sd_massless': r'$f_n/f_p$',
    'fnfp_anapole_massless': r'$f_n/f_p$',
    'fnfp_magdip_massless': r'$f_n/f_p$',
    'fnfp_elecdip_massless': r'$f_n/f_p$',
    'fnfp_LS_massless': r'$f_n/f_p$',
    'fnfp_f1_massless': r'$f_n/f_p$',
    'fnfp_f2_massless': r'$f_n/f_p$',
    'fnfp_f3_massless':r'$f_n/f_p$',
    }

MODELNAME_COLOR = {
    'SI_Higgs': 'k',
    'Millicharge': 'Magenta',
    'SD_fu': 'DarkBlue',
    'Anapole': 'BlueViolet',
    'Mag.dip.heavy': 'DarkGreen',
    'Mag.dip.light': 'Maroon',
    'Elec.dip.heavy': 'LightGreen',
    'Elec.dip.light': 'Red',
    'f1': 'k',
    'f2_flavor-universal': 'DarkBlue',
    'f2_Higgs': 'blue',
    'f3_flavor-universal': 'Maroon',
    'f3_Higgs': 'Red',
    'LS': 'Magenta'
    }

MODELNAME_TEX = {
    'SI_Higgs': 'SI Higgs',
    'Millicharge': 'Millicharge',
    'SD_fu': 'SD flavor-univ.',
    'Anapole': 'Anapole',
    'Mag.dip.heavy': 'Mag. dip. heavy',
    'Mag.dip.light': 'Mag. dip. light',
    'Elec.dip.heavy': 'Elec. dip. heavy',
    'Elec.dip.light': 'Elec. dip. light',
    'f1': '$f_1$',
    'f2_flavor-universal': '$f_2$ flavor-univ.',
    'f2_Higgs': '$f_2$, Higgs',
    'f3_flavor-universal': '$f_3$ flavor-univ.',
    'f3_Higgs': '$f_3$, Higgs',
    'LS': '$L \cdot S$'
    }
<|endoftext|>


<commit_before>#!/usr/bin/env python

def configuration(parent_package='', top_path=None):
    import os.path as op
    import glob
    from numpy.distutils.misc_util import Configuration

    from sfepy import Config

    site_config = Config()
    os_flag = {'posix' : 0, 'windows' : 1}

    auto_dir = op.dirname(__file__)
    auto_name = op.split(auto_dir)[-1]
    config = Configuration(auto_name, parent_package, top_path)

    defines = [('__SDIR__', "'\"%s\"'" % auto_dir),
               ('SFEPY_PLATFORM', os_flag[site_config.system()])]
    if '-DDEBUG_FMF' in site_config.debug_flags():
        defines.append(('DEBUG_FMF', None))

    common_src = ['fmfield.c', 'geometry.c', 'geommech.c', 'common_python.c']
    common_src = [op.join('../../fem/extmods', ii) for ii in common_src]

    csrc = [op.split(ii)[1] for ii in glob.glob('sfepy/terms/extmods/*.c')]
    try:
        csrc.remove('terms.c')
    except ValueError:
        pass

    config.add_library('sfepy_terms',
                       sources=csrc,
                       depends=common_src,
                       extra_compiler_args=site_config.compile_flags(),
                       extra_link_args=site_config.link_flags(),
                       include_dirs=[auto_dir, '../../fem/extmods',
                                     site_config.python_include()],
                       macros=defines)

    src = ['terms.pyx']
    config.add_extension('terms',
                         sources=src,
                         libraries=['sfepy_terms', 'sfepy_common'],
                         depends=[csrc + common_src],
                         extra_compile_args=site_config.compile_flags(),
                         extra_link_args=site_config.link_flags(),
                         include_dirs=[auto_dir, '../../fem/extmods'],
                         define_macros=defines)

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())
<commit_msg>cython: small fix<commit_after>#!/usr/bin/env python

def configuration(parent_package='', top_path=None):
    import os.path as op
    import glob
    from numpy.distutils.misc_util import Configuration

    from sfepy import Config

    site_config = Config()
    os_flag = {'posix' : 0, 'windows' : 1}

    auto_dir = op.dirname(__file__)
    auto_name = op.split(auto_dir)[-1]
    config = Configuration(auto_name, parent_package, top_path)

    defines = [('__SDIR__', "'\"%s\"'" % auto_dir),
               ('SFEPY_PLATFORM', os_flag[site_config.system()])]
    if '-DDEBUG_FMF' in site_config.debug_flags():
        defines.append(('DEBUG_FMF', None))

    common_src = ['fmfield.c', 'geometry.c', 'geommech.c', 'common_python.c']
    common_src = [op.join('../../fem/extmods', ii) for ii in common_src]

    csrc = [op.split(ii)[1] for ii in glob.glob('sfepy/terms/extmods/*.c')]
    try:
        csrc.remove('terms.c')
    except ValueError:
        pass

    config.add_library('sfepy_terms',
                       sources=csrc,
                       depends=common_src,
                       extra_compiler_args=site_config.compile_flags(),
                       extra_link_args=site_config.link_flags(),
                       include_dirs=[auto_dir, '../../fem/extmods',
                                     site_config.python_include()],
                       macros=defines)

    src = ['terms.pyx']
    config.add_extension('terms',
                         sources=src,
                         libraries=['sfepy_terms', 'sfepy_common'],
                         depends=csrc + common_src,
                         extra_compile_args=site_config.compile_flags(),
                         extra_link_args=site_config.link_flags(),
                         include_dirs=[auto_dir, '../../fem/extmods'],
                         define_macros=defines)

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup
    setup(**configuration(top_path='').todict())
<|endoftext|>


<commit_before>from attest.deprecated import *
from attest.hook import *
from attest.reporters import *
from attest.contexts import *
from attest.collectors import *
from attest import statistics

AssertImportHook.enable()
<commit_msg>Redundant import<commit_after>from attest.deprecated import *
from attest.hook import *
from attest.reporters import *
from attest.contexts import *
from attest.collectors import *

AssertImportHook.enable()
<|endoftext|>


<commit_before>import numpy as nm

from sfepy.base.base import Struct
from sfepy.terms.terms import Term
from sfepy.terms.extmods import terms

class ContactInfo(Struct):
    """
    Various contact-related data of contact terms.
    """
    pass

class ContactTerm(Term):
    r"""
    """
    name = 'dw_contact'
    arg_types = ('material', 'virtual', 'state')
    arg_shapes = {'material' : '.: 1',
                  'virtual' : ('D', 'state'), 'state' : 'D'}
    integration = 'surface'

    def __init__(self, *args, **kwargs):
        Term.__init__(self, *args, **kwargs)

        self.detect = 2
        self.ci = None

    def call_function(self, fargs):
        try:
            out, status = self.function(*fargs)

        except (RuntimeError, ValueError):
            terms.errclear()
            raise

        if status:
            terms.errclear()
            raise ValueError('term evaluation failed! (%s)' % self.name)

        return out, status

    def eval_real(self, shape, fargs, mode='eval', term_mode=None,
                  diff_var=None, **kwargs):
        out, status = self.call_function(fargs)
        if mode != 'weak':
            raise ValueError('unsupported evaluation mode! (%s)' % mode)

        return out, status

    @staticmethod
    def function(out_cc):
        return out_cc, 0

    def get_fargs(self, epss, virtual, state,
                  mode=None, term_mode=None, diff_var=None, **kwargs):
        geo, _ = self.get_mapping(virtual)

        region = self.region

        if self.ci is None:
            self.ci = ContactInfo()

        # print(region.name)
        # print(region.shape)
        # print(region.facets)
        # print(region.get_facet_indices())
        # print(state.field.surface_data[region.name].fis)

        # print(geo)
        # print(geo.normal)

        # mesh_coors = self.region.domain.mesh.coors
        # print(mesh_coors[region.vertices])

        # Uses field connectivity (higher order nodes).
        sd = state.field.surface_data[region.name]

        # Uses mesh connectivity.
        # sdg = self.region.domain.surface_groups[region.name]

        # print(mesh_coors[sdg.econn])

        # qps = self.get_physical_qps()
        # qp_coors = qps.values
        # u_qp = self.get(state, 'val').reshape(qp_coors.shape)

        # Deformed QP coordinates.
        # coors = u_qp + qp_coors

        # print(coors)

        ISN = state.field.efaces.T.copy()
        nsd = region.dim
        ngp = geo.n_qp
        neq = state.n_dof
        nsn = ISN.shape[0]
        nes = ISN.shape[1]
        nen = state.field.gel.n_vertex

        fis = region.get_facet_indices()
        elementID = fis[:, 0].copy()
        segmentID = fis[:, 1].copy()

        n = len(elementID)
        IEN = state.field.econn
        # Need surface bf, bfg corresponding to field approximation here, not
        # geo...
        H = nm.asfortranarray(geo.bf[0, :, 0, :])
        ps = state.field.gel.surface_facet.poly_space
        gps, gw = self.integral.get_qp(state.field.gel.surface_facet.name)
        bfg = ps.eval_base(gps, diff=True)
        # ?? shape - try in 3D
        #dH  = nm.asfortranarray(bfg[:, 0, :])
        dH = nm.ravel(bfg, order='C')
        dH  = nm.asfortranarray(dH.reshape(n*ngp, nsn))

        X = nm.asfortranarray(state.field.coors)
        Um = nm.asfortranarray(state().reshape((-1, nsd)))
        xx = nm.asfortranarray(X + Um)

        import sfepy.mechanics.extmods.ccontres as cc

        GPs = nm.empty((n*ngp, 2*nsd+6), dtype=nm.float64, order='F')

        longestEdge, GPs = cc.get_longest_edge_and_gps(GPs, neq,
                                                       elementID, segmentID,
                                                       ISN, IEN, H, xx)

        # print longestEdge
        # print X[IEN[elementID[0]]][ISN]

        # gg = state.field.mappings[('Omega', 2, 'volume')]
        # Implement Region.get_diameters(dim).

        #print nm.sqrt(region.domain.get_element_diameters(state.field.region.cells, gg[0], 0)).max()
        #print (GPs[:, :2] == qp_coors).all()

        #GPs2 = nm.zeros((n*ngp, 2*nsd+6), dtype=nm.float64, order='F')
        #GPs2[:, :2] = qp_coors
        #GPs2[:, 2:4] = nm.repeat(fis, ngp, axis=0)
        #GPs2[:, 4] = nm.finfo(nm.float32).max

        #print (GPs == GPs2).all()
        #print nm.abs(GPs - GPs2).max()

        AABBmin, AABBmax = cc.get_AABB(xx, longestEdge, IEN, ISN,
                                       elementID, segmentID, neq);

        AABBmin = AABBmin - (0.5*longestEdge);
        AABBmax = AABBmax + (0.5*longestEdge);
        N = nm.ceil((AABBmax - AABBmin) / (0.5*longestEdge)).astype(nm.int32)
        N = nm.ones(nsd, dtype=nm.int32) # BUG workaround.

        # print AABBmin, AABBmax, N
        head, next = cc.init_global_search(N, AABBmin, AABBmax, GPs[:,:nsd])

        # print head, next

        npd = region.tdim - 1
        GPs = cc.evaluate_contact_constraints(GPs, ISN, IEN, N,
                                              AABBmin, AABBmax,
                                              head, next, xx,
                                              elementID, segmentID,
                                              npd, neq, longestEdge)
        # print GPs[:, 4]

        #
        Gc = nm.zeros(neq, dtype=nm.float64)

        activeGPs = GPs[:, 2*nsd+3]
        gap = GPs[:, nsd + 2]
        print activeGPs
        print gap
        print 'active:', activeGPs.sum()

        if diff_var is None:
            max_num = 1
            keyContactDetection = self.detect
            keyAssembleKc = 0

        else:
            max_num = 4 * (nsd * nsn)**2 * ngp * GPs.shape[0]
            keyContactDetection = self.detect
            keyAssembleKc = 1

        print 'max_num:', max_num
        vals = nm.empty(max_num, dtype=nm.float64)
        rows = nm.empty(max_num, dtype=nm.int32)
        cols = nm.empty(max_num, dtype=nm.int32)

        print '!!!detect', keyContactDetection
        aux = cc.assemble_contact_residual_and_stiffness(Gc, vals, rows, cols,
                                                         GPs, ISN, IEN,
                                                         X, Um, H, dH, gw,
                                                         activeGPs, neq, npd,
                                                         epss,
                                                         keyContactDetection,
                                                         keyAssembleKc)
        Gc, vals, rows, cols, num = aux
        # print Gc.mean()
        # print GPs
        print 'true num:', num
        print GPs.shape
        print activeGPs
        print gap

        # Uncomment this to detect only in the 1. iteration.
        #self.detect = max(0, self.detect - 1)
        if diff_var is None:
            from sfepy.discrete.variables import create_adof_conn
            rows = nm.unique(create_adof_conn(nm.arange(len(Gc)), sd.econn,
                                              nsd, 0))
            Gc = Gc[rows]

            eq = state.eq_map.eq
            erows = eq[rows]
            active = (erows >= 0)
            out_cc = (Gc[active], erows[active])

        else:
            vals, rows, cols = vals[:num], rows[:num], cols[:num]
            eq = state.eq_map.eq

            erows, ecols = eq[rows], eq[cols]
            active = (erows >= 0) & (ecols >= 0)
            out_cc = (vals[active], erows[active], ecols[active])

        return out_cc,
<commit_msg>fix dH shape in ContactTerm for 2D/3D and any number of contact segments<commit_after>import numpy as nm

from sfepy.base.base import Struct
from sfepy.terms.terms import Term
from sfepy.terms.extmods import terms

class ContactInfo(Struct):
    """
    Various contact-related data of contact terms.
    """
    pass

class ContactTerm(Term):
    r"""
    """
    name = 'dw_contact'
    arg_types = ('material', 'virtual', 'state')
    arg_shapes = {'material' : '.: 1',
                  'virtual' : ('D', 'state'), 'state' : 'D'}
    integration = 'surface'

    def __init__(self, *args, **kwargs):
        Term.__init__(self, *args, **kwargs)

        self.detect = 2
        self.ci = None

    def call_function(self, fargs):
        try:
            out, status = self.function(*fargs)

        except (RuntimeError, ValueError):
            terms.errclear()
            raise

        if status:
            terms.errclear()
            raise ValueError('term evaluation failed! (%s)' % self.name)

        return out, status

    def eval_real(self, shape, fargs, mode='eval', term_mode=None,
                  diff_var=None, **kwargs):
        out, status = self.call_function(fargs)
        if mode != 'weak':
            raise ValueError('unsupported evaluation mode! (%s)' % mode)

        return out, status

    @staticmethod
    def function(out_cc):
        return out_cc, 0

    def get_fargs(self, epss, virtual, state,
                  mode=None, term_mode=None, diff_var=None, **kwargs):
        geo, _ = self.get_mapping(virtual)

        region = self.region

        if self.ci is None:
            self.ci = ContactInfo()

        # print(region.name)
        # print(region.shape)
        # print(region.facets)
        # print(region.get_facet_indices())
        # print(state.field.surface_data[region.name].fis)

        # print(geo)
        # print(geo.normal)

        # mesh_coors = self.region.domain.mesh.coors
        # print(mesh_coors[region.vertices])

        # Uses field connectivity (higher order nodes).
        sd = state.field.surface_data[region.name]

        # Uses mesh connectivity.
        # sdg = self.region.domain.surface_groups[region.name]

        # print(mesh_coors[sdg.econn])

        # qps = self.get_physical_qps()
        # qp_coors = qps.values
        # u_qp = self.get(state, 'val').reshape(qp_coors.shape)

        # Deformed QP coordinates.
        # coors = u_qp + qp_coors

        # print(coors)

        ISN = state.field.efaces.T.copy()
        nsd = region.dim
        ngp = geo.n_qp
        neq = state.n_dof
        nsn = ISN.shape[0]
        nes = ISN.shape[1]
        nen = state.field.gel.n_vertex

        fis = region.get_facet_indices()
        elementID = fis[:, 0].copy()
        segmentID = fis[:, 1].copy()

        n = len(elementID)
        IEN = state.field.econn
        # Need surface bf, bfg corresponding to field approximation here, not
        # geo...
        H = nm.asfortranarray(geo.bf[0, :, 0, :])
        ps = state.field.gel.surface_facet.poly_space
        gps, gw = self.integral.get_qp(state.field.gel.surface_facet.name)
        bfg = ps.eval_base(gps, diff=True)
        dH  = nm.asfortranarray(bfg.ravel().reshape(((nsd - 1) * ngp, nsn)))

        X = nm.asfortranarray(state.field.coors)
        Um = nm.asfortranarray(state().reshape((-1, nsd)))
        xx = nm.asfortranarray(X + Um)

        import sfepy.mechanics.extmods.ccontres as cc

        GPs = nm.empty((n*ngp, 2*nsd+6), dtype=nm.float64, order='F')

        longestEdge, GPs = cc.get_longest_edge_and_gps(GPs, neq,
                                                       elementID, segmentID,
                                                       ISN, IEN, H, xx)

        # print longestEdge
        # print X[IEN[elementID[0]]][ISN]

        # gg = state.field.mappings[('Omega', 2, 'volume')]
        # Implement Region.get_diameters(dim).

        #print nm.sqrt(region.domain.get_element_diameters(state.field.region.cells, gg[0], 0)).max()
        #print (GPs[:, :2] == qp_coors).all()

        #GPs2 = nm.zeros((n*ngp, 2*nsd+6), dtype=nm.float64, order='F')
        #GPs2[:, :2] = qp_coors
        #GPs2[:, 2:4] = nm.repeat(fis, ngp, axis=0)
        #GPs2[:, 4] = nm.finfo(nm.float32).max

        #print (GPs == GPs2).all()
        #print nm.abs(GPs - GPs2).max()

        AABBmin, AABBmax = cc.get_AABB(xx, longestEdge, IEN, ISN,
                                       elementID, segmentID, neq);

        AABBmin = AABBmin - (0.5*longestEdge);
        AABBmax = AABBmax + (0.5*longestEdge);
        N = nm.ceil((AABBmax - AABBmin) / (0.5*longestEdge)).astype(nm.int32)
        N = nm.ones(nsd, dtype=nm.int32) # BUG workaround.

        # print AABBmin, AABBmax, N
        head, next = cc.init_global_search(N, AABBmin, AABBmax, GPs[:,:nsd])

        # print head, next

        npd = region.tdim - 1
        GPs = cc.evaluate_contact_constraints(GPs, ISN, IEN, N,
                                              AABBmin, AABBmax,
                                              head, next, xx,
                                              elementID, segmentID,
                                              npd, neq, longestEdge)
        # print GPs[:, 4]

        #
        Gc = nm.zeros(neq, dtype=nm.float64)

        activeGPs = GPs[:, 2*nsd+3]
        gap = GPs[:, nsd + 2]
        print activeGPs
        print gap
        print 'active:', activeGPs.sum()

        if diff_var is None:
            max_num = 1
            keyContactDetection = self.detect
            keyAssembleKc = 0

        else:
            max_num = 4 * (nsd * nsn)**2 * ngp * GPs.shape[0]
            keyContactDetection = self.detect
            keyAssembleKc = 1

        print 'max_num:', max_num
        vals = nm.empty(max_num, dtype=nm.float64)
        rows = nm.empty(max_num, dtype=nm.int32)
        cols = nm.empty(max_num, dtype=nm.int32)

        print '!!!detect', keyContactDetection
        aux = cc.assemble_contact_residual_and_stiffness(Gc, vals, rows, cols,
                                                         GPs, ISN, IEN,
                                                         X, Um, H, dH, gw,
                                                         activeGPs, neq, npd,
                                                         epss,
                                                         keyContactDetection,
                                                         keyAssembleKc)
        Gc, vals, rows, cols, num = aux
        # print Gc.mean()
        # print GPs
        print 'true num:', num
        print GPs.shape
        print activeGPs
        print gap

        # Uncomment this to detect only in the 1. iteration.
        #self.detect = max(0, self.detect - 1)
        if diff_var is None:
            from sfepy.discrete.variables import create_adof_conn
            rows = nm.unique(create_adof_conn(nm.arange(len(Gc)), sd.econn,
                                              nsd, 0))
            Gc = Gc[rows]

            eq = state.eq_map.eq
            erows = eq[rows]
            active = (erows >= 0)
            out_cc = (Gc[active], erows[active])

        else:
            vals, rows, cols = vals[:num], rows[:num], cols[:num]
            eq = state.eq_map.eq

            erows, ecols = eq[rows], eq[cols]
            active = (erows >= 0) & (ecols >= 0)
            out_cc = (vals[active], erows[active], ecols[active])

        return out_cc,
<|endoftext|>


<commit_before># This file is part of audioread.
# Copyright 2012, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Read audio data using the ffmpeg command line tools via a UNIX
pipe.
"""

import sys
import subprocess
import re
import threading
import select
import time
from Queue import Queue, Empty
from threading  import Thread

from . import DecodeError

class FFmpegError(DecodeError):
    pass

class CommunicationError(FFmpegError):
    """Raised when the output of FFmpeg is not parseable."""

class UnsupportedError(FFmpegError):
    """The file could not be decoded by FFmpeg."""

class NotInstalledError(FFmpegError):
    """Could not find the ffmpeg binary."""

class ReadTimeoutError(FFmpegError):
    """Reading from the ffmpeg command-line tool timed out."""

class ReaderThread(threading.Thread):
    """A thread that consumes data from a filehandle. This is used to ensure
    that a buffer for an input stream never fills up.
    """
    # It may seem a little hacky, but this is the most straightforward &
    # reliable way I can think of to do this. select() is sort of
    # inefficient because it doesn't indicate how much is available to
    # read -- so I end up reading character by character.
    def __init__(self, fh, blocksize=1024):
        super(ReaderThread, self).__init__()
        self.fh = fh
        self.blocksize = blocksize
        self.daemon = True
        self.data = []

    def run(self):
        while True:
            data = self.fh.read(self.blocksize)
            if not data:
                break
            self.data.append(data)

class FFmpegAudioFile(object):
    """An audio file decoded by the ffmpeg command-line utility."""
    def __init__(self, filename):
        try:
            self.proc = subprocess.Popen(
                ['ffmpeg', '-i', filename, '-f', 's16le', '-'],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1
            )
        except OSError:
            raise NotInstalledError()

        # Read relevant information from stderr.
        self._get_info()

        # Start a separate thread to read the rest of the data from
        # stderr.
        self.stderr_reader = ReaderThread(self.proc.stderr)
        self.stderr_reader.start()

    def read_data(self, block_size=4096, timeout=10.0):
        """Read blocks of raw PCM data from the file."""
        # Read from stdout on this thread.

	def enqueue_output(out, queue, block_size):
            # from http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
            data = None
            while True:
                data = out.read(block_size)
                queue.put(data)
                if not data:
                    break

        q = Queue()
        t = Thread(target=enqueue_output, args=(self.proc.stdout, q, block_size))
	t.daemon = True # thread dies with the program
	t.start()

        start_time = time.time()
        while True:
            # Wait for data to be available or a timeout.
            # rready, _, xready = select.select((self.proc.stdout,), (), (self.proc.stdout,), timeout)
            #############################################################################################
            data = None
            try:
                data = q.get(timeout = timeout)
                if data:
                    yield data
                else:
                    break
            except Empty:
                end_time = time.time()
                if not data:
                    if end_time - start_time >= timeout:
                        # Nothing interesting has happened for a while --
                        # FFmpeg is probably hanging.
                        raise ReadTimeoutError(
                            'ffmpeg output: %s' %
                            ''.join(self.stderr_reader.data)
                        )
                    else:
                        start_time = end_time
                        # Keep waiting.
                        continue

                #############################################################################################

    def _get_info(self):
        """Reads the tool's output from its stderr stream, extracts the
        relevant information, and parses it.
        """
        out_parts = []
        while True:
            line = self.proc.stderr.readline()
            if not line:
                # EOF and data not found.
                raise CommunicationError("stream info not found")

            # In Python 3, result of reading from stderr is bytes.
            if isinstance(line, bytes):
                line = line.decode('utf8', 'ignore')

            line = line.strip().lower()

            if 'no such file' in line:
                raise IOError('file not found')
            elif 'invalid data found' in line:
                raise UnsupportedError()
            elif 'duration:' in line:
                out_parts.append(line)
            elif 'audio:' in line:
                out_parts.append(line)
                self._parse_info(''.join(out_parts))
                break

    def _parse_info(self, s):
        """Given relevant data from the ffmpeg output, set audio
        parameter fields on this object.
        """
        # Sample rate.
        match = re.search(r'(\d+) hz', s)
        if match:
            self.samplerate = int(match.group(1))
        else:
            self.samplerate = 0

        # Channel count.
        match = re.search(r'hz, ([^,]+),', s)
        if match:
            mode = match.group(1)
            if mode == 'stereo':
                self.channels = 2
            else:
                match = re.match(r'(\d+) ', mode)
                if match:
                    self.channels = int(match.group(1))
                else:
                    self.channels = 1
        else:
            self.channels = 0

        # Duration.
        match = re.search(
            r'duration: (\d+):(\d+):(\d+).(\d)', s
        )
        if match:
            durparts = list(map(int, match.groups()))
            duration = durparts[0] * 60 * 60 + \
                       durparts[1] * 60 + \
                       durparts[2] + \
                       float(durparts[3]) / 10
            self.duration = duration
        else:
            # No duration found.
            self.duration = 0

    def close(self):
        """Close the ffmpeg process used to perform the decoding."""
        if hasattr(self, 'proc') and self.proc.returncode is None:
            self.proc.kill()
            # Flush the stdout buffer (stderr already flushed).
            stdout_reader = ReaderThread(self.proc.stdout)
            stdout_reader.start()
            self.proc.wait()

    def __del__(self):
        self.close()

    # Iteration.
    def __iter__(self):
        return self.read_data()

    # Context manager.
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()
        return False

<commit_msg>fix: remove tabs<commit_after># This file is part of audioread.
# Copyright 2012, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Read audio data using the ffmpeg command line tools via a UNIX
pipe.
"""

import sys
import subprocess
import re
import threading
import select
import time
from Queue import Queue, Empty
from threading  import Thread

from . import DecodeError

class FFmpegError(DecodeError):
    pass

class CommunicationError(FFmpegError):
    """Raised when the output of FFmpeg is not parseable."""

class UnsupportedError(FFmpegError):
    """The file could not be decoded by FFmpeg."""

class NotInstalledError(FFmpegError):
    """Could not find the ffmpeg binary."""

class ReadTimeoutError(FFmpegError):
    """Reading from the ffmpeg command-line tool timed out."""

class ReaderThread(threading.Thread):
    """A thread that consumes data from a filehandle. This is used to ensure
    that a buffer for an input stream never fills up.
    """
    # It may seem a little hacky, but this is the most straightforward &
    # reliable way I can think of to do this. select() is sort of
    # inefficient because it doesn't indicate how much is available to
    # read -- so I end up reading character by character.
    def __init__(self, fh, blocksize=1024):
        super(ReaderThread, self).__init__()
        self.fh = fh
        self.blocksize = blocksize
        self.daemon = True
        self.data = []

    def run(self):
        while True:
            data = self.fh.read(self.blocksize)
            if not data:
                break
            self.data.append(data)

class FFmpegAudioFile(object):
    """An audio file decoded by the ffmpeg command-line utility."""
    def __init__(self, filename):
        try:
            self.proc = subprocess.Popen(
                ['ffmpeg', '-i', filename, '-f', 's16le', '-'],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1
            )
        except OSError:
            raise NotInstalledError()

        # Read relevant information from stderr.
        self._get_info()

        # Start a separate thread to read the rest of the data from
        # stderr.
        self.stderr_reader = ReaderThread(self.proc.stderr)
        self.stderr_reader.start()

    def read_data(self, block_size=4096, timeout=10.0):
        """Read blocks of raw PCM data from the file."""
        # Read from stdout on this thread.

        def enqueue_output(out, queue, block_size):
            # from http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
            data = None
            while True:
                data = out.read(block_size)
                queue.put(data)
                if not data:
                    break

        q = Queue()
        t = Thread(target=enqueue_output, args=(self.proc.stdout, q, block_size))
        t.daemon = True # thread dies with the program
        t.start()

        start_time = time.time()
        while True:
            # Wait for data to be available or a timeout.
            # rready, _, xready = select.select((self.proc.stdout,), (), (self.proc.stdout,), timeout)
            #############################################################################################
            data = None
            try:
                data = q.get(timeout = timeout)
                if data:
                    yield data
                else:
                    break
            except Empty:
                end_time = time.time()
                if not data:
                    if end_time - start_time >= timeout:
                        # Nothing interesting has happened for a while --
                        # FFmpeg is probably hanging.
                        raise ReadTimeoutError(
                            'ffmpeg output: %s' %
                            ''.join(self.stderr_reader.data)
                        )
                    else:
                        start_time = end_time
                        # Keep waiting.
                        continue

                #############################################################################################

    def _get_info(self):
        """Reads the tool's output from its stderr stream, extracts the
        relevant information, and parses it.
        """
        out_parts = []
        while True:
            line = self.proc.stderr.readline()
            if not line:
                # EOF and data not found.
                raise CommunicationError("stream info not found")

            # In Python 3, result of reading from stderr is bytes.
            if isinstance(line, bytes):
                line = line.decode('utf8', 'ignore')

            line = line.strip().lower()

            if 'no such file' in line:
                raise IOError('file not found')
            elif 'invalid data found' in line:
                raise UnsupportedError()
            elif 'duration:' in line:
                out_parts.append(line)
            elif 'audio:' in line:
                out_parts.append(line)
                self._parse_info(''.join(out_parts))
                break

    def _parse_info(self, s):
        """Given relevant data from the ffmpeg output, set audio
        parameter fields on this object.
        """
        # Sample rate.
        match = re.search(r'(\d+) hz', s)
        if match:
            self.samplerate = int(match.group(1))
        else:
            self.samplerate = 0

        # Channel count.
        match = re.search(r'hz, ([^,]+),', s)
        if match:
            mode = match.group(1)
            if mode == 'stereo':
                self.channels = 2
            else:
                match = re.match(r'(\d+) ', mode)
                if match:
                    self.channels = int(match.group(1))
                else:
                    self.channels = 1
        else:
            self.channels = 0

        # Duration.
        match = re.search(
            r'duration: (\d+):(\d+):(\d+).(\d)', s
        )
        if match:
            durparts = list(map(int, match.groups()))
            duration = durparts[0] * 60 * 60 + \
                       durparts[1] * 60 + \
                       durparts[2] + \
                       float(durparts[3]) / 10
            self.duration = duration
        else:
            # No duration found.
            self.duration = 0

    def close(self):
        """Close the ffmpeg process used to perform the decoding."""
        if hasattr(self, 'proc') and self.proc.returncode is None:
            self.proc.kill()
            # Flush the stdout buffer (stderr already flushed).
            stdout_reader = ReaderThread(self.proc.stdout)
            stdout_reader.start()
            self.proc.wait()

    def __del__(self):
        self.close()

    # Iteration.
    def __iter__(self):
        return self.read_data()

    # Context manager.
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()
        return False

<|endoftext|>