# Path: cloudbio/custom/shared.py
# CBL_REPO_ROOT_URL = "https://raw.github.com/chapmanb/cloudbiolinux/master/"
# def chdir(new_dir):
# def safe_makedir(dname):
# def which(program, env=None):
#     def is_exe(fpath):
# def _if_not_installed(pname):
#     def argcatcher(func):
#         def decorator(*args, **kwargs):
# def _all_cbl_paths(env, ext):
# def _executable_not_on_path(pname):
# def _galaxy_tool_install(args):
# def _galaxy_tool_present(args):
# def _if_not_python_lib(library):
#     def argcatcher(func):
#         def decorator(*args, **kwargs):
# def make_tmp_dir_local(ext, work_dir):
# def _make_tmp_dir(ext=None, work_dir=None):
# def __work_dir():
# def _get_expected_file(url, dir_name=None, safe_tar=False, tar_file_name=None):
# def _safe_dir_name(dir_name, need_dir=True):
# def _remote_fetch(env, url, out_file=None, allow_fail=False, fix_fn=None, samedir=False):
# def _fetch_and_unpack(url, need_dir=True, dir_name=None, revision=None,
#                       safe_tar=False, tar_file_name=None):
# def _configure_make(env):
# def _ac_configure_make(env):
# def _make_copy(find_cmd=None, premake_cmd=None, do_make=True):
#     def _do_work(env):
# def _get_install(url, env, make_command, post_unpack_fn=None, revision=None, dir_name=None,
#                  safe_tar=False, tar_file_name=None):
# def _apply_patch(env, url):
# def _get_install_local(url, env, make_command, dir_name=None,
#                        post_unpack_fn=None, safe_tar=False, tar_file_name=None):
# def _symlinked_install_dir(pname, version, env, extra_dir=None):
# def _symlinked_dir_exists(pname, version, env, extra_dir=None):
# def _symlinked_shared_dir(pname, version, env, extra_dir=None):
# def _symlinked_java_version_dir(pname, version, env):
# def _java_install(pname, version, url, env, install_fn=None,
#                   pre_fetch_fn=None):
# def _python_cmd(env):
# def _pip_cmd(env):
# def _conda_cmd(env):
# def _is_anaconda(env):
# def _python_make(env):
# def _get_installed_file(env, local_file):
# def _get_installed_file_contents(env, local_file):
# def _write_to_file(contents, path, mode):
# def _get_bin_dir(env):
# def _get_include_dir(env):
# def _get_lib_dir(env):
# def _get_install_subdir(env, subdir):
# def _set_default_config(env, install_dir, sym_dir_name="default"):
# def _setup_simple_service(service_name):
# def _render_config_file_template(env, name, defaults={}, overrides={}, default_source=None):
# def _extend_env(env, defaults={}, overrides={}):
# def _setup_conf_file(env, dest, name, defaults={}, overrides={}, default_source=None, mode="0755"):
# def _add_to_profiles(line, profiles=[], use_sudo=True):
# def install_venvburrito():
# def _create_python_virtualenv(env, venv_name, reqs_file=None, reqs_url=None):
#     def create():
# def _create_local_python_virtualenv(env, venv_name, reqs_file, reqs_url):
# def _create_global_python_virtualenv(env, venv_name, reqs_file, reqs_url):
# def _get_bitbucket_download_url(revision, default_repo):
# def _read_boolean(env, name, default):


import os
import shutil
import subprocess
from xml.etree import ElementTree
from cloudbio.custom import shared
def _build_galaxy_loc_line(env, dbkey, file_path, config, prefix, tool_name):
    """Prepare genome information to write to a Galaxy *.loc config file.
    """
    if tool_name:
        str_parts = []
        tool_conf = _get_tool_conf(env, tool_name)
        loc_cols = LocCols(config, dbkey, file_path)
        # Compose the .loc file line as str_parts list by looking for column values
        # from the retrieved tool_conf (as defined in tool_data_table_conf.xml).
        # Any column values required but missing in the tool_conf are
        # supplemented by the defaults defined in LocCols class
        for col in tool_conf.get('columns', []):
            str_parts.append(config.get(col, getattr(loc_cols, col)))
    else:
        str_parts = [dbkey, file_path]
    if prefix:
        str_parts.insert(0, prefix)
    return str_parts

def update_loc_file(env, ref_file, line_parts):
    """Add a reference to the given genome to the base index file.
    """
    if getattr(env, "galaxy_home", None) is not None:
        tools_dir = os.path.join(env.galaxy_home, "tool-data")
        if not os.path.exists(tools_dir):
            subprocess.check_call("mkdir -p %s" % tools_dir, shell=True)
        dt_file = os.path.join(env.galaxy_home, "tool_data_table_conf.xml")
        if not os.path.exists(dt_file):
            shutil.copy(env.tool_data_table_conf_file, dt_file)
        add_str = "\t".join(line_parts)

