# 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


from fabric.api import *
from fabric.contrib.files import *
from cloudbio.package.shared import _yaml_to_packages
from cloudbio.flavor.config import get_config_file
"""
Automated installation on debian package systems with apt.
"""



def _apt_packages(to_install=None, pkg_list=None):
    """
    Install packages available via apt-get.
    Note that ``to_install`` and ``pkg_list`` arguments cannot be used simultaneously.

    :type to_install:  list
    :param to_install: A list of strings (ie, groups) present in the ``main.yaml``
                       config file that will be used to filter out the specific
                       packages to be installed.

    :type pkg_list:  list
    :param pkg_list: An explicit list of packages to install. No other files,
                     flavors are considered.
    """
    if "minimal" not in env.flavor.short_name:
        env.logger.info("Update the system")
        with settings(warn_only=True):
            env.safe_sudo("apt-get update")
    if to_install is not None:
        config_file = get_config_file(env, "packages.yaml")
        if "minimal" not in env.flavor.name and "minimal" not in env.flavor.short_name:
            env.flavor.apt_upgrade_system(env=env)

# 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/deb.py
from fabric.api import *
from fabric.contrib.files import *
from cloudbio.package.shared import _yaml_to_packages
from cloudbio.flavor.config import get_config_file

"""
Automated installation on debian package systems with apt.
"""



def _apt_packages(to_install=None, pkg_list=None):
    """
    Install packages available via apt-get.
    Note that ``to_install`` and ``pkg_list`` arguments cannot be used simultaneously.

    :type to_install:  list
    :param to_install: A list of strings (ie, groups) present in the ``main.yaml``
                       config file that will be used to filter out the specific
                       packages to be installed.

    :type pkg_list:  list
    :param pkg_list: An explicit list of packages to install. No other files,
                     flavors are considered.
    """
    if "minimal" not in env.flavor.short_name:
        env.logger.info("Update the system")
        with settings(warn_only=True):
            env.safe_sudo("apt-get update")
    if to_install is not None:
        config_file = get_config_file(env, "packages.yaml")
        if "minimal" not in env.flavor.name and "minimal" not in env.flavor.short_name:
            env.flavor.apt_upgrade_system(env=env)

        (packages, _) = _yaml_to_packages(config_file.base, to_install, config_file.dist)