feat(fedora): Use dnf repoquery to query packages

Use dnf repoquery to query packages for better interop and more
parsable output.

Signed-off-by: innocentzero <isfarulhaque@proton.me>
This commit is contained in:
innocentzero
2024-04-11 21:08:47 +05:30
parent 1fc8add2c2
commit 24f1dce866
+16 -49
View File
@@ -3,7 +3,6 @@ use std::collections::HashSet;
use std::process::{Command, ExitStatus}; use std::process::{Command, ExitStatus};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use regex::Regex;
use crate::backend::backend_trait::{Backend, Switches, Text}; use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants; use crate::backend::macros::impl_backend_constants;
@@ -17,7 +16,12 @@ pub struct Fedora {
const BINARY: Text = "dnf"; const BINARY: Text = "dnf";
const SECTION: Text = "fedora"; const SECTION: Text = "fedora";
const SWITCHES_INFO: Switches = &["list", "--installed"]; const SWITCHES_INFO: Switches = &[
"repoquery",
"--installed",
"--queryformat",
"%{reponame}/%{name}",
];
const SWITCHES_INSTALL: Switches = &["install"]; const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &[]; const SWITCHES_MAKE_DEPENDENCY: Switches = &[];
const SWITCHES_NOCONFIRM: Switches = &["--assumeyes"]; const SWITCHES_NOCONFIRM: Switches = &["--assumeyes"];
@@ -29,63 +33,26 @@ impl Backend for Fedora {
impl_backend_constants!(); impl_backend_constants!();
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> { fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
let re_str = r"^[0-9A-Za-z_-]*.";
let re = Regex::new(re_str)?;
let mut cmd = Command::new(self.get_binary()); let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_info()); cmd.args(self.get_switches_info());
let output = String::from_utf8(cmd.output()?.stdout)?; let output = String::from_utf8(cmd.output()?.stdout)?;
let packages: HashSet<Package> = output let packages: HashSet<Package> = output.lines().map(|package| package.into()).collect();
.lines()
.map(|line| {
let result = re
.find(
line.split_whitespace()
.next()
.expect("First word cannot be empty!"),
)
.expect("Not a valid package name!");
let mut result = result.as_str().to_string();
result.pop();
result.into()
})
.collect();
Ok(packages) Ok(packages)
} }
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> { fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
let re_str = r"^(([A-Za-z_]*[0-9]*)-)*";
let re = Regex::new(re_str)?;
let mut cmd = Command::new(self.get_binary()); let mut cmd = Command::new(self.get_binary());
cmd.args(&["history", "userinstalled"]); cmd.args([
"repoquery",
"--userinstalled",
"--queryformat",
"%{reponame}/%{name}",
]);
let output = String::from_utf8(cmd.output()?.stdout)?; let output = String::from_utf8(cmd.output()?.stdout)?;
let packages: HashSet<Package> = output let packages: HashSet<Package> = output.lines().map(|package| package.into()).collect();
.lines()
.skip(1)
.map(|line| {
let word = re.find(line).expect("Not a valid package name");
let mut word = word.as_str().to_string();
word.pop();
let pack = word.rsplit_once('-').map_or(word.clone(), |(pack, term)| {
let mut value = true;
for i in term.chars() {
if !i.is_numeric() {
value = false;
break;
}
}
if !value {
pack.to_string() + "-" + term
} else {
pack.to_string()
}
});
pack.into()
})
.collect();
Ok(packages) Ok(packages)
} }
@@ -131,7 +98,7 @@ impl Backend for Fedora {
impl Fedora { impl Fedora {
pub fn new() -> Self { pub fn new() -> Self {
Fedora { Self {
packages: HashSet::new(), packages: HashSet::new(),
} }
} }