示例#1
0
文件: tools.py 项目: trogeat/yunohost
def tools_update(apps=False, system=False):
    """
    Update apps & system package cache

    Keyword arguments:
        system -- Fetch available system packages upgrades (equivalent to apt update)
        apps -- Fetch the application list to check which apps can be upgraded
    """

    # If neither --apps nor --system specified, do both
    if not apps and not system:
        apps = True
        system = True

    upgradable_system_packages = []
    if system:

        # Update APT cache
        # LC_ALL=C is here to make sure the results are in english
        command = "LC_ALL=C apt-get update -o Acquire::Retries=3"

        # Filter boring message about "apt not having a stable CLI interface"
        # Also keep track of wether or not we encountered a warning...
        warnings = []

        def is_legit_warning(m):
            legit_warning = (m.rstrip()
                             and "apt does not have a stable CLI interface"
                             not in m.rstrip())
            if legit_warning:
                warnings.append(m)
            return legit_warning

        callbacks = (
            # stdout goes to debug
            lambda l: logger.debug(l.rstrip()),
            # stderr goes to warning except for the boring apt messages
            lambda l: logger.warning(l.rstrip())
            if is_legit_warning(l) else logger.debug(l.rstrip()),
        )

        logger.info(m18n.n("updating_apt_cache"))

        returncode = call_async_output(command, callbacks, shell=True)

        if returncode != 0:
            raise YunohostError("update_apt_cache_failed",
                                sourceslist="\n".join(_dump_sources_list()))
        elif warnings:
            logger.error(
                m18n.n(
                    "update_apt_cache_warning",
                    sourceslist="\n".join(_dump_sources_list()),
                ))

        upgradable_system_packages = list(_list_upgradable_apt_packages())
        logger.debug(m18n.n("done"))

    upgradable_apps = []
    if apps:
        try:
            _update_apps_catalog()
        except YunohostError as e:
            logger.error(str(e))

        upgradable_apps = list(_list_upgradable_apps())

    if len(upgradable_apps) == 0 and len(upgradable_system_packages) == 0:
        logger.info(m18n.n("already_up_to_date"))

    return {"system": upgradable_system_packages, "apps": upgradable_apps}
示例#2
0
def tools_update(target=None, apps=False, system=False):
    """
    Update apps & system package cache
    """

    # Legacy options (--system, --apps)
    if apps or system:
        logger.warning(
            "Using 'yunohost tools update' with --apps / --system is deprecated, just write 'yunohost tools update apps system' (no -- prefix anymore)"
        )
        if apps and system:
            target = "all"
        elif apps:
            target = "apps"
        else:
            target = "system"

    elif not target:
        target = "all"

    if target not in ["system", "apps", "all"]:
        raise YunohostError(
            "Unknown target %s, should be 'system', 'apps' or 'all'" % target,
            raw_msg=True,
        )

    upgradable_system_packages = []
    if target in ["system", "all"]:

        # Update APT cache
        # LC_ALL=C is here to make sure the results are in english
        command = "LC_ALL=C apt-get update -o Acquire::Retries=3"

        # Filter boring message about "apt not having a stable CLI interface"
        # Also keep track of wether or not we encountered a warning...
        warnings = []

        def is_legit_warning(m):
            legit_warning = (m.rstrip()
                             and "apt does not have a stable CLI interface"
                             not in m.rstrip())
            if legit_warning:
                warnings.append(m)
            return legit_warning

        callbacks = (
            # stdout goes to debug
            lambda l: logger.debug(l.rstrip()),
            # stderr goes to warning except for the boring apt messages
            lambda l: logger.warning(l.rstrip())
            if is_legit_warning(l) else logger.debug(l.rstrip()),
        )

        logger.info(m18n.n("updating_apt_cache"))

        returncode = call_async_output(command, callbacks, shell=True)

        if returncode != 0:
            raise YunohostError("update_apt_cache_failed",
                                sourceslist="\n".join(_dump_sources_list()))
        elif warnings:
            logger.error(
                m18n.n(
                    "update_apt_cache_warning",
                    sourceslist="\n".join(_dump_sources_list()),
                ))

        upgradable_system_packages = list(_list_upgradable_apt_packages())
        logger.debug(m18n.n("done"))

    upgradable_apps = []
    if target in ["apps", "all"]:
        try:
            _update_apps_catalog()
        except YunohostError as e:
            logger.error(str(e))

        upgradable_apps = list(_list_upgradable_apps())

    if len(upgradable_apps) == 0 and len(upgradable_system_packages) == 0:
        logger.info(m18n.n("already_up_to_date"))

    return {"system": upgradable_system_packages, "apps": upgradable_apps}