From 898f2cde5d7a898a08d484af31fb5ec8da6ed979 Mon Sep 17 00:00:00 2001 From: Dheyay Date: Tue, 5 Nov 2024 13:57:01 -0800 Subject: [PATCH] Update apt hook message format for oracular --- apt-hook/json-hook.cc | 28 ++++++++++++++++++++++++++-- features/apt_messages.feature | 3 ++- uaclient/apt_news.py | 13 ++++++++----- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/apt-hook/json-hook.cc b/apt-hook/json-hook.cc index 45650c5961..bb4d3a16cc 100644 --- a/apt-hook/json-hook.cc +++ b/apt-hook/json-hook.cc @@ -193,7 +193,7 @@ bool collect_pro_packages_from_pre_prompt_json(json_object *pre_prompt, std::vec continue; } std::string package_mode(json_object_get_string(tmp)); - + has_key = json_object_object_get_ex(package, "name", &tmp); if (!has_key) { continue; @@ -478,6 +478,20 @@ ESMInfraSeries get_esm_infra_series() { return ret; } +std::string get_machine_series() { + std::ifstream machine_series_file("/etc/lsb-release"); + std::vector series_names = {"xenial", "bionic", "focal", "jammy", "noble", "oracular"}; + if (machine_series_file.is_open()) { + std::string file_contents((std::istreambuf_iterator(machine_series_file)), (std::istreambuf_iterator())); + for (const auto &series_name : series_names) { + if (file_contents.find(series_name) != file_contents.npos) { + return series_name; + } + } + } + return ""; +} + void print_learn_more_with_context() { CloudID cloud_id = get_cloud_id(); ESMInfraSeries esm_infra_series = get_esm_infra_series(); @@ -564,7 +578,11 @@ void print_learn_more_with_context() { } void print_package_names(std::vector package_names) { + std::string machine_series = get_machine_series(); std::string curr_line = " "; + if (machine_series != "" && machine_series == "oracular") { + curr_line = " "; + } for (std::string &name : package_names) { if ((curr_line.length() + 1 + name.length()) >= 79) { std::cout << curr_line << std::endl; @@ -691,8 +709,14 @@ int run() success = count_security_packages_from_apt_stats_json(hook_req.params, counts); if (success) { std::string message = create_count_message(counts); + std::string machine_series = get_machine_series(); if (message != "") { - std::cout << message << std::endl; + if (machine_series != "" && machine_series == "oracular") { + std::cout << " " << message << std::endl; + } + else { + std::cout << message << std::endl; + } } } } else if (hook_req.method == "org.debian.apt.hooks.install.pre-prompt") { diff --git a/features/apt_messages.feature b/features/apt_messages.feature index cb18f96eaa..480329d03a 100644 --- a/features/apt_messages.feature +++ b/features/apt_messages.feature @@ -702,7 +702,8 @@ Feature: APT Messages Reading state information... Calculating upgrade... - one + APT news: + one Summary: Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 0 diff --git a/uaclient/apt_news.py b/uaclient/apt_news.py index 68ea36e279..2997653966 100644 --- a/uaclient/apt_news.py +++ b/uaclient/apt_news.py @@ -263,11 +263,14 @@ def local_apt_news(cfg: UAConfig) -> Optional[str]: def format_news_for_apt_update(news: str) -> str: - prefix = "" if system.get_release_info().series == "oracular" else "#" - lines = [ - (prefix + " " + line) if prefix else line for line in news.split("\n") - ] - return "{0}\n{1}\n{0}\n".format(prefix, "\n".join(lines)) + if system.get_release_info().series == "oracular": + prefix = "\nAPT news:" + lines = [f" {line}" for line in news.split("\n")] + return f"{prefix}\n{chr(10).join(lines)}\n\n" + else: + prefix = "#" + lines = [f"{prefix} {line}" for line in news.split("\n")] + return f"{prefix}\n{chr(10).join(lines)}\n{prefix}\n" def update_apt_news(cfg: UAConfig):