# Path: cloudbio/package/shared.py
# def _yaml_to_packages(yaml_file, to_install=None, subs_yaml_file=None, namesort=True, env=None):
#     """Read a list of packages from a nested YAML configuration file.
#     """
#     print("Reading packages from %s" % yaml_file)
#     with open(yaml_file) as in_handle:
#         full_data = yaml.safe_load(in_handle)
#         if full_data is None:
#             full_data = {}
#     if subs_yaml_file is not None:
#         with open(subs_yaml_file) as in_handle:
#             subs = yaml.safe_load(in_handle)
#     else:
#         subs = {}
#     # filter the data based on what we have configured to install
#     data = [(k, v) for (k, v) in full_data.items()
#             if (to_install is None or k in to_install) and k not in ["channels"]]
#     data.sort()
#     packages = []
#     pkg_to_group = dict()
#     while len(data) > 0:
#         cur_key, cur_info = data.pop(0)
#         if cur_info:
#             if isinstance(cur_info, (list, tuple)):
#                 packages.extend(_filter_subs_packages(cur_info, subs, namesort))
#                 for p in cur_info:
#                     pkg_to_group[p] = cur_key
#             elif isinstance(cur_info, dict):
#                 for key, val in cur_info.items():
#                     # if we are okay, propagate with the top level key
#                     if env and key == 'needs_64bit':
#                         if env.is_64bit:
#                             data.insert(0, (cur_key, val))
#                     elif env and key.startswith(env.distribution):
#                         if key.endswith(env.dist_name):
#                             data.insert(0, (cur_key, val))
#                     else:
#                         data.insert(0, (cur_key, val))
#             else:
#                 raise ValueError(cur_info)
#     return packages, pkg_to_group


import collections
import json
import os
import shutil
import subprocess
import yaml
from cloudbio.package.shared import _yaml_to_packages
    from cloudbio.flavor.config import get_config_file
    from cloudbio.custom import shared
    py_version = ENV_PY_VERSIONS[env_name]
    if "deepvariant" in env_packages:
        # Ignore /etc/boto.cfg which creates conflicts with conda gsutils
        # https://github.com/GoogleCloudPlatform/gsutil/issues/516
        exports = "export BOTO_CONFIG=/ignoreglobal && "
    else:
        exports = ""
    if os.path.exists(mamba_bin):
        try:
            subprocess.check_call("{mamba_bin} install -q -y {env_str} {channels} "
                                  "{py_version} {pkgs_str}".format(**locals()), shell=True)
        except subprocess.CalledProcessError:
            # Fall back to standard conda install when we have system specific issues
            # https://github.com/bcbio/bcbio-nextgen/issues/2871
            subprocess.check_call("{exports}{conda_bin} install -q -y {env_str} {channels} "
                                "{py_version} {pkgs_str}".format(**locals()), shell=True)
    conda_pkg_list = json.loads(subprocess.check_output(
        "{conda_bin} list --json {env_str} -q".format(**locals()), shell=True))
    return conda_pkg_list

def install_in(conda_bin, system_installdir, config_file=None, packages=None):
    """Install packages inside a given anaconda directory.

    New approach, local only and not dependent on fabric.
    conda_bin could refer to mamba
    """
    if config_file is None and packages is None:
        packages = []
        check_channels = []
    else:

# Path: cloudbio/package/shared.py
# def _yaml_to_packages(yaml_file, to_install=None, subs_yaml_file=None, namesort=True, env=None):
#     """Read a list of packages from a nested YAML configuration file.
#     """
#     print("Reading packages from %s" % yaml_file)
#     with open(yaml_file) as in_handle:
#         full_data = yaml.safe_load(in_handle)
#         if full_data is None:
#             full_data = {}
#     if subs_yaml_file is not None:
#         with open(subs_yaml_file) as in_handle:
#             subs = yaml.safe_load(in_handle)
#     else:
#         subs = {}
#     # filter the data based on what we have configured to install
#     data = [(k, v) for (k, v) in full_data.items()
#             if (to_install is None or k in to_install) and k not in ["channels"]]
#     data.sort()
#     packages = []
#     pkg_to_group = dict()
#     while len(data) > 0:
#         cur_key, cur_info = data.pop(0)
#         if cur_info:
#             if isinstance(cur_info, (list, tuple)):
#                 packages.extend(_filter_subs_packages(cur_info, subs, namesort))
#                 for p in cur_info:
#                     pkg_to_group[p] = cur_key
#             elif isinstance(cur_info, dict):
#                 for key, val in cur_info.items():
#                     # if we are okay, propagate with the top level key
#                     if env and key == 'needs_64bit':
#                         if env.is_64bit:
#                             data.insert(0, (cur_key, val))
#                     elif env and key.startswith(env.distribution):
#                         if key.endswith(env.dist_name):
#                             data.insert(0, (cur_key, val))
#                     else:
#                         data.insert(0, (cur_key, val))
#             else:
#                 raise ValueError(cur_info)
#     return packages, pkg_to_group


# Path: cloudbio/package/conda.py
import collections
import json
import os
import shutil
import subprocess
import yaml
from cloudbio.package.shared import _yaml_to_packages
    from cloudbio.flavor.config import get_config_file
    from cloudbio.custom import shared

    py_version = ENV_PY_VERSIONS[env_name]
    if "deepvariant" in env_packages:
        # Ignore /etc/boto.cfg which creates conflicts with conda gsutils
        # https://github.com/GoogleCloudPlatform/gsutil/issues/516
        exports = "export BOTO_CONFIG=/ignoreglobal && "
    else:
        exports = ""
    if os.path.exists(mamba_bin):
        try:
            subprocess.check_call("{mamba_bin} install -q -y {env_str} {channels} "
                                  "{py_version} {pkgs_str}".format(**locals()), shell=True)
        except subprocess.CalledProcessError:
            # Fall back to standard conda install when we have system specific issues
            # https://github.com/bcbio/bcbio-nextgen/issues/2871
            subprocess.check_call("{exports}{conda_bin} install -q -y {env_str} {channels} "
                                "{py_version} {pkgs_str}".format(**locals()), shell=True)
    conda_pkg_list = json.loads(subprocess.check_output(
        "{conda_bin} list --json {env_str} -q".format(**locals()), shell=True))
    return conda_pkg_list

def install_in(conda_bin, system_installdir, config_file=None, packages=None):
    """Install packages inside a given anaconda directory.

    New approach, local only and not dependent on fabric.
    conda_bin could refer to mamba
    """
    if config_file is None and packages is None:
        packages = []
        check_channels = []
    else:

        (packages, _) = _yaml_to_packages(config_file)