<commit_before>import os
from string import Template

import yaml

from cloudbio.custom.bio_general import *
from cloudbio.custom.bio_nextgen import *
from cloudbio.custom.bio_proteomics import *
from cloudbio.custom.shared import _set_default_config, _add_to_profiles
from cloudbio.galaxy.applications import *
from cloudbio.galaxy.r import _install_r_packages
from cloudbio.galaxy.utils import _chown_galaxy, _read_boolean

from fabric.api import sudo
from fabric.contrib.files import exists


def _install_tools(env, tools_conf=None):
    """
    Install tools needed for Galaxy along with tool configuration
    directories needed by Galaxy.
    """

    if not tools_conf:
        tools_conf = _load_tools_conf(env)

    if _read_boolean(env, "galaxy_install_dependencies", False):
       # Need to ensure the install dir exists and is owned by env.galaxy_user
        _setup_install_dir(enconfigured_v)
        _install_configured_applications(env, tools_conf)
        _chown_galaxy(env, env.galaxy_tools_dir)
        _chown_galaxy(env, env.galaxy_jars_dir)

    if _read_boolean(env, "galaxy_install_r_packages", False):
        _install_r_packages(tools_conf)


def _load_tools_conf(env):
    tools_conf_path = env.get("galaxy_tools_conf",
                              os.path.join(env.config_dir, os.pardir,
                                           "contrib", "flavor", "cloudman", "tools.yaml"))
    with open(tools_conf_path) as in_handle:
        full_data = yaml.load(in_handle)
    return full_data


def _setup_install_dir(env):
    """Sets up install dir and ensures its owned by Galaxy"""
    if not exists(env.galaxy_tools_dir):
        sudo("mkdir -p %s" % env.galaxy_tools_dir)
        _chown_galaxy(env, env.galaxy_tools_dir)
    # Create a general-purpose ``bin`` directory under the galaxy_tools_dir
    # and put it on the PATH so users can more easily add custom tools
    bin_dir = os.path.join(env.galaxy_tools_dir, 'bin')
    if not exists(bin_dir):
        sudo("mkdir -p %s" % bin_dir)
        _chown_galaxy(env, bin_dir)
        line = "export PATH={0}:$PATH".format(bin_dir)
        _add_to_profiles(line)
    if not exists(env.galaxy_jars_dir):
        sudo("mkdir -p %s" % env.galaxy_jars_dir)
        _chown_galaxy(env, env.galaxy_jars_dir)


def _install_configured_applications(env, tools_conf):
    """
    Install external tools defined by YAML or dictionary data structure.  Instead of
    installing in system_install (e.g. /usr), these custom tools will be installed as
    Galaxy dependency applications.
    """
    applications = tools_conf["applications"] or {}
    for (name, tool_conf) in applications.iteritems():
        _install_application(name, tool_conf)


def _install_application(name, versions):
    """
    Install single custom tool as Galaxy dependency application.

    TODO: Rename versions and document options.
    """
    if type(versions) is str:
        versions = [versions]
    for version_info in versions:
        if type(version_info) is str:
            _install_tool(env, name, version=version_info, requirement_name=name)
        else:
            version = version_info["version"]
            bin_dirs = version_info.get("bin_dirs", ["bin"])
            env_vars = version_info.get("env_vars", {})
            provides = version_info.get("provides", [])
            if isinstance(provides, (str, unicode, basestring)):
                provides = [provides]

            # Some requirements (e.g. blast+) maybe not have valid python
            # identifiers as name. Use install_blast to setup but override
            # requirement directory name with requirement_name field.
            requirement_name = version_info.get("requirement_name", name)<commit_msg>Galaxy: Add improved alert message when a Galaxy application fails to install.<commit_after>import os
from string import Template

import yaml

