Skip to content

Commit

Permalink
print all installed binary packages instead of source packages
Browse files Browse the repository at this point in the history
  • Loading branch information
decathorpe committed Feb 28, 2020
1 parent f971169 commit 61df18b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 18 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ toml = "^0.5.5"
[profile.release]
codegen-units = 1
lto = true
opt-level = 3

9 changes: 7 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn str_to_karma(string: &str) -> Option<Karma> {
pub fn ask_feedback<'a>(
rl: &mut rustyline::Editor<()>,
update: &'a Update,
builds: &[String],
builds: &[&str],
) -> Result<Feedback<'a>, String> {
print_update(update, builds);

Expand Down Expand Up @@ -94,7 +94,12 @@ pub fn ask_feedback<'a>(

if !comment_lines.is_empty() {
// if both the last line and the current line are empty, break
if comment_lines.last().unwrap().is_empty() && line.is_empty() {
if comment_lines
.last()
.expect("Something went wrong. There must be a last item in a non-empty iterable.")
.is_empty()
&& line.is_empty()
{
break;
};
};
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![warn(missing_docs)]
#![warn(clippy::result_unwrap_used)]
#![warn(clippy::option_unwrap_used)]

//! This crate contains helper functionality that's used by the `fedora-update-feedback` binary.
//! It's contents are probably not useful for external use. But if something turns out to be
Expand Down
36 changes: 30 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct Command {
clear_ignored: bool,
}

#[allow(clippy::cognitive_complexity)]
fn main() -> Result<(), String> {
let args: Command = Command::from_args();

Expand Down Expand Up @@ -68,6 +69,8 @@ fn main() -> Result<(), String> {
// query DNF for installed packages
println!("Querying dnf for installed packages ...");
let installed_packages = get_installed()?;
// query DNF for source -> binary package map
let src_bin_map = get_src_bin_map()?;

println!("Querying bodhi for updates ...");
let mut updates: Vec<Update> = Vec::new();
Expand Down Expand Up @@ -150,7 +153,7 @@ fn main() -> Result<(), String> {
builds_for_update
.entry(update.alias.clone())
.and_modify(|e| e.push(nvr.to_string()))
.or_insert(vec![nvr.to_string()]);
.or_insert_with(|| vec![nvr.to_string()]);
};
}
}
Expand Down Expand Up @@ -189,7 +192,14 @@ fn main() -> Result<(), String> {
// this unwrap is safe since we definitely inserted a value for every update
let builds = builds_for_update.get(update.alias.as_str()).unwrap();

let feedback = ask_feedback(&mut rl, update, &builds)?;
let mut binaries: Vec<&str> = Vec::new();
for build in builds {
if let Some(list) = src_bin_map.get(build) {
binaries.extend(list.iter().map(|s| s.as_str()));
};
}

let feedback = ask_feedback(&mut rl, update, &binaries)?;

match feedback {
Feedback::Cancel => {
Expand Down Expand Up @@ -294,7 +304,7 @@ fn main() -> Result<(), String> {
builds_for_update
.entry(update.alias.clone())
.and_modify(|e| e.push(nvr.to_string()))
.or_insert(vec![nvr.to_string()]);
.or_insert_with(|| vec![nvr.to_string()]);
};
}
}
Expand All @@ -307,7 +317,14 @@ fn main() -> Result<(), String> {
println!(" - {}:", update.title);
// this unwrap is safe since we definitely inserted a value for every update
for build in builds_for_update.get(update.alias.as_str()).unwrap() {
println!(" - {}", build);
let mut binaries: Vec<&str> = Vec::new();
if let Some(list) = src_bin_map.get(build) {
binaries.extend(list.iter().map(|s| s.as_str()));
};

for binary in binaries {
println!(" - {}", binary);
}
}
}
};
Expand Down Expand Up @@ -354,7 +371,7 @@ fn main() -> Result<(), String> {
builds_for_update
.entry(update.alias.clone())
.and_modify(|e| e.push(nvr.to_string()))
.or_insert(vec![nvr.to_string()]);
.or_insert_with(|| vec![nvr.to_string()]);
};
}
}
Expand All @@ -367,7 +384,14 @@ fn main() -> Result<(), String> {
println!(" - {}:", update.title);
// this unwrap is safe since we definitely inserted a value for every update
for build in builds_for_update.get(update.alias.as_str()).unwrap() {
println!(" - {}", build);
let mut binaries: Vec<&str> = Vec::new();
if let Some(list) = src_bin_map.get(build) {
binaries.extend(list.iter().map(|s| s.as_str()));
};

for binary in binaries {
println!(" - {}", binary);
}
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub fn progress_bar(prefix: &str, p: u32, ps: u32) {
);

print!("\r{}", &line);
stdout().flush().unwrap();
stdout().flush().expect("Failed to write to stdout.");
}

/// This helper function pretty-prints an update.
pub fn print_update(update: &Update, builds: &[String]) {
pub fn print_update(update: &Update, builds: &[&str]) {
let date = match &update.date_submitted {
Some(date) => date.to_string(),
None => "(None)".to_string(),
Expand Down
51 changes: 51 additions & 0 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::process::Command;

use bodhi::FedoraRelease;
Expand Down Expand Up @@ -90,3 +91,53 @@ pub fn get_installed() -> Result<Vec<NVR>, String> {

Ok(packages)
}

/// This helper function returns a map from source -> binary package NVRs for installed packages.
pub fn get_src_bin_map() -> Result<HashMap<String, Vec<String>>, String> {
// query dnf for installed binary packages and their corresponding source package
let output = match Command::new("dnf")
.arg("--quiet")
.arg("repoquery")
.arg("--cacheonly")
.arg("--installed")
.arg("--qf")
.arg("%{source_name}-%{version}-%{release} %{name}-%{version}-%{release}.%{arch}")
.output()
{
Ok(output) => output,
Err(error) => return Err(format!("{}", error)),
};

match output.status.code() {
Some(x) if x != 0 => return Err(String::from("Failed to query dnf.")),
Some(_) => {},
None => return Err(String::from("Failed to query dnf.")),
}

let results = match std::str::from_utf8(&output.stdout) {
Ok(result) => result,
Err(error) => return Err(format!("{}", error)),
};

let lines: Vec<&str> = results.trim().split('\n').collect();

let mut pkg_map: HashMap<String, Vec<String>> = HashMap::new();

for line in lines {
let parts: Vec<&str> = line.split(' ').collect();

if parts.len() != 2 {
return Err(String::from("Failed to parse dnf output."));
};

let source = parts.get(0).unwrap();
let binary = parts.get(1).unwrap();

pkg_map
.entry((*source).to_string())
.and_modify(|v| v.push((*binary).to_string()))
.or_insert_with(|| vec![(*binary).to_string()]);
}

Ok(pkg_map)
}

0 comments on commit 61df18b

Please sign in to comment.