From 1374aac171d8db04a1cd191ddb43701c796d60b9 Mon Sep 17 00:00:00 2001 From: Dheyay Date: Wed, 6 Nov 2024 15:35:56 -0800 Subject: [PATCH] Update oracular apt output --- apt-hook/json-hook.cc | 86 +++++++++++++++++++++++++------------------ apt-hook/json-hook.hh | 6 +++ uaclient/apt_news.py | 8 ++-- 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/apt-hook/json-hook.cc b/apt-hook/json-hook.cc index bb4d3a16cc..ed04d067e8 100644 --- a/apt-hook/json-hook.cc +++ b/apt-hook/json-hook.cc @@ -98,6 +98,32 @@ bool version_from_origin_and_archive_ends_with(json_object *version, std::string return false; } +release_info get_release_info() { + release_info info = {"", "", false}; + std::ifstream machine_series_file("/etc/lsb-release"); + if (machine_series_file.is_open()) { + std::string currentLine; + while(std::getline(machine_series_file, currentLine)) { + size_t position = currentLine.find("="); // Find position of equals sign as separator + std::string key = currentLine.substr(0, position); + std::string value = currentLine.substr(position + 1); + + if (!value.empty() && value.front() == '"' && value.back() == '"') { + value = value.substr(1, value.length() - 2); + } + + if (key=="DISTRIB_RELEASE") { + info.release = value; + } else if (key == "DISTRIB_CODENAME") { + info.series = value; + } else if (key == "DISTRIB_DESCRIPTION" && value.find("LTS") != std::string::npos) { + info.is_lts = true; + } + } + } + return info; +} + bool count_security_packages_from_apt_stats_json(json_object *stats, security_package_counts &result) { bool has_key = false; result.standard = 0; @@ -236,18 +262,20 @@ bool collect_pro_packages_from_pre_prompt_json(json_object *pre_prompt, std::vec #define MAX_COUNT_MESSAGE_LEN 256 std::string create_count_message(security_package_counts &counts) { char buf[MAX_COUNT_MESSAGE_LEN] = {0}; + release_info info = get_release_info(); + std::string lts_text = info.is_lts ? "LTS " : ""; if (counts.esm_apps == 0) { if (counts.esm_infra == 0) { if (counts.standard == 0) { return ""; } else if (counts.standard == 1) { - return std::string(gettext("1 standard LTS security update")); + return std::string(gettext("1 standard ")) + lts_text + gettext("security update"); } else if (counts.standard > 1) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates")).c_str(), counts.standard ); return std::string(buf); @@ -256,12 +284,12 @@ std::string create_count_message(security_package_counts &counts) { if (counts.standard == 0) { return std::string(gettext("1 esm-infra security update")); } else if (counts.standard == 1) { - return std::string(gettext("1 standard LTS security update and 1 esm-infra security update")); + return std::string(gettext("1 standard ")) + lts_text + gettext("security update and 1 esm-infra security update"); } else if (counts.standard > 1) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates and 1 esm-infra security update"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates and 1 esm-infra security update")).c_str(), counts.standard ); return std::string(buf); @@ -279,7 +307,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("1 standard LTS security update and %lu esm-infra security updates"), + (std::string(gettext("1 standard ")) + lts_text + gettext("security update and %lu esm-infra security updates")).c_str(), counts.esm_infra ); return std::string(buf); @@ -287,7 +315,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates and %lu esm-infra security updates"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates and %lu esm-infra security updates")).c_str(), counts.standard, counts.esm_infra ); @@ -299,12 +327,12 @@ std::string create_count_message(security_package_counts &counts) { if (counts.standard == 0) { return std::string(gettext("1 esm-apps security update")); } else if (counts.standard == 1) { - return std::string(gettext("1 standard LTS security update and 1 esm-apps security update")); + return std::string(gettext("1 standard ")) + lts_text + gettext("security update and 1 esm-apps security update"); } else if (counts.standard > 1) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates and 1 esm-apps security update"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates and 1 esm-apps security update")).c_str(), counts.standard ); return std::string(buf); @@ -313,12 +341,12 @@ std::string create_count_message(security_package_counts &counts) { if (counts.standard == 0) { return std::string(gettext("1 esm-infra security update and 1 esm-apps security update")); } else if (counts.standard == 1) { - return std::string(gettext("1 standard LTS security update, 1 esm-infra security update and 1 esm-apps security update")); + return std::string(gettext("1 standard ")) + lts_text + gettext("security update, 1 esm-infra security update and 1 esm-apps security update"); } else if (counts.standard > 1) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates, 1 esm-infra security update and 1 esm-apps security update"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates, 1 esm-infra security update and 1 esm-apps security update")).c_str(), counts.standard ); return std::string(buf); @@ -336,7 +364,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("1 standard LTS security update, %lu esm-infra security updates and 1 esm-apps security update"), + (std::string(gettext("1 standard ")) + lts_text + gettext("security update, %lu esm-infra security updates and 1 esm-apps security update")).c_str(), counts.esm_infra ); return std::string(buf); @@ -344,7 +372,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates, %lu esm-infra security updates and 1 esm-apps security update"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates, %lu esm-infra security updates and 1 esm-apps security update")).c_str(), counts.standard, counts.esm_infra ); @@ -365,7 +393,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("1 standard LTS security update and %lu esm-apps security updates"), + (std::string(gettext("1 standard ")) + lts_text + gettext("security update and %lu esm-apps security updates")).c_str(), counts.esm_apps ); return std::string(buf); @@ -373,7 +401,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates and %lu esm-apps security updates"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates and %lu esm-apps security updates")).c_str(), counts.standard, counts.esm_apps ); @@ -392,7 +420,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("1 standard LTS security update, 1 esm-infra security update and %lu esm-apps security updates"), + (std::string(gettext("1 standard ")) + lts_text + gettext("security update, 1 esm-infra security update and %lu esm-apps security updates")).c_str(), counts.esm_apps ); return std::string(buf); @@ -400,7 +428,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates, 1 esm-infra security update and %lu esm-apps security updates"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates, 1 esm-infra security update and %lu esm-apps security updates")).c_str(), counts.standard, counts.esm_apps ); @@ -420,7 +448,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("1 standard LTS security update, %lu esm-infra security updates and %lu esm-apps security updates"), + (std::string(gettext("1 standard ")) + lts_text + gettext("security update, %lu esm-infra security updates and %lu esm-apps security updates")).c_str(), counts.esm_infra, counts.esm_apps ); @@ -429,7 +457,7 @@ std::string create_count_message(security_package_counts &counts) { std::snprintf( buf, MAX_COUNT_MESSAGE_LEN, - gettext("%lu standard LTS security updates, %lu esm-infra security updates and %lu esm-apps security updates"), + (std::string(gettext("%lu standard ")) + lts_text + gettext("security updates, %lu esm-infra security updates and %lu esm-apps security updates")).c_str(), counts.standard, counts.esm_infra, counts.esm_apps @@ -478,20 +506,6 @@ 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(); @@ -578,9 +592,9 @@ void print_learn_more_with_context() { } void print_package_names(std::vector package_names) { - std::string machine_series = get_machine_series(); + release_info info = get_release_info(); std::string curr_line = " "; - if (machine_series != "" && machine_series == "oracular") { + if (info.release != "" && info.release >= "24.10") { curr_line = " "; } for (std::string &name : package_names) { @@ -708,10 +722,10 @@ int run() security_package_counts counts; success = count_security_packages_from_apt_stats_json(hook_req.params, counts); if (success) { + release_info info = get_release_info(); std::string message = create_count_message(counts); - std::string machine_series = get_machine_series(); if (message != "") { - if (machine_series != "" && machine_series == "oracular") { + if (info.release != "" && info.release >= "24.10") { std::cout << " " << message << std::endl; } else { diff --git a/apt-hook/json-hook.hh b/apt-hook/json-hook.hh index b6ed5048b5..fc1ca16b43 100644 --- a/apt-hook/json-hook.hh +++ b/apt-hook/json-hook.hh @@ -13,10 +13,16 @@ struct security_package_counts { long unsigned int esm_infra; long unsigned int esm_apps; }; +struct release_info { + std::string release; // e.g. "24.04" + std::string series; // e.g. "noble" + bool is_lts; // true for LTS releases +}; enum ESMType {APPS, INFRA}; bool read_jsonrpc_request(std::istream &in, jsonrpc_request &req); +release_info get_release_info(); bool string_ends_with(std::string str, std::string ends_with); bool version_from_origin_and_archive_ends_with(json_object *version, std::string from_origin, std::string archive_ends_with); bool count_security_packages_from_apt_stats_json(json_object *stats, security_package_counts &result); diff --git a/uaclient/apt_news.py b/uaclient/apt_news.py index 2997653966..3e6340273f 100644 --- a/uaclient/apt_news.py +++ b/uaclient/apt_news.py @@ -265,12 +265,12 @@ def local_apt_news(cfg: UAConfig) -> Optional[str]: def format_news_for_apt_update(news: str) -> str: 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" + lines = [" " + line for line in news.split("\n")] + return "{0}\n{1}\n\n".format(prefix, "\n".join(lines)) else: prefix = "#" - lines = [f"{prefix} {line}" for line in news.split("\n")] - return f"{prefix}\n{chr(10).join(lines)}\n{prefix}\n" + lines = [prefix + " " + line for line in news.split("\n")] + return "{0}\n{1}\n{0}\n".format(prefix, "\n".join(lines)) def update_apt_news(cfg: UAConfig):