Skip to content

Commit

Permalink
Update apt hook message format for oracular
Browse files Browse the repository at this point in the history
  • Loading branch information
dheyay committed Nov 5, 2024
1 parent 116934e commit 898f2cd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
28 changes: 26 additions & 2 deletions apt-hook/json-hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::string> series_names = {"xenial", "bionic", "focal", "jammy", "noble", "oracular"};
if (machine_series_file.is_open()) {
std::string file_contents((std::istreambuf_iterator<char>(machine_series_file)), (std::istreambuf_iterator<char>()));
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();
Expand Down Expand Up @@ -564,7 +578,11 @@ void print_learn_more_with_context() {
}

void print_package_names(std::vector<std::string> 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;
Expand Down Expand Up @@ -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") {
Expand Down
3 changes: 2 additions & 1 deletion features/apt_messages.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions uaclient/apt_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 898f2cd

Please sign in to comment.