from cloudbio.custom.bio_general import *
from cloudbio.custom.bio_nextgen import *
from cloudbio.custom.bio_proteomics import *
from cloudbio.custom.shared import _set_default_config, _add_to_profiles
from cloudbio.galaxy.applications import *
from cloudbio.galaxy.r import _install_r_packages
from cloudbio.galaxy.utils import _chown_galaxy, _read_boolean

from fabric.api import sudo
from fabric.contrib.files import exists

FAILED_INSTALL_MESSAGE = \
    "Failed to install application %s as a Galaxy application. This may be a transient problem (e.g. mirror for download is currently unavailable) or misconfiguration. The contents of the CloudBioLinux temporary working directory may need to be deleted."


def _install_tools(env, tools_conf=None):
    """
    Install tools needed for Galaxy along with tool configuration
    directories needed by Galaxy.
    """

    if not tools_conf:
        tools_conf = _load_tools_conf(env)

    if _read_boolean(env, "galaxy_install_dependencies", False):
       # Need to ensure the install dir exists and is owned by env.galaxy_user
        _setup_install_dir(env)
        _install_configured_applications(env, tools_conf)
        _chown_galaxy(env, env.galaxy_tools_dir)
        _chown_galaxy(env, env.galaxy_jars_dir)

    if _read_boolean(env, "galaxy_install_r_packages", False):
        _install_r_packages(tools_conf)


def _load_tools_conf(env):
    tools_conf_path = env.get("galaxy_tools_conf",
                              os.path.join(env.config_dir, os.pardir,
                                           "contrib", "flavor", "cloudman", "tools.yaml"))
    with open(tools_conf_path) as in_handle:
        full_data = yaml.load(in_handle)
    return full_data


def _setup_install_dir(env):
    """Sets up install dir and ensures its owned by Galaxy"""
    if not exists(env.galaxy_tools_dir):
        sudo("mkdir -p %s" % env.galaxy_tools_dir)
        _chown_galaxy(env, env.galaxy_tools_dir)
    # Create a general-purpose ``bin`` directory under the galaxy_tools_dir
    # and put it on the PATH so users can more easily add custom tools
    bin_dir = os.path.join(env.galaxy_tools_dir, 'bin')
    if not exists(bin_dir):
        sudo("mkdir -p %s" % bin_dir)
        _chown_galaxy(env, bin_dir)
        line = "export PATH={0}:$PATH".format(bin_dir)
        _add_to_profiles(line)
    if not exists(env.galaxy_jars_dir):
        sudo("mkdir -p %s" % env.galaxy_jars_dir)
        _chown_galaxy(env, env.galaxy_jars_dir)


def _install_configured_applications(env, tools_conf):
    """
    Install external tools defined by YAML or dictionary data structure.  Instead of
    installing in system_install (e.g. /usr), these custom tools will be installed as
    Galaxy dependency applications.
    """
    applications = tools_conf["applications"] or {}
    for (name, tool_conf) in applications.iteritems():
        try:
            _install_application(name, tool_conf)
        except:
            env.logger.warn(FAILED_INSTALL_MESSAGE % name)
            raise


def _install_application(name, versions):
    """
    Install single custom tool as Galaxy dependency application.

    TODO: Rename versions and document options.
    """
    if type(versions) is str:
        versions = [versions]
    for version_info in versions:
        if type(version_info) is str:
            _install_tool(env, name, version=version_info, requirement_name=name)
        else:
            version = version_info["version"]
            bin_dirs = version_info.get("bin_dirs", ["bin"])
            env_vars = version_info.get("env_vars", {})
            provides = version_info.get("provides", [])
            if isinstance(provides, (str, unicode, basestring)):
                provides = [provides]

            # Some requirements (e.g. blast+) maybe not have valid python
            # identifiers as name. Use install_blast to setup but override
            # requirement directory name with requirement_name field.
            requirement_name = version_info.get("requirement_name", name)
{'fraction_non_alphanumeric': 0.08361710177828226, 'fraction_numerical': 0.0002522386177323748, 'mean_word_length': 3.160545645330535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625141', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2314, 'n_words': 645}