# Path: cloudbio/custom/shared.py
# CBL_REPO_ROOT_URL = "https://raw.github.com/chapmanb/cloudbiolinux/master/"
# def chdir(new_dir):
# def safe_makedir(dname):
# def which(program, env=None):
#     def is_exe(fpath):
# def _if_not_installed(pname):
#     def argcatcher(func):
#         def decorator(*args, **kwargs):
# def _all_cbl_paths(env, ext):
# def _executable_not_on_path(pname):
# def _galaxy_tool_install(args):
# def _galaxy_tool_present(args):
# def _if_not_python_lib(library):
#     def argcatcher(func):
#         def decorator(*args, **kwargs):
# def make_tmp_dir_local(ext, work_dir):
# def _make_tmp_dir(ext=None, work_dir=None):
# def __work_dir():
# def _get_expected_file(url, dir_name=None, safe_tar=False, tar_file_name=None):
# def _safe_dir_name(dir_name, need_dir=True):
# def _remote_fetch(env, url, out_file=None, allow_fail=False, fix_fn=None, samedir=False):
# def _fetch_and_unpack(url, need_dir=True, dir_name=None, revision=None,
#                       safe_tar=False, tar_file_name=None):
# def _configure_make(env):
# def _ac_configure_make(env):
# def _make_copy(find_cmd=None, premake_cmd=None, do_make=True):
#     def _do_work(env):
# def _get_install(url, env, make_command, post_unpack_fn=None, revision=None, dir_name=None,
#                  safe_tar=False, tar_file_name=None):
# def _apply_patch(env, url):
# def _get_install_local(url, env, make_command, dir_name=None,
#                        post_unpack_fn=None, safe_tar=False, tar_file_name=None):
# def _symlinked_install_dir(pname, version, env, extra_dir=None):
# def _symlinked_dir_exists(pname, version, env, extra_dir=None):
# def _symlinked_shared_dir(pname, version, env, extra_dir=None):
# def _symlinked_java_version_dir(pname, version, env):
# def _java_install(pname, version, url, env, install_fn=None,
#                   pre_fetch_fn=None):
# def _python_cmd(env):
# def _pip_cmd(env):
# def _conda_cmd(env):
# def _is_anaconda(env):
# def _python_make(env):
# def _get_installed_file(env, local_file):
# def _get_installed_file_contents(env, local_file):
# def _write_to_file(contents, path, mode):
# def _get_bin_dir(env):
# def _get_include_dir(env):
# def _get_lib_dir(env):
# def _get_install_subdir(env, subdir):
# def _set_default_config(env, install_dir, sym_dir_name="default"):
# def _setup_simple_service(service_name):
# def _render_config_file_template(env, name, defaults={}, overrides={}, default_source=None):
# def _extend_env(env, defaults={}, overrides={}):
# def _setup_conf_file(env, dest, name, defaults={}, overrides={}, default_source=None, mode="0755"):
# def _add_to_profiles(line, profiles=[], use_sudo=True):
# def install_venvburrito():
# def _create_python_virtualenv(env, venv_name, reqs_file=None, reqs_url=None):
#     def create():
# def _create_local_python_virtualenv(env, venv_name, reqs_file, reqs_url):
# def _create_global_python_virtualenv(env, venv_name, reqs_file, reqs_url):
# def _get_bitbucket_download_url(revision, default_repo):
# def _read_boolean(env, name, default):


# Path: cloudbio/biodata/galaxy.py
import os
import shutil
import subprocess
from xml.etree import ElementTree
from cloudbio.custom import shared

def _build_galaxy_loc_line(env, dbkey, file_path, config, prefix, tool_name):
    """Prepare genome information to write to a Galaxy *.loc config file.
    """
    if tool_name:
        str_parts = []
        tool_conf = _get_tool_conf(env, tool_name)
        loc_cols = LocCols(config, dbkey, file_path)
        # Compose the .loc file line as str_parts list by looking for column values
        # from the retrieved tool_conf (as defined in tool_data_table_conf.xml).
        # Any column values required but missing in the tool_conf are
        # supplemented by the defaults defined in LocCols class
        for col in tool_conf.get('columns', []):
            str_parts.append(config.get(col, getattr(loc_cols, col)))
    else:
        str_parts = [dbkey, file_path]
    if prefix:
        str_parts.insert(0, prefix)
    return str_parts

def update_loc_file(env, ref_file, line_parts):
    """Add a reference to the given genome to the base index file.
    """
    if getattr(env, "galaxy_home", None) is not None:
        tools_dir = os.path.join(env.galaxy_home, "tool-data")
        if not os.path.exists(tools_dir):
            subprocess.check_call("mkdir -p %s" % tools_dir, shell=True)
        dt_file = os.path.join(env.galaxy_home, "tool_data_table_conf.xml")
        if not os.path.exists(dt_file):
            shutil.copy(env.tool_data_table_conf_file, dt_file)
        add_str = "\t".join(line_parts)

        with shared.chdir(tools_dir):