Ejemplo n.º 1
0
def ensure_executables_in_path_or_raise(executables, abstract_spec):
    """Ensure that some executables are in path or raise.

    Args:
        executables (list): list of executables to be searched in the PATH,
            in order. The function exits on the first one found.
        abstract_spec (str): abstract spec that provides the executables

    Raises:
        RuntimeError: if the executables cannot be ensured to be in PATH

    Return:
        Executable object
    """
    cmd = spack.util.executable.which(*executables)
    if cmd:
        return cmd

    executables_str = ', '.join(executables)
    source_configs = spack.config.get('bootstrap:sources', [])

    h = GroupedExceptionHandler()

    for current_config in source_configs:
        with h.forward(current_config['name']):
            _validate_source_is_trusted(current_config)

            b = _make_bootstrapper(current_config)
            if b.try_search_path(executables, abstract_spec):
                # Additional environment variables needed
                concrete_spec, cmd = b.last_search['spec'], b.last_search[
                    'command']
                env_mods = spack.util.environment.EnvironmentModifications()
                for dep in concrete_spec.traverse(root=True,
                                                  order='post',
                                                  deptype=('link', 'run')):
                    env_mods.extend(
                        spack.user_environment.
                        environment_modifications_for_spec(
                            dep, set_package_py_globals=False))
                cmd.add_default_envmod(env_mods)
                return cmd

    assert h, 'expected at least one exception to have been raised at this point: while bootstrapping {0}'.format(
        executables_str)  # noqa: E501
    msg = 'cannot bootstrap any of the {0} executables '.format(
        executables_str)
    if abstract_spec:
        msg += 'from spec "{0}" '.format(abstract_spec)
    if tty.is_debug():
        msg += h.grouped_message(with_tracebacks=True)
    else:
        msg += h.grouped_message(with_tracebacks=False)
        msg += '\nRun `spack --debug ...` for more detailed errors'
    raise RuntimeError(msg)
Ejemplo n.º 2
0
def ensure_module_importable_or_raise(module, abstract_spec=None):
    """Make the requested module available for import, or raise.

    This function tries to import a Python module in the current interpreter
    using, in order, the methods configured in bootstrap.yaml.

    If none of the methods succeed, an exception is raised. The function exits
    on first success.

    Args:
        module (str): module to be imported in the current interpreter
        abstract_spec (str): abstract spec that might provide the module. If not
            given it defaults to "module"

    Raises:
        ImportError: if the module couldn't be imported
    """
    # If we can import it already, that's great
    tty.debug(
        "[BOOTSTRAP MODULE {0}] Try importing from Python".format(module))
    if _python_import(module):
        return

    abstract_spec = abstract_spec or module
    source_configs = spack.config.get('bootstrap:sources', [])

    h = GroupedExceptionHandler()

    for current_config in source_configs:
        with h.forward(current_config['name']):
            _validate_source_is_trusted(current_config)

            b = _make_bootstrapper(current_config)
            if b.try_import(module, abstract_spec):
                return

    assert h, 'expected at least one exception to have been raised at this point: while bootstrapping {0}'.format(
        module)  # noqa: E501
    msg = 'cannot bootstrap the "{0}" Python module '.format(module)
    if abstract_spec:
        msg += 'from spec "{0}" '.format(abstract_spec)
    if tty.is_debug():
        msg += h.grouped_message(with_tracebacks=True)
    else:
        msg += h.grouped_message(with_tracebacks=False)
        msg += '\nRun `spack --debug ...` for more detailed errors'
    raise ImportError(msg)