# Path: cloudbio/custom/shared.py
# def _set_default_config(env, install_dir, sym_dir_name="default"):
#     """
#     Sets up default galaxy config directory symbolic link (if needed). Needed
#     when it doesn't exists or when installing a new version of software.
#     """
#     version = env["tool_version"]
#     if env.safe_exists(install_dir):
#         install_dir_root = "%s/.." % install_dir
#         sym_dir = "%s/%s" % (install_dir_root, sym_dir_name)
#         replace_default = False
#         if not env.safe_exists(sym_dir):
#             replace_default = True
#         if not replace_default:
#             default_version = env.safe_sudo("basename `readlink -f %s`" % sym_dir)
#             if version > default_version:  # Bug: Wouldn't work for 1.9 < 1.10
#                 print("default version %s is older than version %s just installed, replacing..." % (default_version, version))
#                 replace_default = True
#         if replace_default:
#             env.safe_sudo("rm -rf %s; ln -f -s %s %s" % (sym_dir, install_dir, sym_dir))
# 
# def _add_to_profiles(line, profiles=[], use_sudo=True):
#     """
#     If it's not already there, append ``line`` to shell profiles files.
#     By default, these are ``/etc/profile`` and ``/etc/bash.bashrc`` but can be
#     overridden by providing a list of file paths to the ``profiles`` argument.
#     """
#     if not profiles:
#         profiles = ['/etc/bash.bashrc', '/etc/profile']
#     for profile in profiles:
#         if not env.safe_contains(profile, line):
#             env.safe_append(profile, line, use_sudo=use_sudo)


import os
import six
import yaml
from string import Template
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
def _tools_conf_path(env):
    """
    Load path to galaxy_tools_conf file from env, allowing expansion of $__contrib_dir__.
    Default to $__contrib_dir__/flavor/cloudman/tools.yaml.
    """
    contrib_dir = os.path.join(env.config_dir, os.pardir, "contrib")
    default_tools_conf_path = os.path.join(contrib_dir, "flavor", "cloudman", "tools.yaml")
    tools_conf_path = env.get("galaxy_tools_conf", default_tools_conf_path)
    ## Allow expansion of __config_dir__ in galaxy_tools_conf property.
    return Template(tools_conf_path).safe_substitute({"__contrib_dir__": contrib_dir})


def _load_tools_conf(env):
    with open(_tools_conf_path(env)) as in_handle:
        full_data = yaml.safe_load(in_handle)
    return full_data


def _setup_install_dir(env):
    """Sets up install dir and ensures its owned by Galaxy"""
    if not env.safe_exists(env.galaxy_tools_dir):
        env.safe_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 env.safe_exists(bin_dir):
        env.safe_sudo("mkdir -p %s" % bin_dir)
        _chown_galaxy(env, bin_dir)
        line = "export PATH={0}:$PATH".format(bin_dir)

# Path: cloudbio/custom/shared.py
# def _set_default_config(env, install_dir, sym_dir_name="default"):
#     """
#     Sets up default galaxy config directory symbolic link (if needed). Needed
#     when it doesn't exists or when installing a new version of software.
#     """
#     version = env["tool_version"]
#     if env.safe_exists(install_dir):
#         install_dir_root = "%s/.." % install_dir
#         sym_dir = "%s/%s" % (install_dir_root, sym_dir_name)
#         replace_default = False
#         if not env.safe_exists(sym_dir):
#             replace_default = True
#         if not replace_default:
#             default_version = env.safe_sudo("basename `readlink -f %s`" % sym_dir)
#             if version > default_version:  # Bug: Wouldn't work for 1.9 < 1.10
#                 print("default version %s is older than version %s just installed, replacing..." % (default_version, version))
#                 replace_default = True
#         if replace_default:
#             env.safe_sudo("rm -rf %s; ln -f -s %s %s" % (sym_dir, install_dir, sym_dir))
# 
# def _add_to_profiles(line, profiles=[], use_sudo=True):
#     """
#     If it's not already there, append ``line`` to shell profiles files.
#     By default, these are ``/etc/profile`` and ``/etc/bash.bashrc`` but can be
#     overridden by providing a list of file paths to the ``profiles`` argument.
#     """
#     if not profiles:
#         profiles = ['/etc/bash.bashrc', '/etc/profile']
#     for profile in profiles:
#         if not env.safe_contains(profile, line):
#             env.safe_append(profile, line, use_sudo=use_sudo)


# Path: cloudbio/galaxy/tools.py
import os
import six
import yaml
from string import Template
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

def _tools_conf_path(env):
    """
    Load path to galaxy_tools_conf file from env, allowing expansion of $__contrib_dir__.
    Default to $__contrib_dir__/flavor/cloudman/tools.yaml.
    """
    contrib_dir = os.path.join(env.config_dir, os.pardir, "contrib")
    default_tools_conf_path = os.path.join(contrib_dir, "flavor", "cloudman", "tools.yaml")
    tools_conf_path = env.get("galaxy_tools_conf", default_tools_conf_path)
    ## Allow expansion of __config_dir__ in galaxy_tools_conf property.
    return Template(tools_conf_path).safe_substitute({"__contrib_dir__": contrib_dir})


def _load_tools_conf(env):
    with open(_tools_conf_path(env)) as in_handle:
        full_data = yaml.safe_load(in_handle)
    return full_data


def _setup_install_dir(env):
    """Sets up install dir and ensures its owned by Galaxy"""
    if not env.safe_exists(env.galaxy_tools_dir):
        env.safe_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 env.safe_exists(bin_dir):
        env.safe_sudo("mkdir -p %s" % bin_dir)
        _chown_galaxy(env, bin_dir)
        line = "export PATH={0}:$PATH".format(bin_dir)

        _add_to_profiles(line)